Trait Write

Source
pub trait Write: MaybeSend {
    // Required methods
    fn write_all<B: IoBuf>(
        &mut self,
        buf: B,
    ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend;
    fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend;
    fn close(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend;
}
Expand description

The core trait for writing data.

It is similar to std::io::Write, but it takes ownership of the buffer, because completion-based IO requires the buffer to be pinned and should be safe to cancellation.

Write represents “sequential write all and overwrite” semantics, which means each buffer will be written to the file sequentially and overwrite the previous file when closed.

Contents are not guaranteed to be written to the file until the Write::close method is called. Write::flush may be used to flush the data to the file in some implementations, but not all implementations will do so.

Whether the operation is successful or not, the buffer will be returned. Fusio promises that the returned buffer will be the same as the input buffer.

§Examples

use fusio_core::{IoBuf, Write};

async fn write_data<W: Write>(
    mut writer: W,
    data: Vec<u8>,
) -> Result<(), fusio_core::error::Error> {
    let (result, _buf) = writer.write_all(data).await;
    result?;
    writer.flush().await?;
    writer.close().await?;
    Ok(())
}

§Dyn Compatibility

This trait is not dyn compatible. If you want to use Write trait in a dynamic way, you could use DynWrite trait.

Required Methods§

Source

fn write_all<B: IoBuf>( &mut self, buf: B, ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend

Source

fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend

Source

fn close(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Write for Box<dyn DynWrite + '_>

Available on crate feature alloc only.
Source§

async fn write_all<B: IoBuf>(&mut self, buf: B) -> (Result<(), Error>, B)

Source§

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

Source§

async fn close(&mut self) -> Result<(), Error>

Source§

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

Source§

async fn write_all<B: IoBuf>(&mut self, buf: B) -> (Result<(), Error>, B)

Source§

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

Source§

async fn close(&mut self) -> Result<(), Error>

Source§

impl<W: Write> Write for &mut W

Source§

fn write_all<B: IoBuf>( &mut self, buf: B, ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend

Source§

fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend

Source§

fn close(&mut self) -> impl Future<Output = Result<(), Error>> + MaybeSend

Implementors§