Trait Write

Source
pub trait Write {
    // Required methods
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize>>;
    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<()>>;
    fn poll_close(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<()>>;

    // Provided methods
    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[IoSlice<'_>],
    ) -> Poll<Result<usize>> { ... }
    fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>>
       where Self: Unpin { ... }
    fn flush(&mut self) -> ImplFuture<Result<()>>
       where Self: Unpin { ... }
    fn write_vectored<'a>(
        &'a mut self,
        bufs: &'a [IoSlice<'a>],
    ) -> ImplFuture<Result<usize>>
       where Self: Unpin { ... }
    fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>>
       where Self: Unpin { ... }
    fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>>
       where Self: Unpin { ... }
}
Expand description

Allows writing to a byte stream.

This trait is a re-export of futures::io::AsyncWrite and is an async version of std::io::Write.

Methods other than poll_write, poll_write_vectored, poll_flush, and poll_close do not really exist in the trait itself, but they become available when WriteExt from the prelude is imported:

use async_std::prelude::*;

Required Methods§

Source

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object.

Source

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination.

Source

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object.

Provided Methods§

Source

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize>>

Attempt to write bytes from bufs into the object using vectored IO operations.

Source

fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>>
where Self: Unpin,

Writes some bytes into the byte stream.

Returns the number of bytes written from the start of the buffer.

If the return value is Ok(n) then it must be guaranteed that 0 <= n <= buf.len(). A return value of 0 typically means that the underlying object is no longer able to accept bytes and will likely not be able to in the future as well, or that the buffer provided is empty.

§Examples
use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;

let n = file.write(b"hello world").await?;
Source

fn flush(&mut self) -> ImplFuture<Result<()>>
where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination.

§Examples
use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;

file.write_all(b"hello world").await?;
file.flush().await?;
Source

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Like write, except that it writes from a slice of buffers.

Data is copied from each buffer in order, with the final buffer read from possibly being only partially consumed. This method must behave as a call to write with the buffers concatenated would.

The default implementation calls write with either the first nonempty buffer provided, or an empty one if none exists.

Source

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>>
where Self: Unpin,

Writes an entire buffer into the byte stream.

This method will continuously call write until there is no more data to be written or an error is returned. This method will not return until the entire buffer has been successfully written or such an error occurs.

§Examples
use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;

file.write_all(b"hello world").await?;

Source

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>>
where Self: Unpin,

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

This method will continuously call write until there is no more data to be written or an error is returned. This future will not resolve until the entire buffer has been successfully written or such an error occurs.

§Examples
use async_std::io::prelude::*;
use async_std::fs::File;

let mut buffer = File::create("foo.txt").await?;

// this call
write!(buffer, "{:.*}", 2, 1.234567).await?;
// turns into this:
buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)).await?;

Implementations on Foreign Types§

Source§

impl Write for Vec<u8>

Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Source§

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

Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Source§

impl<T: Write + Unpin + ?Sized> Write for Box<T>

Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Implementors§

Source§

impl Write for &File

Source§

impl Write for &TcpStream

Source§

impl Write for &UnixStream

Available on Unix only.
Source§

impl Write for File

Source§

impl Write for TcpStream

Source§

impl Write for UnixStream

Available on Unix only.
Source§

impl Write for Cursor<&mut Vec<u8>>

Source§

impl Write for Cursor<&mut [u8]>

Source§

impl Write for Cursor<Vec<u8>>

Source§

impl Write for Sink

Source§

impl Write for Stderr

Source§

impl Write for StderrLock<'_>

Available on unstable only.
Source§

impl Write for Stdout

Source§

impl Write for StdoutLock<'_>

Available on unstable only.
Source§

impl<P> Write for Pin<P>
where P: DerefMut + Unpin, <P as Deref>::Target: Write,

Source§

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