Trait embedded_io::Write

source ·
pub trait Write: ErrorType {
    // Required methods
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
    fn flush(&mut self) -> Result<(), Self::Error>;

    // Provided methods
    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> { ... }
    fn write_fmt(
        &mut self,
        fmt: Arguments<'_>
    ) -> Result<(), WriteFmtError<Self::Error>> { ... }
}
Expand description

Blocking writer.

This trait is the embedded-io equivalent of [std::io::Write].

Required Methods§

source

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written.

If the writer is not currently ready to accept more bytes (for example, its buffer is full), this function blocks until it is ready to accept least one byte.

If it’s ready to accept bytes, a non-zero amount of bytes is written from the beginning of buf, and the amount is returned. It is not guaranteed that all available buffer space is filled, i.e. it is possible for the implementation to write an amount of bytes less than buf.len() while the writer continues to be ready to accept more bytes immediately.

Implementations must not return Ok(0) unless buf is empty. Situations where the writer is not able to accept more bytes must instead be indicated with an error, where the ErrorKind is WriteZero.

If buf is empty, write returns without blocking, with either Ok(0) or an error. Ok(0) doesn’t indicate an error.

source

fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, blocking until all intermediately buffered contents reach their destination.

Provided Methods§

source

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer.

This function calls write() in a loop until exactly buf.len() bytes have been written, blocking if needed.

If you are using WriteReady to avoid blocking, you should not use this function. WriteReady::write_ready() returning true only guarantees the first call to write() will not block, so this function may still block in subsequent calls.

This function will panic if write() returns Ok(0).

source

fn write_fmt( &mut self, fmt: Arguments<'_> ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered.

This function calls write() in a loop until the entire formatted string has been written, blocking if needed.

If you are using WriteReady to avoid blocking, you should not use this function. WriteReady::write_ready() returning true only guarantees the first call to write() will not block, so this function may still block in subsequent calls.

Unlike Write::write, the number of bytes written is not returned. However, in the case of writing to an &mut [u8] its possible to calculate the number of bytes written by subtracting the length of the slice after the write, from the initial length of the slice.

let mut buf: &mut [u8] = &mut [0u8; 256];
let start = buf.len();
let len = write!(buf, "{}", "Test").and_then(|_| Ok(start - buf.len()));

Implementations on Foreign Types§

source§

impl Write for &mut [u8]

Write is implemented for &mut [u8] by copying into the slice, overwriting its data.

Note that writing updates the slice to point to the yet unwritten part. The slice will be empty when it has been completely overwritten.

If the number of bytes to be written exceeds the size of the slice, write operations will return short writes: ultimately, a SliceWriteError::Full.

source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

source§

fn flush(&mut self) -> Result<(), Self::Error>

source§

impl<T: ?Sized + Write> Write for &mut T

source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

source§

fn flush(&mut self) -> Result<(), Self::Error>

Implementors§