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
use Read;
use error::ChainError;

/// Chains two readers.
///
/// All reads are forwarded to the first reader until it returns end. Then all other reads are
/// forwarded to the second reader.
pub struct Chain<F, S> {
    first: F,
    second: S,
    first_finished: bool,
}

impl<F: Read, S: Read> Chain<F, S> {
    /// Creates chain of readers.
    pub fn new(first: F, second: S) -> Self {
        Chain {
            first: first,
            second: second,
            first_finished: false,
        }
    }
}

impl<F: Read, S: Read> Read for Chain<F, S> {
    type ReadError = ChainError<F::ReadError, S::ReadError>;

    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::ReadError> {
        if self.first_finished {
            self.second
                .read(buf)
                .map_err(ChainError::Second)
        } else {
            self.first
                .read(buf)
                .map_err(ChainError::First)
                .map(|l| {
                    if l == 0 {
                        self.first_finished = true;
                    }
                    l
                })
        }
    }
}