1 use std::ops::Index; 2 3 #[derive(Debug, Clone)] 4 pub struct LazyBuffer<I: Iterator> { 5 pub it: I, 6 done: bool, 7 buffer: Vec<I::Item>, 8 } 9 10 impl<I> LazyBuffer<I> 11 where 12 I: Iterator, 13 { new(it: I) -> LazyBuffer<I>14 pub fn new(it: I) -> LazyBuffer<I> { 15 LazyBuffer { 16 it, 17 done: false, 18 buffer: Vec::new(), 19 } 20 } 21 len(&self) -> usize22 pub fn len(&self) -> usize { 23 self.buffer.len() 24 } 25 is_done(&self) -> bool26 pub fn is_done(&self) -> bool { 27 self.done 28 } 29 get_next(&mut self) -> bool30 pub fn get_next(&mut self) -> bool { 31 if self.done { 32 return false; 33 } 34 let next_item = self.it.next(); 35 match next_item { 36 Some(x) => { 37 self.buffer.push(x); 38 true 39 } 40 None => { 41 self.done = true; 42 false 43 } 44 } 45 } 46 } 47 48 impl<I, J> Index<J> for LazyBuffer<I> 49 where 50 I: Iterator, 51 I::Item: Sized, 52 Vec<I::Item>: Index<J> 53 { 54 type Output = <Vec<I::Item> as Index<J>>::Output; 55 index(&self, _index: J) -> &Self::Output56 fn index(&self, _index: J) -> &Self::Output { 57 self.buffer.index(_index) 58 } 59 } 60