Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tee"
version = "0.1.0"
version = "0.2.0"
authors = ["softprops <[email protected]>"]
description = "An adapter for readers which delegate reads to a writer"
documentation = "http://softprops.github.io/tee"
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ pub struct TeeReader<R: Read, W: Write> {
writer: W,
}

/// Returns a TeeReader which can be used as Read whose
/// reads delegate bytes read to the provided reader and write to the provided
/// writer. The write operation must complete before the read completes.
///
/// Errors reported by the write operation will be interpreted as errors for the read
pub fn tee<R: Read, W: Write>(reader: R, writer: W) -> TeeReader<R, W> {
TeeReader::new(reader, writer)
}

impl<R: Read, W: Write> TeeReader<R, W> {
/// Returns a TeeReader which can be used as Read whose
/// reads delegate bytes read to the provided reader and write to the provided
Expand All @@ -28,15 +37,14 @@ impl<R: Read, W: Write> TeeReader<R, W> {

impl<R: Read, W: Write> Read for TeeReader<R, W> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let n = try!(self.reader.read(buf));
try!(self.writer.write_all(&buf[..n]));
let n = self.reader.read(buf)?;
self.writer.write_all(&buf[..n])?;
Ok(n)
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Read;

#[test]
Expand All @@ -45,7 +53,7 @@ mod tests {
let mut teeout = Vec::new();
let mut stdout = Vec::new();
{
let mut tee = TeeReader::new(&mut reader, &mut teeout);
let mut tee = super::tee(&mut reader, &mut teeout);
let _ = tee.read_to_end(&mut stdout);
}
assert_eq!(teeout, stdout);
Expand Down