Last active
August 2, 2017 18:28
-
-
Save pythoneer/5e9c2c37cd572c0d0f4c9063bd279d47 to your computer and use it in GitHub Desktop.
rust_generic_concrete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a container holding all kinds of stuff | |
trait Container { | |
type F; | |
type M; | |
type L; | |
fn contains(&self, first: &Self::F, middle: &Self::M, last: &Self::L) -> bool; | |
fn first(&self) -> &Self::F; | |
fn middle(&self) -> &Self::M; | |
fn last(&self) -> &Self::L; | |
} | |
//a builder that can build containers | |
trait ContainerBuilder { | |
type CON: Container; | |
fn first(&mut self, f: <Self::CON as Container>::F) -> &mut Self; | |
fn middle(&mut self, m: <Self::CON as Container>::M) -> &mut Self; | |
fn last(&mut self, l: <Self::CON as Container>::L) -> &mut Self; | |
fn build(&self) -> Self::CON; | |
} | |
//a container holding integers | |
struct IntContainer { | |
first: i32, | |
middle: i32, | |
last: i32, | |
} | |
impl Container for IntContainer { | |
type F = i32; | |
type M = i32; | |
type L = i32; | |
fn contains(&self, first: &Self::F, middle: &Self::M, last: &Self::L) -> bool { | |
self.first == *first && self.middle == *middle && self.last == *last | |
} | |
fn first(&self) -> &Self::F { | |
&self.first | |
} | |
fn middle(&self) -> &Self::M { | |
&self.middle | |
} | |
fn last(&self) -> &Self::L { | |
&self.last | |
} | |
} | |
//a builder that can build integer container | |
#[derive(Default)] | |
struct IntContainerBuilder { | |
first: i32, | |
middle: i32, | |
last: i32, | |
} | |
impl ContainerBuilder for IntContainerBuilder { | |
type CON = IntContainer; | |
fn first(&mut self, f: <Self::CON as Container>::F) -> &mut Self { | |
self.first = f; | |
self | |
} | |
fn middle(&mut self, m: <Self::CON as Container>::M) -> &mut Self { | |
self.middle = m; | |
self | |
} | |
fn last(&mut self, l: <Self::CON as Container>::L) -> &mut Self { | |
self.last = l; | |
self | |
} | |
fn build(&self) -> Self::CON { | |
Self::CON{first: self.first, middle: self.middle, last: self.last} | |
} | |
} | |
fn first<C: Container>(container: &C) -> &C::F { | |
container.first() | |
} | |
fn middle<C: Container>(container: &C) -> &C::M { | |
container.middle() | |
} | |
fn last<C: Container>(container: &C) -> &C::L { | |
container.last() | |
} | |
fn contains<C: Container>(container: &C, first: &C::F, middle: &C::M, last: &C::L) -> bool { | |
container.contains(first, middle, last) | |
} | |
fn main() { | |
let ic = IntContainerBuilder::default().first(1).middle(2).last(3).build(); | |
let last_item = last(&ic); | |
println!("last {}", last_item); | |
let contains = contains(&ic, &1, &2, &3); | |
println!("contains {}", contains); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment