1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::io::{Read, Result};
pub fn concat<I>(iter: I) -> Concat<I> where I: Iterator, <I as Iterator>::Item: Read {
Concat::<I>::from(iter)
}
pub struct Concat<I> where I: Iterator, <I as Iterator>::Item: Read {
iter: I,
curr: Option<<I as Iterator>::Item>,
}
impl<I> Concat<I> where I: Iterator, <I as Iterator>::Item: Read {
pub fn current(&self) -> Option<&<I as Iterator>::Item> {
self.curr.as_ref()
}
}
impl<I> From<I> for Concat<I> where I: Iterator, <I as Iterator>::Item: Read {
fn from(mut iter: I) -> Concat<I> {
let curr = iter.next();
Concat {
iter: iter,
curr: curr,
}
}
}
impl<I> Read for Concat<I> where I: Iterator, <I as Iterator>::Item: Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let n = match self.curr {
None => 0,
Some(ref mut r) => try!(r.read(buf)),
};
if n > 0 || buf.len() == 0 || self.curr.is_none() {
Ok(n)
} else {
self.curr = self.iter.next();
self.read(buf)
}
}
}
mod tests;