Skip to main content

WriteExt

Trait WriteExt 

Source
pub trait WriteExt: Write + Send {
    // Required method
    fn prepare_and_write<F, E>(
        &mut self,
        min_capacity: usize,
        prepare_fn: F,
    ) -> impl Future<Output = Result<(), Error>> + Send
       where F: FnOnce(BytesBuf) -> Result<BytesView, E> + Send,
             E: Error + Send + Sync + 'static;
}
Expand description

Universal convenience methods built on top of Write.

This trait is implemented for all types that implement Write.

Required Methods§

Source

fn prepare_and_write<F, E>( &mut self, min_capacity: usize, prepare_fn: F, ) -> impl Future<Output = Result<(), Error>> + Send
where F: FnOnce(BytesBuf) -> Result<BytesView, E> + Send, E: Error + Send + Sync + 'static,

Provides a memory buffer to a callback, then writes the contents.

The write is only executed if prepare_fn returns Ok(BytesView) with the data to write.

The expectation is that you perform your serialization logic directly in prepare_fn, instead of just copying bytes from some other buffer (which would not be efficient).

§Example
use std::convert::Infallible;

use bytesbuf_io::WriteExt;

let mut sink = get_sink();

sink.prepare_and_write(100, |mut buf| {
    buf.put_slice(*b"Hello, world!");
    Ok::<_, Infallible>(buf.consume_all())
})
.await
.unwrap();

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.

Implementors§

Source§

impl<T> WriteExt for T
where T: Write,