trait definition and struct-tuple example
This commit is contained in:
commit
1a43c2d1a5
7 changed files with 112 additions and 0 deletions
28
src/example.rs
Normal file
28
src/example.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use crate::generic::{Field, Sum, Generic};
|
||||
|
||||
|
||||
struct IntPair {
|
||||
a: i32,
|
||||
b: i32
|
||||
}
|
||||
|
||||
impl Generic for IntPair {
|
||||
type Representation = Sum<Field<i32>, Field<i32>>;
|
||||
fn generalize(self) -> Self::Representation {
|
||||
Sum {
|
||||
left: Field { value: self.a },
|
||||
right: Field { value: self.b }
|
||||
}
|
||||
}
|
||||
fn specialize(rep: Self::Representation) -> Self {
|
||||
let Sum {
|
||||
left: Field { value: a },
|
||||
right: Field { value: b },
|
||||
} = rep;
|
||||
IntPair { a, b }
|
||||
}
|
||||
}
|
||||
|
||||
fn convert(x: IntPair) -> (i32, i32) {
|
||||
Generic::specialize(x.generalize())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue