Is there a way to make storing iterators less wordy?
I got something in principle like the following code, but wherever I am using the stream, I now have to sprinkle the <I: Iterator<Item=usize>>
. If there are many usages, this becomes quite tedious, especially when considering that the type of stream source does not matter for using it.
struct Stream<I: Iterator<Item=usize>> {
source: I,
}
impl<I: Iterator<Item=usize>> Stream<I> {
fn new(source: I) -> Stream<I> {
Stream { source }
}
fn advance(&mut self) -> Option<usize> {
self.source.next()
}
}
fn main() {
let mut stream = Stream::new(0..8);
assert_eq!(stream.advance(), Some(0));
}
I tried stuffing the iterator into a box. However, this does not compile due to the parameter type
I may not live long enough
.
struct Stream {
source: Box<dyn Iterator<Item=usize>>,
}
impl Stream {
fn new<I: Iterator<Item=usize>>(source: I) -> Stream {
Stream { source: Box::from(source) }
}
fn advance(&mut self) -> Option<usize> {
self.source.next()
}
}
fn main() {
let mut stream = Stream::new(0..8);
assert_eq!(stream.advance(), Some(0));
}
Is there a way to make this work, or a different way to get rid of the iterator type parameter?