concat/
lib.rs

1//! Provides the Concat reader adaptor, which wraps around an iterator of readers and exposes its
2//! items' contents sequentially. Thus, the contents read from a Concat instance will be the
3//! concatenation of the items' contents.
4
5use 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    /// Returns a reference to the item last read, or None if the iterator has been exhausted.
18    ///
19    /// This is useful for error handling and reporting: if a read operation fails, the reference
20    /// returned will point to the item which caused the the error.
21    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            // The current reader reached the end so we have to advance the iterator and try again.
48            self.curr = self.iter.next();
49            self.read(buf)
50        }
51    }
52}
53
54mod tests;