Skip to main content

TeeWriter

Struct TeeWriter 

Source
pub struct TeeWriter<P, B> { /* private fields */ }
Expand description

Writer wrapper that mirrors accepted bytes into a branch writer.

TeeWriter writes to the primary writer first, then writes exactly the accepted bytes into the branch writer with Write::write_all. If the branch writer fails, the primary writer may already have accepted bytes and the branch error is returned.

Seeking moves the primary writer first, then seeks the branch writer to the primary writer’s resulting absolute position. If the branch seek fails, the primary writer may already have moved.

§Examples

use std::io::Write;

use qubit_io::TeeWriter;

let primary = Vec::new();
let branch = Vec::new();
let mut writer = TeeWriter::new(primary, branch);

writer.write_all(b"abc")?;
let (primary, branch) = writer.into_inner();

assert_eq!(b"abc", primary.as_slice());
assert_eq!(b"abc", branch.as_slice());

Implementations§

Source§

impl<P, B> TeeWriter<P, B>

Source

pub fn new(primary: P, branch: B) -> Self

Creates a tee writer.

§Parameters
  • primary: Primary destination writer.
  • branch: Secondary writer that mirrors accepted bytes.
§Returns

A new tee writer.

Source

pub fn primary_ref(&self) -> &P

Returns an immutable reference to the primary writer.

§Returns

The primary writer reference.

Source

pub fn primary_mut(&mut self) -> &mut P

Returns a mutable reference to the primary writer.

§Returns

The primary writer reference.

Source

pub fn branch_ref(&self) -> &B

Returns an immutable reference to the branch writer.

§Returns

The branch writer reference.

Source

pub fn branch_mut(&mut self) -> &mut B

Returns a mutable reference to the branch writer.

§Returns

The branch writer reference.

Source

pub fn into_inner(self) -> (P, B)

Consumes this wrapper and returns both wrapped writers.

§Returns

A tuple containing the primary writer and branch writer.

Trait Implementations§

Source§

impl<P, B> Seek for TeeWriter<P, B>
where P: Seek, B: Seek,

Source§

fn seek(&mut self, position: SeekFrom) -> Result<u64>

Seeks both wrapped writers to the same resulting absolute position.

§Parameters
  • position: Target position for the primary writer.
§Returns

The new primary writer position.

§Errors

Returns the primary seek error, or the branch seek error after the primary writer has already moved.

1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more
Source§

impl<P, B> Write for TeeWriter<P, B>
where P: Write, B: Write,

Source§

fn write(&mut self, buffer: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<P, B> Freeze for TeeWriter<P, B>
where P: Freeze, B: Freeze,

§

impl<P, B> RefUnwindSafe for TeeWriter<P, B>

§

impl<P, B> Send for TeeWriter<P, B>
where P: Send, B: Send,

§

impl<P, B> Sync for TeeWriter<P, B>
where P: Sync, B: Sync,

§

impl<P, B> Unpin for TeeWriter<P, B>
where P: Unpin, B: Unpin,

§

impl<P, B> UnsafeUnpin for TeeWriter<P, B>
where P: UnsafeUnpin, B: UnsafeUnpin,

§

impl<P, B> UnwindSafe for TeeWriter<P, B>
where P: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> SeekExt for T
where T: Seek + ?Sized,

Source§

fn stream_size(&mut self) -> Result<u64, Error>

Gets the stream size without changing the final stream position. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WriteExt for T
where T: Write + ?Sized,

Source§

unsafe fn write_unchecked( &mut self, buffer: &[u8], start_index: usize, count: usize, ) -> Result<usize>

Writes bytes from a range of buffer without checking the range bounds in release builds. Read more
Source§

unsafe fn write_all_unchecked( &mut self, buffer: &[u8], start_index: usize, count: usize, ) -> Result<()>

Writes exactly count bytes from a range of buffer without checking the range bounds in release builds. Read more
Source§

impl<T> WriteSeek for T
where T: Write + Seek + ?Sized,

Source§

impl<T> WriteSeekExt for T
where T: Write + Seek + ?Sized,

Source§

fn write_all_at_preserving_position( &mut self, offset: u64, buffer: &[u8], ) -> Result<(), Error>

Writes all bytes at offset and restores the original position. Read more