Skip to main content

MultipartWriteExt

Trait MultipartWriteExt 

Source
pub trait MultipartWriteExt<Part>: MultipartWrite<Part> {
Show 24 methods // Provided methods fn boxed<'a>( self, ) -> BoxMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error> where Self: Sized + Send + 'a { ... } fn box_fused<'a>( self, ) -> BoxFusedMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error> where Self: Sized + Send + FusedMultipartWrite<Part> + 'a { ... } fn box_fused_local<'a>( self, ) -> LocalBoxFusedMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error> where Self: Sized + FusedMultipartWrite<Part> + 'a { ... } fn boxed_local<'a>( self, ) -> LocalBoxMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error> where Self: Sized + 'a { ... } fn buffered( self, capacity: impl Into<Option<usize>>, ) -> Buffered<Self, Part> where Self: Sized { ... } fn complete(&mut self) -> Complete<'_, Self, Part> where Self: Unpin { ... } fn fanout<U>(self, other: U) -> Fanout<Self, U, Part> where Part: Clone, U: MultipartWrite<Part, Error = Self::Error>, Self: Sized { ... } fn feed(&mut self, part: Part) -> Feed<'_, Self, Part> where Self: Unpin { ... } fn filter_part<F>(self, f: F) -> FilterPart<Self, Part, F> where F: FnMut(&Part) -> bool, Self: Sized { ... } fn filter_map_part<P, F>(self, f: F) -> FilterMapPart<Self, Part, P, F> where F: FnMut(P) -> Option<Part>, Self: Sized { ... } fn flush(&mut self) -> Flush<'_, Self, Part> where Self: Unpin { ... } fn fold_sent<T, F>(self, id: T, f: F) -> FoldSent<Self, T, F, Part> where F: FnMut(T, &Self::Recv) -> T, Self: Sized { ... } fn for_each_recv<Fut, F>(self, f: F) -> ForEachRecv<Self, Part, Fut, F> where Self: Sized, Self::Recv: Clone, F: FnMut(Self::Recv) -> Fut, Fut: Future<Output = ()> { ... } fn fuse<F>(self, f: F) -> Fuse<Self, Part, F> where F: FnMut(&Self::Output) -> bool, Self: Sized { ... } fn lift<U, P>(self, other: U) -> Lift<Self, U, P, Part> where Self: Sized, Self::Error: From<U::Error>, U: MultipartWrite<P, Output = Part> { ... } fn map_sent<R, F>(self, f: F) -> MapSent<Self, Part, R, F> where F: FnMut(Self::Recv) -> R, Self: Sized { ... } fn map_err<E, F>(self, f: F) -> MapErr<Self, Part, E, F> where F: FnMut(Self::Error) -> E, Self: Sized { ... } fn map_ok<T, F>(self, f: F) -> MapOk<Self, Part, T, F> where F: FnMut(Self::Output) -> T, Self: Sized { ... } fn poll_ready_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>> where Self: Unpin { ... } fn poll_flush_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>> where Self: Unpin { ... } fn poll_complete_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<Self::Output, Self::Error>> where Self: Unpin { ... } fn ready_part<P, Fut, F>(self, f: F) -> ReadyPart<Self, Part, P, Fut, F> where F: FnMut(P) -> Fut, Fut: Future<Output = Result<Part, Self::Error>>, Self: Sized { ... } fn send_flush(&mut self, part: Part) -> SendFlush<'_, Self, Part> where Self: Unpin { ... } fn then<T, Fut, F>(self, f: F) -> Then<Self, Part, T, Fut, F> where F: FnMut(Result<Self::Output, Self::Error>) -> Fut, Fut: Future<Output = Result<T, Self::Error>>, Self: Sized { ... }
}
Expand description

An extension trait for MultipartWrite providing a variety of convenient combinator functions.

Provided Methods§

Source

fn boxed<'a>( self, ) -> BoxMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error>
where Self: Sized + Send + 'a,

Wrap this writer in a Box, pinning it.

Source

fn box_fused<'a>( self, ) -> BoxFusedMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error>
where Self: Sized + Send + FusedMultipartWrite<Part> + 'a,

Wrap this writer, which additionally has conditions making it a FusedMultipartWrite, in a Box, pinning it.

Source

fn box_fused_local<'a>( self, ) -> LocalBoxFusedMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error>
where Self: Sized + FusedMultipartWrite<Part> + 'a,

Wrap this writer, which additionally has conditions making it a FusedMultipartWrite, in a Box, pinning it.

Similar to box_fused but without the Send requirement.

Source

fn boxed_local<'a>( self, ) -> LocalBoxMultipartWrite<'a, Part, Self::Recv, Self::Output, Self::Error>
where Self: Sized + 'a,

Wrap this writer in a Box, pinning it.

Similar to boxed but without the Send requirement.

Source

fn buffered(self, capacity: impl Into<Option<usize>>) -> Buffered<Self, Part>
where Self: Sized,

Adds a fixed size buffer to the current writer.

The resulting MultipartWrite will buffer up to capacity items when the underlying writer is not able to accept new parts.

The values returned when the underlying writer has received a part are also accumulated and returned in batch.

Source

fn complete(&mut self) -> Complete<'_, Self, Part>
where Self: Unpin,

A future that runs this writer to completion, returning the associated output.

Source

fn fanout<U>(self, other: U) -> Fanout<Self, U, Part>
where Part: Clone, U: MultipartWrite<Part, Error = Self::Error>, Self: Sized,

Fanout the part to multiple writers.

This adapter clones each incoming part and forwards it to both writers.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let wr1 = write::extend(init.clone());
let wr2 = write::extend(init);

let mut writer = wr1.fanout(wr2);
writer.send_flush(1).await.unwrap();
writer.send_flush(2).await.unwrap();
writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, (vec![1, 2, 3], vec![1, 2, 3]));
Source

fn feed(&mut self, part: Part) -> Feed<'_, Self, Part>
where Self: Unpin,

A future that completes after the given part has been received by the writer.

Unlike send_flush, the returned future does not flush the writer. It is the caller’s responsibility to ensure all pending items are processed by calling flush or complete.

Source

fn filter_part<F>(self, f: F) -> FilterPart<Self, Part, F>
where F: FnMut(&Part) -> bool, Self: Sized,

Apply a filter to this writer’s parts, returning a new writer with the same output.

The return type of this writer is Option<Self::Recv> and is None when the part did not pass the filter.

§Examples
use multipart_write::{MultipartWriteExt, write};

let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init).filter_part(|n| n % 2 == 0);

let r1 = writer.send_flush(1).await.unwrap();
let r2 = writer.send_flush(2).await.unwrap();
let r3 = writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert!(r1.is_none() && r2.is_some() && r3.is_none());
assert_eq!(out, vec![2]);
Source

fn filter_map_part<P, F>(self, f: F) -> FilterMapPart<Self, Part, P, F>
where F: FnMut(P) -> Option<Part>, Self: Sized,

Attempt to map the input to a part for this writer, filtering out the inputs where the mapping returns None.

The return type of this writer is Option<Self::Recv> and is None when the provided closure returns None.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<String> = Vec::new();
let mut writer = write::extend(init).filter_map_part(|n: u8| {
    if n % 2 == 0 { Some(n.to_string()) } else { None }
});

let r1 = writer.send_flush(1).await.unwrap();
let r2 = writer.send_flush(2).await.unwrap();
let r3 = writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert!(r1.is_none() && r2.is_some() && r3.is_none());
assert_eq!(out, vec!["2".to_string()]);
Source

fn flush(&mut self) -> Flush<'_, Self, Part>
where Self: Unpin,

A future that completes when the underlying writer has been flushed.

Source

fn fold_sent<T, F>(self, id: T, f: F) -> FoldSent<Self, T, F, Part>
where F: FnMut(T, &Self::Recv) -> T, Self: Sized,

Accumulate the values returned by starting a send, returning it with the output.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init).fold_sent(0, |n, _| n + 1);

let r1 = writer.send_flush(1).await.unwrap();
let r2 = writer.send_flush(2).await.unwrap();
let r3 = writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, (3, vec![1, 2, 3]));
Source

fn for_each_recv<Fut, F>(self, f: F) -> ForEachRecv<Self, Part, Fut, F>
where Self: Sized, Self::Recv: Clone, F: FnMut(Self::Recv) -> Fut, Fut: Future<Output = ()>,

Evaluate the given async closure on the associated Self::Recv for this writer.

The result is a new writer that has all of the same properties as this writer, except that poll_ready will not accept the next part until the future returned by evaluating F on the return value resolves.

§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};

use multipart_write::{MultipartWriteExt as _, write};

let counter = Arc::new(AtomicU8::new(1));

// `extend` has no return type, so `map_sent` makes one for the
// demonstration.
let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init)
    .map_sent(|_| {
        let cnt = Arc::clone(&counter);
        let n = cnt.fetch_add(1, Ordering::SeqCst);
        n
    })
    .for_each_recv(|n| {
        println!("{n} parts written");
        futures::future::ready(())
    });

let r1 = writer.send_flush(1).await.unwrap();
let r2 = writer.send_flush(2).await.unwrap();
let r3 = writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, vec![1, 2, 3]);
Source

fn fuse<F>(self, f: F) -> Fuse<Self, Part, F>
where F: FnMut(&Self::Output) -> bool, Self: Sized,

Returns a new writer that fuses according to the provided closure.

The resulting writer wraps both Self::Recv and Self::Output in an Option and is guaranteed to both output and return Ok(None) when called after becoming fused.

Source

fn lift<U, P>(self, other: U) -> Lift<Self, U, P, Part>
where Self: Sized, Self::Error: From<U::Error>, U: MultipartWrite<P, Output = Part>,

Produce the parts for this writer from the output of another writer.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let wr = write::extend(init.clone()).map_ok(|vs| vs.iter().sum::<u8>());
let mut writer = write::extend(init).lift(wr);

// We use `feed` and not `send_flush` because `send_flush` will complete
// the outer writer and write its output to the inner writer after each
// send, which is not what we want the example to show.
writer.feed(1).await.unwrap();
writer.feed(2).await.unwrap();

// Flush the writer manually, which now completes the outer writer and
// writes its output, the sum of the parts written, to the inner writer.
writer.flush().await.unwrap();

writer.feed(3).await.unwrap();
writer.feed(4).await.unwrap();
writer.feed(5).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, vec![3, 12]);
Source

fn map_sent<R, F>(self, f: F) -> MapSent<Self, Part, R, F>
where F: FnMut(Self::Recv) -> R, Self: Sized,

Map this writer’s return type to a different value, returning a new multipart writer with the given return type.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init).map_sent(|_| "OK");

let r1 = writer.send_flush(1).await.unwrap();
let r2 = writer.send_flush(2).await.unwrap();
let r3 = writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(vec![r1, r2, r3], vec!["OK", "OK", "OK"]);
assert_eq!(out, vec![1, 2, 3]);
Source

fn map_err<E, F>(self, f: F) -> MapErr<Self, Part, E, F>
where F: FnMut(Self::Error) -> E, Self: Sized,

Map this writer’s error type to a different value, returning a new multipart writer with the given error type.

Source

fn map_ok<T, F>(self, f: F) -> MapOk<Self, Part, T, F>
where F: FnMut(Self::Output) -> T, Self: Sized,

Map this writer’s output type to a different type, returning a new multipart writer with the given output type.

Source

fn poll_ready_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
where Self: Unpin,

A convenience method for calling MultipartWrite::poll_ready on Unpin writer types.

Source

fn poll_flush_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
where Self: Unpin,

A convenience method for calling MultipartWrite::poll_flush on Unpin writer types.

Source

fn poll_complete_unpin( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<Self::Output, Self::Error>>
where Self: Unpin,

A convenience method for calling MultipartWrite::poll_complete on Unpin writer types.

Source

fn ready_part<P, Fut, F>(self, f: F) -> ReadyPart<Self, Part, P, Fut, F>
where F: FnMut(P) -> Fut, Fut: Future<Output = Result<Part, Self::Error>>, Self: Sized,

Provide a part to this writer in the output of a future.

The result is a new writer over the type U that passes each value through the function f, resolving the output, and sending it to the inner writer.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init)
    .ready_part(|n: u8| futures::future::ready(Ok(n + 1)));

writer.send_flush(1).await.unwrap();
writer.send_flush(2).await.unwrap();
writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, vec![2, 3, 4]);
Source

fn send_flush(&mut self, part: Part) -> SendFlush<'_, Self, Part>
where Self: Unpin,

A future that completes when a part has been fully processed into the writer, including flushing.

Source

fn then<T, Fut, F>(self, f: F) -> Then<Self, Part, T, Fut, F>
where F: FnMut(Result<Self::Output, Self::Error>) -> Fut, Fut: Future<Output = Result<T, Self::Error>>, Self: Sized,

Asynchronously map the result of completing this writer to a different result.

§Examples
use multipart_write::{MultipartWriteExt as _, write};

let init: Vec<u8> = Vec::new();
let mut writer = write::extend(init).then(|res| {
    futures::future::ready(res.map(|vs| vs.iter().sum::<u8>()))
});

writer.send_flush(1).await.unwrap();
writer.send_flush(2).await.unwrap();
writer.send_flush(3).await.unwrap();
let out = writer.complete().await.unwrap();

assert_eq!(out, 6);

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<Wr: MultipartWrite<Part>, Part> MultipartWriteExt<Part> for Wr