pub struct StreamWriter<T: 'static> { /* private fields */ }async only.Expand description
Represents the writable end of a Component Model stream.
Implementations§
Source§impl<T> StreamWriter<T>
impl<T> StreamWriter<T>
Sourcepub fn write(&mut self, values: Vec<T>) -> StreamWrite<'_, T> ⓘ
pub fn write(&mut self, values: Vec<T>) -> StreamWrite<'_, T> ⓘ
Initiate a write of the values provided into this stream.
This method is akin to an async fn except that the returned
StreamWrite future can also be cancelled via StreamWrite::cancel
to re-acquire undelivered values.
This method will perform at most a single write of the values
provided. The returned future will resolve once the write has completed.
§Return Values
The returned StreamWrite future returns a tuple of (result, buf).
The result can be StreamResult::Complete(n) meaning that n values
were sent from values into this writer. A result of
StreamResult::Dropped means that no values were sent and the other side
has hung-up and sending values will no longer be possible.
The buf returned is an AbiBuffer<T> which retains ownership of the
original values provided here. That can be used to re-acquire values
through the AbiBuffer::into_vec method. The buf maintains an
internal cursor of how many values have been written and if the write
should be resumed to write the entire buffer then the
StreamWriter::write_buf method can be used to resume writing at the
next value in the buffer.
§Cancellation
The returned StreamWrite future can be cancelled like any other Rust
future via drop, but this means that values will be lost within the
future. The StreamWrite::cancel method can be used to re-acquire the
in-progress write that is being done with values. This is effectively
a way of forcing the future to immediately resolve.
Note that if this future is cancelled via drop it does not mean that
no values were sent. It may be possible that values were still sent
despite being cancelled. Cancelling a write and determining what
happened must be done with StreamWrite::cancel.
Sourcepub fn write_buf(&mut self, values: AbiBuffer<T>) -> StreamWrite<'_, T> ⓘ
pub fn write_buf(&mut self, values: AbiBuffer<T>) -> StreamWrite<'_, T> ⓘ
Same as StreamWriter::write, except this takes AbiBuffer<T>
instead of Vec<T>.
Sourcepub async fn write_all(&mut self, values: Vec<T>) -> Vec<T>
pub async fn write_all(&mut self, values: Vec<T>) -> Vec<T>
Writes all of the values provided into this stream.
This is a higher-level method than StreamWriter::write and does not
expose cancellation for example. This will successively attempt to write
all of values provided into this stream. Upon completion the same
vector will be returned and any remaining elements in the vector were
not sent because the stream was dropped.
Sourcepub async fn write_one(&mut self, value: T) -> Option<T>
pub async fn write_one(&mut self, value: T) -> Option<T>
Writes the singular value provided
This is a higher-level method than StreamWriter::write and does not
expose cancellation for example. This will attempt to send value on
this stream.
If the other end hangs up then the value is returned back as
Some(value), otherwise None is returned indicating the value was
sent.