Trait Sink
trait Sink<Item>
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.
Associated Types
type ErrorThe 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 as >::Error>>Attempts to prepare the
Sinkto receive a value.This method must be called and return
Poll::Ready(Ok(()))prior to each call tostart_send.This method returns
Poll::Readyonce the underlying sink is ready to receive data. If this method returnsPoll::Pending, the current task is registered to be notified (viacx.waker().wake_by_ref()) whenpoll_readyshould 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 as >::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_readywhich returnedPoll::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_flushorpoll_closein order to guarantee completion of a send.Implementations of
poll_readyandstart_sendwill usually involve flushing behind the scenes in order to make room for new messages. It is only necessary to callpoll_flushif you need to guarantee that all of the items placed into theSinkhave 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 as >::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 viastart_sendhave been flushed.Returns
Poll::Pendingif there is more work left to do, in which case the current task is scheduled (viacx.waker().wake_by_ref()) to wake up whenpoll_flushshould 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 as >::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::Pendingif there is more work left to do, in which case the current task is scheduled (viacx.waker().wake_by_ref()) to wake up whenpoll_closeshould be called again.If this function encounters an error, the sink should be considered to have failed permanently, and no more
Sinkmethods should be called.