pub trait SinkExt: Sink {
// Provided methods
fn with<U, Fut, F>(self, f: F) -> With<Self, U, Fut, F>
where F: FnMut(U) -> Fut,
Fut: IntoFuture<Item = Self::SinkItem>,
<Fut as IntoFuture>::Error: From<Self::SinkError>,
Self: Sized { ... }
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, U, St, F>
where F: FnMut(U) -> St,
St: Stream<Item = Self::SinkItem, Error = Self::SinkError>,
Self: Sized { ... }
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
where F: FnOnce(Self::SinkError) -> E,
Self: Sized { ... }
fn sink_err_into<E>(self) -> SinkErrInto<Self, E>
where Self: Sized,
Self::SinkError: Into<E> { ... }
fn buffer(self, amt: usize) -> Buffer<Self>
where Self: Sized { ... }
fn close(self) -> Close<Self>
where Self: Sized { ... }
fn fanout<S>(self, other: S) -> Fanout<Self, S>
where Self: Sized,
Self::SinkItem: Clone,
S: Sink<SinkItem = Self::SinkItem, SinkError = Self::SinkError> { ... }
fn flush(self) -> Flush<Self>
where Self: Sized { ... }
fn send(self, item: Self::SinkItem) -> Send<Self>
where Self: Sized { ... }
fn send_all<S>(self, stream: S) -> SendAll<Self, S>
where S: Stream<Item = Self::SinkItem>,
Self::SinkError: From<<S as Stream>::Error>,
Self: Sized { ... }
fn left_sink<B>(self) -> Either<Self, B> ⓘ
where B: Sink<SinkItem = Self::SinkItem, SinkError = Self::SinkError>,
Self: Sized { ... }
fn right_sink<B>(self) -> Either<B, Self> ⓘ
where B: Sink<SinkItem = Self::SinkItem, SinkError = Self::SinkError>,
Self: Sized { ... }
}
Expand description
An extension trait for Sink
s that provides a variety of convenient
combinator functions.
Provided Methods§
Sourcefn with<U, Fut, F>(self, f: F) -> With<Self, U, Fut, F>where
F: FnMut(U) -> Fut,
Fut: IntoFuture<Item = Self::SinkItem>,
<Fut as IntoFuture>::Error: From<Self::SinkError>,
Self: Sized,
fn with<U, Fut, F>(self, f: F) -> With<Self, U, Fut, F>where
F: FnMut(U) -> Fut,
Fut: IntoFuture<Item = Self::SinkItem>,
<Fut as IntoFuture>::Error: From<Self::SinkError>,
Self: Sized,
Composes a function in front of the sink.
This adapter produces a new sink that passes each value through the
given function f
before sending it to self
.
To process each value, f
produces a future, which is then polled to
completion before passing its result down to the underlying sink. If the
future produces an error, that error is returned by the new sink.
Note that this function consumes the given sink, returning a wrapped
version, much like Iterator::map
.
Sourcefn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, U, St, F>
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, U, St, F>
Composes a function in front of the sink.
This adapter produces a new sink that passes each value through the
given function f
before sending it to self
.
To process each value, f
produces a stream, of which each value
is passed to the underlying sink. A new value will not be accepted until
the stream has been drained
Note that this function consumes the given sink, returning a wrapped
version, much like Iterator::flat_map
.
§Examples
Using this function with an iterator through use of the stream::iter_ok()
function
use futures::prelude::*;
use futures::stream;
use futures_channel::mpsc;
use futures_executor::block_on;
let (tx, rx) = mpsc::channel::<i32>(5);
let tx = tx.with_flat_map(|x| {
stream::iter_ok(vec![42; x].into_iter().map(|y| y))
});
block_on(tx.send(5)).unwrap();
assert_eq!(block_on(rx.collect()), Ok(vec![42, 42, 42, 42, 42]));
Sourcefn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
Transforms the error returned by the sink.
Sourcefn sink_err_into<E>(self) -> SinkErrInto<Self, E>
fn sink_err_into<E>(self) -> SinkErrInto<Self, E>
Map this sink’s error to a different error type using the Into
trait.
If wanting to map errors of a Sink + Stream
, use .sink_err_into().err_into()
.
Sourcefn buffer(self, amt: usize) -> Buffer<Self>where
Self: Sized,
fn buffer(self, amt: usize) -> Buffer<Self>where
Self: Sized,
Adds a fixed-size buffer to the current sink.
The resulting sink will buffer up to amt
items when the underlying
sink is unwilling to accept additional items. Calling flush
on
the buffered sink will attempt to both empty the buffer and complete
processing on the underlying sink.
Note that this function consumes the given sink, returning a wrapped
version, much like Iterator::map
.
This method is only available when the std
feature of this
library is activated, and it is activated by default.
Sourcefn close(self) -> Close<Self>where
Self: Sized,
fn close(self) -> Close<Self>where
Self: Sized,
Close the sink.
The sink itself is returned after closeing is complete.
Sourcefn fanout<S>(self, other: S) -> Fanout<Self, S>
fn fanout<S>(self, other: S) -> Fanout<Self, S>
Fanout items to multiple sinks.
This adapter clones each incoming item and forwards it to both this as well as the other sink at the same time.
Sourcefn flush(self) -> Flush<Self>where
Self: Sized,
fn flush(self) -> Flush<Self>where
Self: Sized,
Flush the sync, processing all pending items.
The sink itself is returned after flushing is complete; this adapter is intended to be used when you want to stop sending to the sink until all current requests are processed.
Sourcefn send(self, item: Self::SinkItem) -> Send<Self>where
Self: Sized,
fn send(self, item: Self::SinkItem) -> Send<Self>where
Self: Sized,
A future that completes after the given item has been fully processed into the sink, including flushing.
Note that, because of the flushing requirement, it is usually better
to batch together items to send via send_all
, rather than flushing
between each item.
On completion, the sink is returned.
Sourcefn send_all<S>(self, stream: S) -> SendAll<Self, S>
fn send_all<S>(self, stream: S) -> SendAll<Self, S>
A future that completes after the given stream has been fully processed into the sink, including flushing.
This future will drive the stream to keep producing items until it is exhausted, sending each item to the sink. It will complete once both the stream is exhausted, the sink has received all items, and the sink has been flushed. Note that the sink is not closed.
Doing sink.send_all(stream)
is roughly equivalent to
stream.forward(sink)
. The returned future will exhaust all items from
stream
and send them to self
.
On completion, the pair (sink, source)
is returned.
Sourcefn left_sink<B>(self) -> Either<Self, B> ⓘ
fn left_sink<B>(self) -> Either<Self, B> ⓘ
Wrap this sink in an Either
sink, making it the left-hand variant
of that Either
.
This can be used in combination with the right_sink
method to write if
statements that evaluate to different streams in different branches.
Sourcefn right_sink<B>(self) -> Either<B, Self> ⓘ
fn right_sink<B>(self) -> Either<B, Self> ⓘ
Wrap this stream in an Either
stream, making it the right-hand variant
of that Either
.
This can be used in combination with the left_sink
method to write if
statements that evaluate to different streams in different branches.