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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tee

[![Build Status](https://travis-ci.org/softprops/tee.svg)](https://travis-ci.org/softprops/tee) [![Coverage Status](https://coveralls.io/repos/softprops/tee/badge.svg?branch=master&service=github)](https://coveralls.io/github/softprops/tee?branch=master)
[![Build Status](https://travis-ci.org/softprops/tee.svg)](https://travis-ci.org/softprops/tee) [![Coverage Status](https://coveralls.io/repos/softprops/tee/badge.svg?branch=master&service=github)](https://coveralls.io/github/softprops/tee?branch=master) [![Docs](https://docs.rs/tee/badge.svg)](https://docs.rs/tee)

A rustlang adapter for readers which delegate read bytes to a writer, adapted from the standard library's `std::io::Read#tee` which has since been deprecated.

Expand Down Expand Up @@ -31,4 +31,6 @@ With `tee`, this looks more like
let tee_reader = tee::TeeReader::new(reader, writer);
```

However, importing `TeeExt` will allow usage exactly like the original stdlib function.

Doug Tangren (softprops) 2015
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
//! An adapter for objects implementing `Read` which copies read bytes and writes them to a
//! provided reader.
//!
//! # Example
//!
//! ```rust
//! # extern crate tee;
//! use std::io::Read;
//! use tee::TeeExt;
//! # fn main() {
//! let reader = "It's over 9000!".as_bytes();
//! let mut teeout = Vec::new();
//! let mut stdout = Vec::new();
//! reader.tee(&mut teeout).read_to_end(&mut stdout).unwrap();
//! assert_eq!(teeout, stdout);
//! # }
//! ```

use std::io::{Read, Result, Write};

/// An adapter for readers whose inputs
Expand All @@ -13,6 +31,7 @@ impl<R: Read, W: Write> TeeReader<R, W> {
/// writer. The write operation must complete before the read completes.
///
/// Errors reported by the write operation will be interpreted as errors for the read
#[inline]
pub fn new(reader: R, writer: W) -> TeeReader<R, W> {
TeeReader {
reader: reader,
Expand All @@ -22,13 +41,27 @@ impl<R: Read, W: Write> TeeReader<R, W> {
}

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

/// An extension trait, allowing `.tee()` to be constructed in the same manner as the old
/// `std::io::Tee`.
pub trait TeeExt: Sized + Read {
fn tee<W: Write>(self, writer: W) -> TeeReader<Self, W>;
}

impl<R: Read> TeeExt for R {
#[inline]
fn tee<W: Write>(self, writer: W) -> TeeReader<R, W> {
TeeReader::new(self, writer)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -45,4 +78,13 @@ mod tests {
}
assert_eq!(teeout, stdout);
}

#[test]
fn tee_ext() {
let reader = "It's over 9000!".as_bytes();
let mut teeout = Vec::new();
let mut stdout = Vec::new();
reader.tee(&mut teeout).read_to_end(&mut stdout).unwrap();
assert_eq!(teeout, stdout);
}
}