Created
August 2, 2017 20:58
-
-
Save pythoneer/0b97809aa859b16a6e6c78e65ba2dde5 to your computer and use it in GitHub Desktop.
generic iteratos
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: Copy; | |
type M: Copy; | |
type L: Copy; | |
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; | |
fn create(f: Self::F, m: Self::M, l: Self::L) -> Self; | |
} | |
//a builder that can build containers | |
trait ContainerBuilder<C: Container> { | |
fn first(&mut self, f: C::F) -> &mut Self; | |
fn middle(&mut self, m: C::M) -> &mut Self; | |
fn last(&mut self, l: C::L) -> &mut Self; | |
fn build(&self) -> C; | |
} | |
//a container holding integers | |
#[derive(Default)] | |
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 | |
} | |
fn create(f: Self::F, m: Self::M, l: Self::L) -> Self { | |
IntContainer{first: f, middle: m, last: l} | |
} | |
} | |
//a builder that can build integer container | |
#[derive(Default)] | |
struct AContainerBuilder<C: Container> { | |
first: C::F, | |
middle: C::M, | |
last: C::L, | |
} | |
impl<C: Container> ContainerBuilder<C> for AContainerBuilder<C> { | |
fn first(&mut self, f: C::F) -> &mut Self { | |
self.first = f; | |
self | |
} | |
fn middle(&mut self, m: C::M) -> &mut Self { | |
self.middle = m; | |
self | |
} | |
fn last(&mut self, l: C::L) -> &mut Self { | |
self.last = l; | |
self | |
} | |
fn build(&self) -> C { | |
C::create(self.first, self.middle, 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 = AContainerBuilder::<IntContainer>::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