Created
July 19, 2020 22:37
-
-
Save rrohrer/aebadd9b3f2bb89b48b209ccbb978996 to your computer and use it in GitHub Desktop.
Streaming Iterators in a Trait
This file contains 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
trait HasIter<'a, T> { | |
type IterType: Iterator<Item = T>; | |
} | |
trait Collection<T>: for<'a> HasIter<'a, &'a T> { | |
fn iter<'a>(&'a self) -> <Self as HasIter<'a, &'a T>>::IterType; | |
} | |
struct Bar<T>(T); | |
struct BarIter<'a, T>(&'a Bar<T>); | |
impl<'a, T> Iterator for BarIter<'a, T> { | |
type Item = &'a T; | |
fn next(&mut self) -> Option<&'a T> { | |
Some(&(self.0).0) | |
} | |
} | |
impl<'a, T> HasIter<'a, &'a T> for Bar<T> { | |
type IterType = BarIter<'a, T>; | |
} | |
impl<T> Collection<T> for Bar<T> { | |
fn iter<'a>(&'a self) -> BarIter<'a, T> { | |
BarIter(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment