pub struct Writer<W: WriteBytes>(/* private fields */);
Expand description
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(())
}
Implementations§
Source§impl<W: WriteBytes> Writer<W>
impl<W: WriteBytes> Writer<W>
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
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[..]);
Sourcepub fn num_bytes_written(&self) -> usize
pub fn num_bytes_written(&self) -> usize
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);
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
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§
Source§impl<W: WriteBytes> Write for Writer<W>
impl<W: WriteBytes> Write for Writer<W>
Source§fn write(&mut self, data: &[u8]) -> Result<usize>
fn write(&mut self, data: &[u8]) -> Result<usize>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
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>
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
Source§impl<W: WriteBytes> WriteBytes for Writer<W>
impl<W: WriteBytes> WriteBytes for Writer<W>
impl<W: Eq + WriteBytes> Eq for Writer<W>
impl<W: WriteBytes> StructuralPartialEq for Writer<W>
Auto Trait Implementations§
impl<W> Freeze for Writer<W>where
W: Freeze,
impl<W> RefUnwindSafe for Writer<W>where
W: RefUnwindSafe,
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,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<W> WriteBytesExt for Wwhere
W: WriteBytes,
impl<W> WriteBytesExt for Wwhere
W: WriteBytes,
Source§fn try_write_u8(&mut self, n: u8) -> Result<()>
fn try_write_u8(&mut self, n: u8) -> Result<()>
Attempts to write a
u8
into the underlying buffer. Read moreSource§fn try_write_i8(&mut self, n: i8) -> Result<()>
fn try_write_i8(&mut self, n: i8) -> Result<()>
Attempts to write an
i8
into the underlying buffer. Read moreSource§fn write_u16_le(&mut self, n: u16)
fn write_u16_le(&mut self, n: u16)
Writes a
u16
into the underlying buffer in little endian byte order. Read moreSource§fn write_u16_be(&mut self, n: u16)
fn write_u16_be(&mut self, n: u16)
Writes a
u16
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_u16_le(&mut self, n: u16) -> Result<()>
fn try_write_u16_le(&mut self, n: u16) -> Result<()>
Attempts to write a
u16
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_u16_be(&mut self, n: u16) -> Result<()>
fn try_write_u16_be(&mut self, n: u16) -> Result<()>
Attempts to write a
u16
into the underlying buffer in big endian byte order. Read moreSource§fn write_i16_le(&mut self, n: i16)
fn write_i16_le(&mut self, n: i16)
Writes an
i16
into the underlying buffer in little endian byte order. Read moreSource§fn write_i16_be(&mut self, n: i16)
fn write_i16_be(&mut self, n: i16)
Writes an
i16
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_i16_le(&mut self, n: i16) -> Result<()>
fn try_write_i16_le(&mut self, n: i16) -> Result<()>
Attempts to write an
i16
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_i16_be(&mut self, n: i16) -> Result<()>
fn try_write_i16_be(&mut self, n: i16) -> Result<()>
Attempts to write an
i16
into the underlying buffer in big endian byte order. Read moreSource§fn write_u32_le(&mut self, n: u32)
fn write_u32_le(&mut self, n: u32)
Writes a
u32
into the underlying buffer in little endian byte order. Read moreSource§fn write_u32_be(&mut self, n: u32)
fn write_u32_be(&mut self, n: u32)
Writes a
u32
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_u32_le(&mut self, n: u32) -> Result<()>
fn try_write_u32_le(&mut self, n: u32) -> Result<()>
Attempts to write a
u32
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_u32_be(&mut self, n: u32) -> Result<()>
fn try_write_u32_be(&mut self, n: u32) -> Result<()>
Attempts to write a
u32
into the underlying buffer in big endian byte order. Read moreSource§fn write_i32_le(&mut self, n: i32)
fn write_i32_le(&mut self, n: i32)
Writes an
i32
into the underlying buffer in little endian byte order. Read moreSource§fn write_i32_be(&mut self, n: i32)
fn write_i32_be(&mut self, n: i32)
Writes an
i32
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_i32_le(&mut self, n: i32) -> Result<()>
fn try_write_i32_le(&mut self, n: i32) -> Result<()>
Attempts to write an
i32
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_i32_be(&mut self, n: i32) -> Result<()>
fn try_write_i32_be(&mut self, n: i32) -> Result<()>
Attempts to write an
i32
into the underlying buffer in big endian byte order. Read moreSource§fn write_u64_le(&mut self, n: u64)
fn write_u64_le(&mut self, n: u64)
Writes a
u64
into the underlying buffer in little endian byte order. Read moreSource§fn write_u64_be(&mut self, n: u64)
fn write_u64_be(&mut self, n: u64)
Writes a
u64
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_u64_le(&mut self, n: u64) -> Result<()>
fn try_write_u64_le(&mut self, n: u64) -> Result<()>
Attempts to write a
u64
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_u64_be(&mut self, n: u64) -> Result<()>
fn try_write_u64_be(&mut self, n: u64) -> Result<()>
Attempts to write a
u64
into the underlying buffer in big endian byte order. Read moreSource§fn write_i64_le(&mut self, n: i64)
fn write_i64_le(&mut self, n: i64)
Writes an
i64
into the underlying buffer in little endian byte order. Read moreSource§fn write_i64_be(&mut self, n: i64)
fn write_i64_be(&mut self, n: i64)
Writes an
i64
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_i64_le(&mut self, n: i64) -> Result<()>
fn try_write_i64_le(&mut self, n: i64) -> Result<()>
Attempts to write an
i64
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_i64_be(&mut self, n: i64) -> Result<()>
fn try_write_i64_be(&mut self, n: i64) -> Result<()>
Attempts to write an
i64
into the underlying buffer in big endian byte order. Read moreSource§fn write_u128_le(&mut self, n: u128)
fn write_u128_le(&mut self, n: u128)
Writes a
u128
into the underlying buffer in little endian byte order. Read moreSource§fn write_u128_be(&mut self, n: u128)
fn write_u128_be(&mut self, n: u128)
Writes a
u128
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_u128_le(&mut self, n: u128) -> Result<()>
fn try_write_u128_le(&mut self, n: u128) -> Result<()>
Attempts to write a
u128
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_u128_be(&mut self, n: u128) -> Result<()>
fn try_write_u128_be(&mut self, n: u128) -> Result<()>
Attempts to write a
u128
into the underlying buffer in big endian byte order. Read moreSource§fn write_i128_le(&mut self, n: i128)
fn write_i128_le(&mut self, n: i128)
Writes an
i128
into the underlying buffer in little endian byte order. Read moreSource§fn write_i128_be(&mut self, n: i128)
fn write_i128_be(&mut self, n: i128)
Writes an
i128
into the underlying buffer in big endian byte order. Read moreSource§fn try_write_i128_le(&mut self, n: i128) -> Result<()>
fn try_write_i128_le(&mut self, n: i128) -> Result<()>
Attempts to write an
i128
into the underlying buffer in little endian byte order. Read moreSource§fn try_write_i128_be(&mut self, n: i128) -> Result<()>
fn try_write_i128_be(&mut self, n: i128) -> Result<()>
Attempts to write an
i128
into the underlying buffer in big endian byte order. Read moreSource§fn write_f32_le(&mut self, n: f32)
fn write_f32_le(&mut self, n: f32)
Writes an IEEE754
f32
into the underlying buffer in little endian byte
order. Read moreSource§fn write_f32_be(&mut self, n: f32)
fn write_f32_be(&mut self, n: f32)
Writes an IEEE754
f32
into the underlying buffer in big endian byte
order. Read moreSource§fn try_write_f32_le(&mut self, n: f32) -> Result<()>
fn try_write_f32_le(&mut self, n: f32) -> Result<()>
Attempts to write an IEEE754
f32
into the underlying buffer. Read moreSource§fn try_write_f32_be(&mut self, n: f32) -> Result<()>
fn try_write_f32_be(&mut self, n: f32) -> Result<()>
Attempts to write an IEEE754
f32
into the underlying buffer in big
endian byte order. Read moreSource§fn write_f64_le(&mut self, n: f64)
fn write_f64_le(&mut self, n: f64)
Writes an IEEE754
f64
into the underlying buffer in little endian byte
order. Read moreSource§fn write_f64_be(&mut self, n: f64)
fn write_f64_be(&mut self, n: f64)
Writes an IEEE754
f64
into the underlying buffer in big endian byte
order. Read more