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