Trait Sink
pub trait Sink<Item> {
type Error;
// Required methods
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>;
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error>;
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>;
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>;
}Expand description
A Sink is a value into which other values can be sent, asynchronously.
Basic examples of sinks include the sending side of:
- Channels
- Sockets
- Pipes
In addition to such “primitive” sinks, it’s typical to layer additional functionality, such as buffering, on top of an existing sink.
Sending to a sink is “asynchronous” in the sense that the value may not be sent in its entirety immediately. Instead, values are sent in a two-phase way: first by initiating a send, and then by polling for completion. This two-phase setup is analogous to buffered writing in synchronous code, where writes often succeed immediately, but internally are buffered and are actually written only upon flushing.
In addition, the Sink may be full, in which case it is not even possible
to start the sending process.
As with Future and Stream, the Sink trait is built from a few core
required methods, and a host of default methods for working in a
higher-level way. The Sink::send_all combinator is of particular
importance: you can use it to send an entire stream to a sink, which is
the simplest way to ultimately consume a stream.
Required Associated Types§
type Error
type Error
The type of value produced by the sink when an error occurs.
Required Methods§
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Attempts to prepare the Sink to receive a value.
This method must be called and return Poll::Ready(Ok(())) prior to
each call to start_send.
This method returns Poll::Ready once the underlying sink is ready to
receive data. If this method returns Poll::Pending, the current task
is registered to be notified (via cx.waker().wake_by_ref()) when poll_ready
should be called again.
In most cases, if the sink encounters an error, the sink will permanently be unable to receive items.
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error>
Begin the process of sending a value to the sink.
Each call to this function must be preceded by a successful call to
poll_ready which returned Poll::Ready(Ok(())).
As the name suggests, this method only begins the process of sending
the item. If the sink employs buffering, the item isn’t fully processed
until the buffer is fully flushed. Since sinks are designed to work with
asynchronous I/O, the process of actually writing out the data to an
underlying object takes place asynchronously. You must use
poll_flush or poll_close in order to guarantee completion of a
send.
Implementations of poll_ready and start_send will usually involve
flushing behind the scenes in order to make room for new messages.
It is only necessary to call poll_flush if you need to guarantee that
all of the items placed into the Sink have been sent.
In most cases, if the sink encounters an error, the sink will permanently be unable to receive items.
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_flush( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Flush any remaining output from this sink.
Returns Poll::Ready(Ok(())) when no buffered items remain. If this
value is returned then it is guaranteed that all previous values sent
via start_send have been flushed.
Returns Poll::Pending if there is more work left to do, in which
case the current task is scheduled (via cx.waker().wake_by_ref()) to wake up when
poll_flush should be called again.
In most cases, if the sink encounters an error, the sink will permanently be unable to receive items.
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_close( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Flush any remaining output and close this sink, if necessary.
Returns Poll::Ready(Ok(())) when no buffered items remain and the sink
has been successfully closed.
Returns Poll::Pending if there is more work left to do, in which
case the current task is scheduled (via cx.waker().wake_by_ref()) to wake up when
poll_close should be called again.
If this function encounters an error, the sink should be considered to
have failed permanently, and no more Sink methods should be called.
Implementations on Foreign Types§
§impl<'a, T> Sink<T> for SendSink<'a, T>
impl<'a, T> Sink<T> for SendSink<'a, T>
type Error = SendError<T>
fn poll_ready( self: Pin<&mut SendSink<'a, T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <SendSink<'a, T> as Sink<T>>::Error>>
fn start_send( self: Pin<&mut SendSink<'a, T>>, item: T, ) -> Result<(), <SendSink<'a, T> as Sink<T>>::Error>
fn poll_flush( self: Pin<&mut SendSink<'a, T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <SendSink<'a, T> as Sink<T>>::Error>>
fn poll_close( self: Pin<&mut SendSink<'a, T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <SendSink<'a, T> as Sink<T>>::Error>>
§impl<L, R, Item, Error> Sink<Item> for Either<L, R>
impl<L, R, Item, Error> Sink<Item> for Either<L, R>
type Error = Error
fn poll_ready( self: Pin<&mut Either<L, R>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Either<L, R> as Sink<Item>>::Error>>
fn start_send( self: Pin<&mut Either<L, R>>, item: Item, ) -> Result<(), <Either<L, R> as Sink<Item>>::Error>
fn poll_flush( self: Pin<&mut Either<L, R>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Either<L, R> as Sink<Item>>::Error>>
fn poll_close( self: Pin<&mut Either<L, R>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Either<L, R> as Sink<Item>>::Error>>
§impl<P, Item> Sink<Item> for Pin<P>
impl<P, Item> Sink<Item> for Pin<P>
type Error = <<P as Deref>::Target as Sink<Item>>::Error
fn poll_ready( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Pin<P> as Sink<Item>>::Error>>
fn start_send( self: Pin<&mut Pin<P>>, item: Item, ) -> Result<(), <Pin<P> as Sink<Item>>::Error>
fn poll_flush( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Pin<P> as Sink<Item>>::Error>>
fn poll_close( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <Pin<P> as Sink<Item>>::Error>>
§impl<S, Item> Sink<Item> for &mut S
impl<S, Item> Sink<Item> for &mut S
type Error = <S as Sink<Item>>::Error
fn poll_ready( self: Pin<&mut &mut S>, cx: &mut Context<'_>, ) -> Poll<Result<(), <&mut S as Sink<Item>>::Error>>
fn start_send( self: Pin<&mut &mut S>, item: Item, ) -> Result<(), <&mut S as Sink<Item>>::Error>
fn poll_flush( self: Pin<&mut &mut S>, cx: &mut Context<'_>, ) -> Poll<Result<(), <&mut S as Sink<Item>>::Error>>
fn poll_close( self: Pin<&mut &mut S>, cx: &mut Context<'_>, ) -> Poll<Result<(), <&mut S as Sink<Item>>::Error>>
§impl<T> Sink<T> for PollSender<T>where
T: Send,
impl<T> Sink<T> for PollSender<T>where
T: Send,
type Error = PollSendError<T>
fn poll_ready( self: Pin<&mut PollSender<T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), <PollSender<T> as Sink<T>>::Error>>
fn poll_flush( self: Pin<&mut PollSender<T>>, _cx: &mut Context<'_>, ) -> Poll<Result<(), <PollSender<T> as Sink<T>>::Error>>
fn start_send( self: Pin<&mut PollSender<T>>, item: T, ) -> Result<(), <PollSender<T> as Sink<T>>::Error>
fn poll_close( self: Pin<&mut PollSender<T>>, _cx: &mut Context<'_>, ) -> Poll<Result<(), <PollSender<T> as Sink<T>>::Error>>
Implementors§
§impl<'a, S> Sink<&'a [u8]> for CopyToBytes<S>
impl<'a, S> Sink<&'a [u8]> for CopyToBytes<S>
§impl<A, B, Item> Sink<Item> for rama::futures::prelude::future::Either<A, B>
Available on crate feature sink only.
impl<A, B, Item> Sink<Item> for rama::futures::prelude::future::Either<A, B>
sink only.§impl<I, C, S> Sink<(I, SocketAddr)> for UdpFramedRelay<C, S>
impl<I, C, S> Sink<(I, SocketAddr)> for UdpFramedRelay<C, S>
§impl<I, C, T> Sink<(I, SocketAddr)> for UdpFramed<C, T>
impl<I, C, T> Sink<(I, SocketAddr)> for UdpFramed<C, T>
§impl<I, C, T> Sink<(I, UnixSocketAddress)> for UnixDatagramFramed<C, T>
impl<I, C, T> Sink<(I, UnixSocketAddress)> for UnixDatagramFramed<C, T>
§impl<S, Fut, F, Item> Sink<Item> for AndThen<S, Fut, F>where
S: Sink<Item>,
Available on crate feature sink only.
impl<S, Fut, F, Item> Sink<Item> for AndThen<S, Fut, F>where
S: Sink<Item>,
sink only.§impl<S, Fut, F, Item> Sink<Item> for OrElse<S, Fut, F>where
S: Sink<Item>,
Available on crate feature sink only.
impl<S, Fut, F, Item> Sink<Item> for OrElse<S, Fut, F>where
S: Sink<Item>,
sink only.§impl<S, Fut, F, Item> Sink<Item> for Then<S, Fut, F>where
S: Sink<Item>,
Available on crate feature sink only.
impl<S, Fut, F, Item> Sink<Item> for Then<S, Fut, F>where
S: Sink<Item>,
sink only.§impl<S, Fut, F, Item> Sink<Item> for TryFilterMap<S, Fut, F>where
S: Sink<Item>,
Available on crate feature sink only.
impl<S, Fut, F, Item> Sink<Item> for TryFilterMap<S, Fut, F>where
S: Sink<Item>,
sink only.§impl<S, Fut, F, Item, E> Sink<Item> for TrySkipWhile<S, Fut, F>
Available on crate feature sink only.
impl<S, Fut, F, Item, E> Sink<Item> for TrySkipWhile<S, Fut, F>
sink only.§impl<S, Fut, F, Item, E> Sink<Item> for TryTakeWhile<S, Fut, F>
Available on crate feature sink only.
impl<S, Fut, F, Item, E> Sink<Item> for TryTakeWhile<S, Fut, F>
sink only.§impl<S, Item> Sink<Item> for BufferUnordered<S>
Available on crate feature sink only.
impl<S, Item> Sink<Item> for BufferUnordered<S>
sink only.§impl<S, Item> Sink<Item> for IntoStream<S>where
S: Sink<Item>,
Available on crate feature sink only.
impl<S, Item> Sink<Item> for IntoStream<S>where
S: Sink<Item>,
sink only.§impl<S, Item> Sink<Item> for ReadyChunks<S>
Available on crate feature sink only.
impl<S, Item> Sink<Item> for ReadyChunks<S>
sink only.§impl<S, Item> Sink<Item> for TryFlatten<S>
Available on crate feature sink only.
impl<S, Item> Sink<Item> for TryFlatten<S>
sink only.§impl<S, Item> Sink<Item> for TryReadyChunks<S>
Available on crate feature sink only.
impl<S, Item> Sink<Item> for TryReadyChunks<S>
sink only.§impl<S, Item, E> Sink<Item> for TryBufferUnordered<S>
Available on crate feature sink only.
impl<S, Item, E> Sink<Item> for TryBufferUnordered<S>
sink only.§impl<S, Item, E> Sink<Item> for TryBuffered<S>
Available on crate feature sink only.
impl<S, Item, E> Sink<Item> for TryBuffered<S>
sink only.§impl<Si, F, E, Item> Sink<Item> for SinkMapErr<Si, F>
impl<Si, F, E, Item> Sink<Item> for SinkMapErr<Si, F>
§impl<Si, Item, E> Sink<Item> for SinkErrInto<Si, Item, E>
impl<Si, Item, E> Sink<Item> for SinkErrInto<Si, Item, E>
§impl<Si, Item, U, St, F> Sink<U> for WithFlatMap<Si, Item, U, St, F>
impl<Si, Item, U, St, F> Sink<U> for WithFlatMap<Si, Item, U, St, F>
§impl<T> Sink<Message> for AsyncWebSocket<T>
impl<T> Sink<Message> for AsyncWebSocket<T>
type Error = ProtocolError
§impl<T, I, E> Sink<I> for FramedWrite<T, E>
impl<T, I, E> Sink<I> for FramedWrite<T, E>
§impl<_Item, F> Sink<_Item> for FlattenStream<F>
Available on crate feature sink only.
impl<_Item, F> Sink<_Item> for FlattenStream<F>
sink only.§impl<_Item, Fut> Sink<_Item> for TryFlattenStream<Fut>
Available on crate feature sink only.
impl<_Item, Fut> Sink<_Item> for TryFlattenStream<Fut>
sink only.§impl<_Item, Fut, Si> Sink<_Item> for FlattenSink<Fut, Si>where
TryFlatten<Fut, Si>: Sink<_Item>,
Available on crate feature sink only.
impl<_Item, Fut, Si> Sink<_Item> for FlattenSink<Fut, Si>where
TryFlatten<Fut, Si>: Sink<_Item>,
sink only.§impl<_Item, St> Sink<_Item> for TryFlattenUnordered<St>
Available on crate feature sink only.
impl<_Item, St> Sink<_Item> for TryFlattenUnordered<St>
sink only.§impl<_Item, St, F> Sink<_Item> for InspectErr<St, F>
Available on crate feature sink only.
impl<_Item, St, F> Sink<_Item> for InspectErr<St, F>
sink only.§impl<_Item, St, U, F> Sink<_Item> for FlatMapUnordered<St, U, F>
Available on crate feature sink only.
impl<_Item, St, U, F> Sink<_Item> for FlatMapUnordered<St, U, F>
sink only.