Created
January 17, 2019 03:19
-
-
Save rust-play/63f021e838c9e9b78ea8ffc4d4c12c98 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
struct Flatten<I, S> | |
where | |
I: Iterator, | |
S: Iterator, | |
{ | |
base: I, | |
inner: S, | |
} | |
impl<I> Flatten<I, <I::Item as IntoIterator>::IntoIter> | |
where | |
I: Iterator, | |
<I as Iterator>::Item: IntoIterator, | |
{ | |
fn new (mut it: I) -> Flatten<I, <I::Item as IntoIterator>::IntoIter> { | |
let inner = it.next().unwrap().into_iter(); | |
Flatten { | |
base: it, | |
inner, | |
} | |
} | |
} | |
impl<I> Iterator for Flatten<I, <I::Item as IntoIterator>::IntoIter> | |
where | |
I: Iterator, | |
<I as Iterator>::Item: IntoIterator, | |
{ | |
type Item = <<I as Iterator>::Item as IntoIterator>::Item; | |
fn next(&mut self) -> Option<<I::Item as IntoIterator>::Item> { | |
loop { | |
match self.inner.next() { | |
Some(a) => return Some(a), | |
None => {}, | |
} | |
match self.base.next() { | |
Some(a) => self.inner = a.into_iter(), | |
None => return None, | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment