Skip to main content

AsyncBufferedOutput

Struct AsyncBufferedOutput 

Source
pub struct AsyncBufferedOutput<O>
where O: AsyncOutput, O::Item: Clone + Default,
{ /* private fields */ }
Expand description

Buffered asynchronous item output.

Accepted items remain owned by this wrapper until the inner output accepts them. Partial writes are committed before another Poll::Pending, making flushing cancellation-safe. Dropping this type cannot perform asynchronous I/O; callers that need delivery guarantees must poll flush to completion or recover the pending buffer through Self::into_parts.

§Type Parameters

  • O: Asynchronous item output type.

Implementations§

Source§

impl<O> AsyncBufferedOutput<O>
where O: AsyncOutput, O::Item: Clone + Default,

Source

pub fn new(inner: O) -> Self

Creates a buffered output with the default item capacity.

§Parameters
  • inner: Asynchronous item output to buffer.
§Returns

Returns a buffered output with DEFAULT_BUFFER_CAPACITY items.

§Panics

Panics if O::Item::default() or O::Item::clone() panics, or the default backing length exceeds Vec’s supported capacity.

Source

pub fn with_capacity(inner: O, capacity: usize) -> Self

Creates a buffered output with a requested item capacity.

§Parameters
  • inner: Asynchronous item output to buffer.
  • capacity: Requested number of buffered items.
§Returns

Returns a buffered output whose actual capacity is at least one.

§Panics

Panics if O::Item::default() or O::Item::clone() panics, or the requested backing length exceeds Vec’s supported capacity.

Source

pub fn try_with_capacity( inner: O, capacity: usize, ) -> Result<Self, TryReserveError>

Tries to create a buffered output with a requested item capacity.

§Parameters
  • inner: Asynchronous item output to buffer.
  • capacity: Requested number of buffered items.
§Returns

Returns a buffered output whose actual capacity is at least one.

§Errors

Returns the allocation error when the backing buffer cannot be allocated.

§Panics

Panics if initializing the backing buffer requires O::Item::default() or O::Item::clone() and either operation panics.

Source

pub const fn inner(&self) -> &O

Returns a shared reference to the wrapped output.

§Returns

Returns the wrapped output. Items may still be pending in this wrapper.

Source

pub fn inner_mut(&mut self) -> &mut O

Returns a mutable reference to the wrapped output.

Direct output calls can be ordered before items retained in the buffer.

§Returns

Returns the wrapped output.

Source

pub fn into_parts(self) -> (O, Buffer<O::Item>)

Consumes this wrapper without flushing the wrapped output.

This method does not call AsyncOutput::flush_async and performs no asynchronous I/O. Call AsyncOutput::flush_async before this method for normal completion. Otherwise, the returned buffer contains the pending items that the caller must write before continuing the logical stream.

§Returns

Returns the wrapped output and pending item buffer.

Source

pub fn capacity(&self) -> usize

Returns the internal item capacity.

§Returns

Returns the total number of items in the backing buffer.

Source

pub const fn pending_len(&self) -> usize

Returns the number of pending buffered items.

§Returns

Returns the readable-window length awaiting delivery.

Source

pub fn pending(&self) -> &[O::Item]

Returns the pending buffered item window.

§Returns

Returns items accepted by this wrapper but not yet accepted by the inner output.

Source

pub fn try_reserve_capacity( &mut self, capacity: usize, ) -> Result<(), TryReserveError>

Tries to ensure that the internal item capacity is at least capacity.

Pending items are retained and this method performs no I/O.

§Parameters
  • capacity: Minimum total item capacity to reserve.
§Returns

Returns Ok(()) after the backing buffer has at least capacity item slots.

§Errors

Returns the allocation error when the backing buffer cannot grow.

§Panics

Panics if growing the backing buffer requires O::Item::default() or O::Item::clone() and either operation panics.

Source

pub fn spare_capacity(&self) -> usize

Returns the unused capacity in the internal buffer.

§Returns

Returns the number of items that can be buffered without draining.

Source

pub fn spare_raw_parts_mut(&mut self) -> (&mut [O::Item], usize, usize)

Returns the full backing storage and its spare-tail range.

Call Self::advance after writing initialized items into the returned spare range.

§Returns

Returns the backing storage together with the first and past-the-end indexes of its spare range.

Source

pub unsafe fn advance(&mut self, count: usize)

Advances the pending-item limit without checking bounds.

§Parameters
  • count: Number of initialized spare items to mark as pending.
§Safety

The caller must guarantee that count <= self.spare_capacity() and that the corresponding spare items have been initialized.

Source

pub fn poll_ensure_spare_capacity( self: Pin<&mut Self>, cx: &mut Context<'_>, count: usize, ) -> Poll<Result<()>>

Polls delivery of pending items when count spare items are needed.

§Parameters
  • cx: Task context used to register a wake-up.
  • count: Minimum number of spare item slots to make available.
§Returns

Returns a ready success when the requested spare capacity is available, or Poll::Pending while the wrapped output is not ready.

§Errors

Returns ErrorKind::InvalidInput when count exceeds the buffer capacity, ErrorKind::WriteZero when draining makes no progress, or an error reported by the wrapped output.

Source§

impl<O> AsyncBufferedOutput<O>
where O: AsyncOutput + Unpin, O::Item: Clone + Default + Unpin,

Source

pub async fn ensure_spare_capacity_async(&mut self, count: usize) -> Result<()>

Asynchronously ensures that the pending buffer has room for at least count more items.

Pending items are written to the wrapped output when necessary; this does not flush the wrapped output itself.

§Parameters
  • count: Minimum number of spare item slots to make available.
§Returns

Returns Ok(()) when the requested spare capacity is available.

§Errors

Returns ErrorKind::InvalidInput when count exceeds the buffer capacity, ErrorKind::WriteZero when the wrapped output makes no progress, or an error from the wrapped output.

Trait Implementations§

Source§

impl<O> AsyncClose for AsyncBufferedOutput<O>
where O: AsyncClose, O::Item: Clone + Default,

Source§

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

Polls delivery of pending items followed by closing the inner output.

§Parameters
  • cx: Task context used to register a wake-up.
§Returns

Returns Poll::Pending while delivery or closing is incomplete, otherwise a ready success result.

§Errors

Returns io::ErrorKind::WriteZero if draining makes no progress, or an error reported by the wrapped output. Invalid asynchronous error kinds from the close operation are normalized to io::ErrorKind::InvalidData.

Source§

fn close_async(&mut self) -> CloseFuture<'_, Self> ⓘ
where Self: Sized + Unpin,

Creates a future that closes this output. Read more
Source§

impl<O> AsyncOutput for AsyncBufferedOutput<O>
where O: AsyncOutput, O::Item: Clone + Default,

Source§

type Item = <O as AsyncOutput>::Item

Item type accepted by the wrapped output.

Source§

fn is_buffered(&self) -> bool

Reports that this output already buffers items.

§Returns

Always returns true.

Source§

unsafe fn poll_write_unchecked( self: Pin<&mut Self>, cx: &mut Context<'_>, input: &[Self::Item], index: usize, count: usize, ) -> Poll<Result<usize>>

Polls one write through the retained item buffer.

A zero-length request completes immediately. The method first uses spare buffer capacity, then drains pending items when necessary.

§Parameters
  • cx: Task context used to register a wake-up.
  • input: Source item slice.
  • index: Starting source index.
  • count: Maximum number of items to accept.
§Returns

Returns Poll::Pending when pending items cannot yet be delivered. A ready success contains the number of newly accepted items.

§Errors

Returns io::ErrorKind::WriteZero if draining makes no progress. Other errors are propagated from the wrapped output.

§Panics

May panic if a nonzero requested input range does not fit. Debug builds validate buffered-copy ranges before copying.

§Safety

The range index..index + count must be valid for input.

Source§

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

Polls delivery of pending items followed by the inner flush operation.

§Parameters
  • cx: Task context used to register a wake-up.
§Returns

Returns Poll::Pending while delivery or flushing is incomplete, otherwise a ready success result.

§Errors

Returns io::ErrorKind::WriteZero if draining makes no progress, or an error reported by the wrapped output. Invalid asynchronous error kinds from the flush operation are normalized to io::ErrorKind::InvalidData.

Source§

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

Polls one write from the full source slice. Read more
Source§

fn write_async<'a>( &'a mut self, input: &'a [Self::Item], ) -> WriteFuture<'a, Self> ⓘ
where Self: Sized + Unpin,

Creates a future that performs one asynchronous write operation. Read more
Source§

fn write_fully_async<'a>( &'a mut self, input: &'a [Self::Item], ) -> WriteFullyFuture<'a, Self> ⓘ
where Self: Sized + Unpin,

Creates a future that writes the entire source slice. Read more
Source§

fn flush_async(&mut self) -> FlushFuture<'_, Self> ⓘ
where Self: Sized + Unpin,

Creates a future that flushes internally buffered items. Read more
Source§

impl<O> Debug for AsyncBufferedOutput<O>
where O: AsyncOutput + Debug, O::Item: Clone + Default + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<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 = !

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

fn try_from(value: U) -> Result<T, !>

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.