[][src]Struct cookie_factory::WriteCounter

pub struct WriteCounter<W>(_, _);

Wrapper around Write that counts how much data was written

This can be used to keep track how much data was actually written by serializing functions, for example

use cookie_factory::{WriteCounter, string};

let mut buf = [0u8; 100];

{
    let mut writer = WriteCounter::new(&mut buf[..]);
    writer = string("abcd")(writer).unwrap();
    assert_eq!(writer.position(), 4);
    let (buf, len) = writer.into_inner();
    assert_eq!(buf.len(), 96);
    assert_eq!(len, 4);
}

assert_eq!(&buf[..4], &b"abcd"[..]);

For byte slices std::io::Cursor provides more features and allows to retrieve the original slice

#[cfg(feature = "std")]
use std::io::Cursor;
#[cfg(not(feature = "std"))]
use cookie_factory::lib::std::io::{Cursor, Write};

use cookie_factory::string;

let mut buf = [0u8; 100];

let mut cursor = Cursor::new(&mut buf[..]);
cursor = string("abcd")(cursor).unwrap();
assert_eq!(cursor.position(), 4);
let buf = cursor.into_inner();

assert_eq!(&buf[..4], &b"abcd"[..]);

Methods

impl<W: Write> WriteCounter<W>[src]

pub fn new(w: W) -> Self[src]

Create a new WriteCounter around w

pub fn position(&self) -> u64[src]

Returns the amount of bytes written so far

pub fn into_inner(self) -> (W, u64)[src]

Consumes the WriteCounter and returns the contained Write and the amount of bytes written

Trait Implementations

impl<W: Write> Write for WriteCounter<W>[src]

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

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

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

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

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>1.0.0[src]

Writes a formatted string into this writer, returning any error encountered. Read more

Important traits for &'_ mut W
fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

impl<W> Sync for WriteCounter<W> where
    W: Sync

impl<W> Unpin for WriteCounter<W> where
    W: Unpin

impl<W> Send for WriteCounter<W> where
    W: Send

impl<W> UnwindSafe for WriteCounter<W> where
    W: UnwindSafe

impl<W> RefUnwindSafe for WriteCounter<W> where
    W: RefUnwindSafe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]