Struct Writer

Source
pub struct Writer<S> { /* private fields */ }
Available on crate feature asyncio only.
Expand description

Adds items to the queue.

Values sent by the writer will be added to the end of the reader’s buffer, and capacity can be sent back to the writer from the start of the reader’s buffer to allow it to write more data.

A writer will automatically close itself when dropped.

Implementations§

Source§

impl<S> Writer<S>

Source

pub fn is_reader_open(&self) -> bool

Returns if the corresponding reader is still open.

If this is false, any attempt to write or flush the object will fail.

Source

pub fn has_space(&self) -> bool

Returns if space is available in the writer’s buffer.

If this is true it is guaranteed that the next call to poll_empty_buf will return a non-empty slice, unless feed is called first.

Keep in mind that when using a reader and writer on separate threads, a writer that has no space can have more made available at any time - even between calls to has_space and other functions.

Source

pub fn is_flushed(&self) -> bool

Returns if the buffer is flushed, i.e there are no items to read and any read operations will stall.

If this is true a writer can only resume the reader by calling feed to pass items to the reader.

Keep in mind that when using a reader and writer on separate threads, a writer that is not flushed can become flushed at any time - even between calls to is_flushed and other functions.

Source

pub fn poll_empty_buf<T>( &mut self, cx: &mut Context<'_>, ) -> Poll<RegionMut<'_, T>>
where S: Storage<T>,

Attempt to get the writable buffer, waiting for more space if it is empty.

On success, returns Poll::Ready(Ok(buf)).

If no space is available for writing, the method returns Poll::Pending and arranges for the current task to receive a notification when the reader provides space or is closed.

This functions is a lower-level call. It needs to be paired with the feed method to function properly. When calling this method, none of the contents will be “written” in the sense that later calling poll_empty_buf may return the same contents. As such, feed must be called with the number of items that have been written to the buffer to ensure that the items are never returned twice.

An empty buffer returned indicates that the queue cannot be written to as the reader has closed.

Source

pub fn feed(&mut self, len: usize)

Marks items at the start of the writer buffer as ready to be read, removing them from the slice returned by poll_empty_buf and making them available in the reader’s buffer.

§Panics

This function will panic if amt is larger than the writer’s available space buffer.

Source

pub async fn write<T>(&mut self, buf: &[T]) -> usize
where S: Storage<T>, T: Clone,

Writes some items from a buffer into this queue, returning how many items were written.

This function will attempt to write the entire contents of buf, but the entire write may not succeed if not enough space is available.

§Return

It is guaranteed that the return value is <= buf.len().

A return value of 0 indicates one of these two scenarios:

  1. The reader has closed.
  2. The buffer specified had a length of 0.
§Cancel safety

This method is cancel safe. If you use it in a select! statement and some other branch completes first, then it is guaranteed that no data was written.

Source

pub async fn write_all<T>(&mut self, buf: &[T]) -> Result<usize, WriteError>
where S: Storage<T>, T: Clone,

Attempts to write all items in a buffer into this queue.

If the reader closes before all items are written, an error of the kind WriteError::ReaderClosed will be returned.

§Return

If the return value is Ok(n), it is guaranteed that n == buf.len().

§Cancel safety

This method is not cancellation safe. If it is used in a select! statement and some other branch completes first, then the provided buffer may have been partially written, but future calls to write_all will start over from the beginning of the buffer.

Source

pub fn poll_flush( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), WriteError>>

Attempt to flush the buffer, ensuring that any items waiting to be read are consumed by the reader.

On success, returns Poll::Ready(Ok(())). If the reader is closed, returns Poll::Ready(Err(FlushError::ReaderClosed)).

If flushing cannot immediately complete, this method returns Poll::Pending and arranges for the current task to receive a notification when the object can make progress towards flushing.

Source

pub async fn flush(&mut self) -> Result<(), WriteError>

Flushes the buffer, ensuring that any items waiting to be read are consumed by the reader.

If the reader closes before the buffer is flushed, an error of the kind WriteError::ReaderClosed will be returned.

Source

pub fn poll_close( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), WriteError>>

Attempt to close the writer, flushing any remaining data and indicating to the reader that no more data will be written.

On success, returns Poll::Ready(Ok(())). Any future read operations will fail. Closing the writer multiple times has no effect.

Source

pub async fn close(&mut self) -> Result<(), WriteError>

Closes the buffer, flushing any remaining data and indicating to the reader that no more data will be written.

If the reader closes before the buffer is flushed, an error of the kind WriteError::ReaderClosed will be returned.

Trait Implementations§

Source§

impl<S> AsyncWrite for Writer<S>
where S: Storage<u8>,

Available on crate feature std-io only.
Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
Source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Source§

impl<S: Debug> Debug for Writer<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S> Drop for Writer<S>

Source§

fn drop(&mut self)

Closes the writer without flushing.

Auto Trait Implementations§

§

impl<S> Freeze for Writer<S>

§

impl<S> !RefUnwindSafe for Writer<S>

§

impl<S> Send for Writer<S>
where S: Sync + Send,

§

impl<S> Sync for Writer<S>
where S: Sync + Send,

§

impl<S> Unpin for Writer<S>

§

impl<S> !UnwindSafe for Writer<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

Source§

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

Creates a future which will entirely flush this AsyncWrite. Read more
Source§

fn close(&mut self) -> Close<'_, Self>
where Self: Unpin,

Creates a future which will entirely close this AsyncWrite.
Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>
where Self: Unpin,

Creates a future which will write bytes from buf into the object. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> WriteVectored<'a, Self>
where Self: Unpin,

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self>
where Self: Unpin,

Write data into this object. Read more
Source§

fn into_sink<Item>(self) -> IntoSink<Self, Item>
where Item: AsRef<[u8]>, Self: Sized,

Allow using an AsyncWrite as a Sink<Item: AsRef<[u8]>>. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.