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
//! Provides the Concat reader adaptor, which wraps around an iterator of readers and exposes its
//! items' contents sequentially. Thus, the contents read from a Concat instance will be the
//! concatenation of the items' contents.

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 {
    /// Returns a reference to the item last read, or None if the iterator has been exhausted.
    ///
    /// This is useful for error handling and reporting: if a read operation fails, the reference
    /// returned will point to the item which caused the the error.
    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 {
            // The current reader reached the end so we have to advance the iterator and try again.
            self.curr = self.iter.next();
            self.read(buf)
        }
    }
}

mod tests;