diff options
author | Till Höppner | 2016-02-25 06:48:03 +0100 |
---|---|---|
committer | Till Höppner | 2016-02-25 06:48:03 +0100 |
commit | 9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a (patch) | |
tree | 1ca71876029cb466aa6230f1aead05b32f19bf6d /cli/src/chain.rs | |
parent | 685aac1cc537692b2cf9342dcb6c26fa74c3c920 (diff) | |
download | ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.tar.gz ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.tar.xz ilc-9f5dd9dad6b13476bab2c6eb3c6528f8ad49311a.zip |
Refactor... everything.
Diffstat (limited to 'cli/src/chain.rs')
-rw-r--r-- | cli/src/chain.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cli/src/chain.rs b/cli/src/chain.rs new file mode 100644 index 0000000..a8014b8 --- /dev/null +++ b/cli/src/chain.rs @@ -0,0 +1,54 @@ +use std::io::{Read, Result, Write}; + +pub struct Chain<T> { + elem: Vec<T>, + index: usize, +} + +impl<T: Read> Read for Chain<T> { + fn read(&mut self, buf: &mut [u8]) -> Result<usize> { + loop { + match self.elem.get_mut(self.index) { + Some(ref mut r) => { + match try!(r.read(buf)) { + 0 => self.index += 1, + n => return Ok(n), + } + } + None => return Ok(0), + } + } + } +} + +impl<T: Write> Write for Chain<T> { + fn write(&mut self, buf: &[u8]) -> Result<usize> { + loop { + match self.elem.get_mut(self.index) { + Some(ref mut r) => { + match try!(r.write(buf)) { + 0 => self.index += 1, + n => return Ok(n), + } + } + None => return Ok(0), + } + } + } + + fn flush(&mut self) -> Result<()> { + match self.elem.get_mut(self.index) { + Some(ref mut r) => r.flush(), + None => Ok(()), + } + } +} + +impl<T> Chain<T> { + pub fn new(elem: Vec<T>) -> Chain<T> { + Chain { + index: 0, + elem: elem, + } + } +} |