[−][src]Trait byteio::WriteBytes
Write a slice of bytes into a buffer.
Writers can be thought of as mutable cursors that only allow you to seek
forward from the current position. They can be implemented with one method;
write_exact
. This forcibly attempts to writer the bytes
to the underlying buffer, and advance the buffer forward such that the next
call to write_exact
will commit the bytes directly after
those previously written.
WriteBytes
uses AsMut<[u8]>
as a supertrait. This means
that a generic writer can obtain its current length or mutably peek at the
underlying bytes without advancing the cursor through write operations.
Examples
Writing into a slice:
use byteio::WriteBytes; fn main() -> byteio::Result<()> { let mut buf = [0; 8]; { let mut buf = &mut buf[..]; buf.try_write_exact(&[1, 2])?; assert_eq!(buf.len(), 6); buf.try_write_exact(&[3, 4, 5, 6, 7])?; assert_eq!(buf.len(), 1); buf.try_write_exact(&[8])?; assert!(buf.is_empty()); } assert_eq!(buf, [1, 2, 3, 4, 5, 6, 7, 8]); Ok(()) }
Required methods
fn write_exact(&mut self, buf: &[u8])
Forcibly attempts to write the exact slice into the buffer.
Panics
Panics if there are not enough bytes in self
.
Provided methods
fn try_write_exact(&mut self, buf: &[u8]) -> Result<()>
Attempts to write the exact slice into the buffer.
If there are not enough bytes in self
this function will return
Error::EndOfStream
.