[][src]Struct byteio::Writer

pub struct Writer<W: WriteBytes>(_, _);

A convenience structure used for counting the number of bytes written.

This structure wraps an implementation of WriteBytes. It forwards write operations to the inner type while also maintaining a count of the number of bytes that pass through it.

When you have finished with it, you can return the original type via the into_inner method.

When compiled with the std feature this structure implements Write.

Examples

use byteio::{WriteBytes, WriteBytesExt, Writer};

fn main() -> byteio::Result<()> {
    let mut buf = [0; 7];

    {
        let mut writer = Writer::new(&mut buf[..]);

        writer.try_write_u8(1)?;
        writer.try_write_u16_be(2)?;
        writer.try_write_u32_be(3)?;

        assert_eq!(writer.num_bytes_written(), 7);

        let inner = writer.into_inner();
        assert!(inner.is_empty());
    }

    assert_eq!(buf, [1, 0, 2, 0, 0, 0, 3]);

    Ok(())
}

Methods

impl<W: WriteBytes> Writer<W>[src]

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

Creates a new Writer by wrapping a WriteBytes implementor.

Examples

use byteio::Writer;

let mut buf = [0_u8; 2];
let mut writer = Writer::new(&mut buf[..]);

pub fn num_bytes_written(&self) -> usize[src]

Retrieves the number of bytes that have been written by this Writer.

Examples

use byteio::{WriteBytes, Writer};

let mut buf = [0_u8; 2];
let mut writer = Writer::new(&mut buf[..]);
writer.write_exact(&[1]);
writer.write_exact(&[1]);

assert_eq!(writer.num_bytes_written(), 2);

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

Consumes this Writer and returns the original WriteBytes implementor.

use byteio::{WriteBytes, Writer};

let mut buf = [0_u8; 2];
let mut writer = Writer::new(&mut buf[..]);
writer.write_exact(&[1]);
let inner = writer.into_inner();

assert_eq!(inner.len(), 1); // the writer consumed one byte from its view of the slice

Trait Implementations

impl<W: WriteBytes> WriteBytes for Writer<W>[src]

impl<W: PartialEq + WriteBytes> PartialEq<Writer<W>> for Writer<W>[src]

impl<W: Eq + WriteBytes> Eq for Writer<W>[src]

impl<W: Hash + WriteBytes> Hash for Writer<W>[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<W: Debug + WriteBytes> Debug for Writer<W>[src]

impl<W: WriteBytes> AsMut<[u8]> for Writer<W>[src]

impl<W: Clone + WriteBytes> Clone for Writer<W>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<W: WriteBytes> Write for Writer<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

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> Send for Writer<W> where
    W: Send

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

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

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

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

Blanket Implementations

impl<W> WriteBytesExt for W where
    W: WriteBytes
[src]

fn write_u8(&mut self, n: u8)[src]

Writes a u8 into the underlying buffer. Read more

fn try_write_u8(&mut self, n: u8) -> Result<()>[src]

Attempts to write a u8 into the underlying buffer. Read more

fn write_i8(&mut self, n: i8)[src]

Writes an i8 into the underlying buffer. Read more

fn try_write_i8(&mut self, n: i8) -> Result<()>[src]

Attempts to write an i8 into the underlying buffer. Read more

fn write_u16_le(&mut self, n: u16)[src]

Writes a u16 into the underlying buffer in little endian byte order. Read more

fn write_u16_be(&mut self, n: u16)[src]

Writes a u16 into the underlying buffer in big endian byte order. Read more

fn try_write_u16_le(&mut self, n: u16) -> Result<()>[src]

Attempts to write a u16 into the underlying buffer in little endian byte order. Read more

fn try_write_u16_be(&mut self, n: u16) -> Result<()>[src]

Attempts to write a u16 into the underlying buffer in big endian byte order. Read more

fn write_i16_le(&mut self, n: i16)[src]

Writes an i16 into the underlying buffer in little endian byte order. Read more

fn write_i16_be(&mut self, n: i16)[src]

Writes an i16 into the underlying buffer in big endian byte order. Read more

fn try_write_i16_le(&mut self, n: i16) -> Result<()>[src]

Attempts to write an i16 into the underlying buffer in little endian byte order. Read more

fn try_write_i16_be(&mut self, n: i16) -> Result<()>[src]

Attempts to write an i16 into the underlying buffer in big endian byte order. Read more

fn write_u32_le(&mut self, n: u32)[src]

Writes a u32 into the underlying buffer in little endian byte order. Read more

fn write_u32_be(&mut self, n: u32)[src]

Writes a u32 into the underlying buffer in big endian byte order. Read more

fn try_write_u32_le(&mut self, n: u32) -> Result<()>[src]

Attempts to write a u32 into the underlying buffer in little endian byte order. Read more

fn try_write_u32_be(&mut self, n: u32) -> Result<()>[src]

Attempts to write a u32 into the underlying buffer in big endian byte order. Read more

fn write_i32_le(&mut self, n: i32)[src]

Writes an i32 into the underlying buffer in little endian byte order. Read more

fn write_i32_be(&mut self, n: i32)[src]

Writes an i32 into the underlying buffer in big endian byte order. Read more

fn try_write_i32_le(&mut self, n: i32) -> Result<()>[src]

Attempts to write an i32 into the underlying buffer in little endian byte order. Read more

fn try_write_i32_be(&mut self, n: i32) -> Result<()>[src]

Attempts to write an i32 into the underlying buffer in big endian byte order. Read more

fn write_u64_le(&mut self, n: u64)[src]

Writes a u64 into the underlying buffer in little endian byte order. Read more

fn write_u64_be(&mut self, n: u64)[src]

Writes a u64 into the underlying buffer in big endian byte order. Read more

fn try_write_u64_le(&mut self, n: u64) -> Result<()>[src]

Attempts to write a u64 into the underlying buffer in little endian byte order. Read more

fn try_write_u64_be(&mut self, n: u64) -> Result<()>[src]

Attempts to write a u64 into the underlying buffer in big endian byte order. Read more

fn write_i64_le(&mut self, n: i64)[src]

Writes an i64 into the underlying buffer in little endian byte order. Read more

fn write_i64_be(&mut self, n: i64)[src]

Writes an i64 into the underlying buffer in big endian byte order. Read more

fn try_write_i64_le(&mut self, n: i64) -> Result<()>[src]

Attempts to write an i64 into the underlying buffer in little endian byte order. Read more

fn try_write_i64_be(&mut self, n: i64) -> Result<()>[src]

Attempts to write an i64 into the underlying buffer in big endian byte order. Read more

fn write_u128_le(&mut self, n: u128)[src]

Writes a u128 into the underlying buffer in little endian byte order. Read more

fn write_u128_be(&mut self, n: u128)[src]

Writes a u128 into the underlying buffer in big endian byte order. Read more

fn try_write_u128_le(&mut self, n: u128) -> Result<()>[src]

Attempts to write a u128 into the underlying buffer in little endian byte order. Read more

fn try_write_u128_be(&mut self, n: u128) -> Result<()>[src]

Attempts to write a u128 into the underlying buffer in big endian byte order. Read more

fn write_i128_le(&mut self, n: i128)[src]

Writes an i128 into the underlying buffer in little endian byte order. Read more

fn write_i128_be(&mut self, n: i128)[src]

Writes an i128 into the underlying buffer in big endian byte order. Read more

fn try_write_i128_le(&mut self, n: i128) -> Result<()>[src]

Attempts to write an i128 into the underlying buffer in little endian byte order. Read more

fn try_write_i128_be(&mut self, n: i128) -> Result<()>[src]

Attempts to write an i128 into the underlying buffer in big endian byte order. Read more

fn write_f32_le(&mut self, n: f32)[src]

Writes an IEEE754 f32 into the underlying buffer in little endian byte order. Read more

fn write_f32_be(&mut self, n: f32)[src]

Writes an IEEE754 f32 into the underlying buffer in big endian byte order. Read more

fn try_write_f32_le(&mut self, n: f32) -> Result<()>[src]

Attempts to write an IEEE754 f32 into the underlying buffer. Read more

fn try_write_f32_be(&mut self, n: f32) -> Result<()>[src]

Attempts to write an IEEE754 f32 into the underlying buffer in big endian byte order. Read more

fn write_f64_le(&mut self, n: f64)[src]

Writes an IEEE754 f64 into the underlying buffer in little endian byte order. Read more

fn write_f64_be(&mut self, n: f64)[src]

Writes an IEEE754 f64 into the underlying buffer in big endian byte order. Read more

fn try_write_f64_le(&mut self, n: f64) -> Result<()>[src]

Attempts to write an IEEE754 f64 into the underlying buffer. Read more

fn try_write_f64_be(&mut self, n: f64) -> Result<()>[src]

Attempts to write an IEEE754 f64 into the underlying buffer in big endian byte order. Read more

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> Into<U> for T where
    U: From<T>, 
[src]

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

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> Borrow<T> for T where
    T: ?Sized
[src]

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

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.