[−][src]Struct byteio::Writer
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.
Examples
use byteio::{WriteBytes, WriteBytesExt, Writer}; use byteorder::BigEndian; 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::<BigEndian>(2)?; writer.try_write_u32::<BigEndian>(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]
fn write_exact(&mut self, buf: &[u8])
[src]
fn try_write_exact(&mut self, buf: &[u8]) -> Result<()>
[src]
impl<W: Debug + WriteBytes> Debug 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<__HW: Hasher>(&self, state: &mut __HW)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<W: WriteBytes> AsMut<[u8]> for Writer<W>
[src]
impl<W: Clone + WriteBytes> Clone for Writer<W>
[src]
Auto Trait Implementations
Blanket Implementations
impl<W> WriteBytesExt for W where
W: WriteBytes,
[src]
W: WriteBytes,
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<E: ByteOrder>(&mut self, n: u16)
[src]
Writes a u16
into the underlying buffer. Read more
fn try_write_u16<E: ByteOrder>(&mut self, n: u16) -> Result<()>
[src]
Attempts to write a u16
into the underlying buffer. Read more
fn write_i16<E: ByteOrder>(&mut self, n: i16)
[src]
Writes an i16
into the underlying buffer. Read more
fn try_write_i16<E: ByteOrder>(&mut self, n: i16) -> Result<()>
[src]
Attempts to write an i16
into the underlying buffer. Read more
fn write_u32<E: ByteOrder>(&mut self, n: u32)
[src]
Writes a u32
into the underlying buffer. Read more
fn try_write_u32<E: ByteOrder>(&mut self, n: u32) -> Result<()>
[src]
Attempts to write a u32
into the underlying buffer. Read more
fn write_i32<E: ByteOrder>(&mut self, n: i32)
[src]
Writes an i32
into the underlying buffer. Read more
fn try_write_i32<E: ByteOrder>(&mut self, n: i32) -> Result<()>
[src]
Attempts to write an i32
into the underlying buffer. Read more
fn write_u64<E: ByteOrder>(&mut self, n: u64)
[src]
Writes a u64
into the underlying buffer. Read more
fn try_write_u64<E: ByteOrder>(&mut self, n: u64) -> Result<()>
[src]
Attempts to write a u64
into the underlying buffer. Read more
fn write_i64<E: ByteOrder>(&mut self, n: i64)
[src]
Writes an i64
into the underlying buffer. Read more
fn try_write_i64<E: ByteOrder>(&mut self, n: i64) -> Result<()>
[src]
Attempts to write an i64
into the underlying buffer. Read more
fn write_u128<E: ByteOrder>(&mut self, n: u128)
[src]
Writes a u128
into the underlying buffer. Read more
fn try_write_u128<E: ByteOrder>(&mut self, n: u128) -> Result<()>
[src]
Attempts to write a u128
into the underlying buffer. Read more
fn write_i128<E: ByteOrder>(&mut self, n: i128)
[src]
Writes an i128
into the underlying buffer. Read more
fn try_write_i128<E: ByteOrder>(&mut self, n: i128) -> Result<()>
[src]
Attempts to write an i128
into the underlying buffer. Read more
fn write_f32<E: ByteOrder>(&mut self, n: f32)
[src]
Writes an IEEE754 f32
into the underlying buffer. Read more
fn try_write_f32<E: ByteOrder>(&mut self, n: f32) -> Result<()>
[src]
Attempts to write an IEEE754 f32
into the underlying buffer. Read more
fn write_f64<E: ByteOrder>(&mut self, n: f64)
[src]
Writes an IEEE754 f64
into the underlying buffer. Read more
fn try_write_f64<E: ByteOrder>(&mut self, n: f64) -> Result<()>
[src]
Attempts to write an IEEE754 f64
into the underlying buffer. Read more
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,