]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
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 | { | |
14 | pub fn new(it: I) -> LazyBuffer<I> { | |
15 | let mut it = it; | |
16 | let mut buffer = Vec::new(); | |
17 | let done; | |
18 | if let Some(first) = it.next() { | |
19 | buffer.push(first); | |
20 | done = false; | |
21 | } else { | |
22 | done = true; | |
23 | } | |
24 | LazyBuffer { | |
25 | it: it, | |
26 | done: done, | |
27 | buffer: buffer, | |
28 | } | |
29 | } | |
30 | ||
31 | pub fn len(&self) -> usize { | |
32 | self.buffer.len() | |
33 | } | |
34 | ||
35 | pub fn is_done(&self) -> bool { | |
36 | self.done | |
37 | } | |
38 | ||
39 | pub fn get_next(&mut self) -> bool { | |
40 | if self.done { | |
41 | return false; | |
42 | } | |
43 | let next_item = self.it.next(); | |
44 | match next_item { | |
45 | Some(x) => { | |
46 | self.buffer.push(x); | |
47 | true | |
48 | } | |
49 | None => { | |
50 | self.done = true; | |
51 | false | |
52 | } | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | impl<I, J> Index<J> for LazyBuffer<I> | |
58 | where | |
59 | I: Iterator, | |
60 | I::Item: Sized, | |
61 | Vec<I::Item>: Index<J> | |
62 | { | |
63 | type Output = <Vec<I::Item> as Index<J>>::Output; | |
64 | ||
65 | fn index(&self, _index: J) -> &Self::Output { | |
66 | self.buffer.index(_index) | |
67 | } | |
68 | } |