Trait Write

Source
pub trait Write: Stream {
    // Required methods
    fn write(&mut self, buf: &[u8]) -> Result<usize>;
    fn flush(&mut self) -> Result<()>;

    // Provided method
    fn write_all<R>(&mut self, buf: &mut R) -> Result<()>
       where Self: Sized,
             R: BufRead + ?Sized { ... }
}
Expand description

Alternative to std::io::Write

This trait is automatically implemented for all types that implement std::io::Write.

§Differences to std::io::Write

Methods that are just wrappers around the equivalent methods of std::io::Write:

  • write
  • flush

Methods that provide a slightly different functionality than their counterparts in std::io::Write:

  • write_all

Functions that were removed or moved to a different trait, because they cannot be implemented with providing all desired guarantees:

  • write_fmt → removed

Required Methods§

Source

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

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

This method is equivalent to std::io::Write::write.

Source

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.

This method is equivalent to std::io::Write::flush.

Provided Methods§

Source

fn write_all<R>(&mut self, buf: &mut R) -> Result<()>
where Self: Sized, R: BufRead + ?Sized,

Attempts to write an entire buffer into this write.

Similarly to std::io::Write::write_all, this method will continuously call write while there is more data to write. This method will not return until the entire buffer has been successfully written or an error occurs. The first error generated from this method will be returned.

The supplied buffer will be consumed by the writing operation.

§Errors

This function will return an error immediately if any call to write returns any kind of error. Instances of ErrorKind::Interrupted are not handled by this function.

All bytes consumed from the buffer will be written to the the writer and vice versa. It is guaranteed that no data is lost in case of error.

§Differences to std::io::Write::write_all
  • Does not retry on ErrorKind::Interrupted.
  • Takes a BufRead instance instead of just a plain &[u8].
§Advantages
  • Function is interruptable, e.g. to allow graceful shutdown for server applications.
  • In case of error, it is always clear how much data was already written. No data is lost.

Implementations on Foreign Types§

Source§

impl Write for Vec<u8>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for File

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for Cursor<Box<[u8]>>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for Cursor<Vec<u8>>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for Stderr

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for Stdout

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for Sink

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for TcpStream

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for UnixStream

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl Write for ChildStdin

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for &'a File

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for &'a TcpStream

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for &'a UnixStream

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for &'a mut [u8]

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for Cursor<&'a mut [u8]>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for StderrLock<'a>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a> Write for StdoutLock<'a>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<'a, W: Write + ?Sized> Write for &'a mut W

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<W: Write> Write for BufWriter<W>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<W: Write> Write for LineWriter<W>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Source§

impl<W: Write + ?Sized> Write for Box<W>

Source§

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

Source§

fn flush(&mut self) -> Result<()>

Implementors§

Source§

impl<I: Write> Write for Retry<I>

Source§

impl<R: Write> Write for Compat<R>

Source§

impl<T: Write> Write for Limited<T>

Source§

impl<T: Write> Write for Take<T>