[][src]Trait byteio::WriteBytes

pub trait WriteBytes: AsMut<[u8]> {
    fn write_exact(&mut self, buf: &[u8]);

    fn try_write_exact(&mut self, buf: &[u8]) -> Result<()> { ... }
}

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.

Loading content...

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.

Loading content...

Implementations on Foreign Types

impl<'_> WriteBytes for &'_ mut [u8][src]

impl WriteBytes for Vec<u8>[src]

impl<'_, W: WriteBytes> WriteBytes for &'_ mut W[src]

Loading content...

Implementors

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

Loading content...