Crate teewriter

Crate teewriter 

Source
Expand description

Utilities for working with sync and async tee writers This crate provides implementations for creating tee writers that can write to two or more underlying writers simultaneously. It supports both synchronous writers (implementing std::io::Write) and asynchronous writers (implementing tokio::io::AsyncWrite or futures::io::AsyncWrite).

§Example with std::io::Write

#[cfg(feature = “io-std”)]

use teewriter::TeeWriterExt;
use std::io::Write;
let mut writer1 = Vec::new();
let mut writer2 = Vec::new();
let mut tee = writer1.tee(&mut writer2);
tee.write_all(b"Hello, world!").unwrap();
let (writer1, writer2, offset) = tee.into_inner();
if offset != 0 {
  panic!("Writers are out of sync by {}", offset);
}
assert_eq!(writer1, b"Hello, world!");
assert_eq!(writer2, b"Hello, world!");

§Example with tokio::io::AsyncWrite

#[cfg(feature = “tokio”)]

use teewriter::TeeWriterExt;
use tokio::io::AsyncWriteExt;
#[tokio::main]
async fn main() {
   let mut writer1 = tokio::io::sink();
   let mut writer2 = tokio::io::sink();
   let mut tee = writer1.tee(&mut writer2);
   tee.write_all(b"Hello, async world!").await.unwrap();
   let (writer1, writer2, offset) = tee.into_inner();
   if offset != 0 {
     panic!("Writers are out of sync by {}", offset);
   }
}

§Example with futures::io::AsyncWrite

#[cfg(feature = “futures”)]

use teewriter::TeeWriterExt;
use futures::io::AsyncWriteExt;
use futures::executor::block_on;
fn main() {
  let mut writer1 = futures::io::sink();
  let mut writer2 = futures::io::sink();
  let mut tee = writer1.tee(&mut writer2);
  block_on(tee.write_all(b"Hello, futures world!")).unwrap();
  let (writer1, writer2, offset) = tee.into_inner();
  if offset != 0 {
    panic!("Writers are out of sync by {}", offset);
  }
  assert_eq!(offset, 0);
}

Modules§

either
tee

Structs§

TeeWriter
A writer that writes to two underlying writers, in sync

Enums§

Either

Traits§

EitherExt
TeeWriterExt
Extension trait to add tee method to any writer