RingBufferProducer

Struct RingBufferProducer 

Source
pub struct RingBufferProducer<T, PW: OptionalWaiter, CW: OptionalWaiter> { /* private fields */ }
Expand description

Producer side of the ring buffer.

Implementations§

Source§

impl<T, PW: OptionalWaiter, CW: OptionalWaiter> RingBufferProducer<T, PW, CW>

Source

pub fn push(&mut self, v: T) -> Result<(), PushError<T>>

Pushes a single item into the ring buffer.

If there was enough space, returns Ok(()).

If the buffer is full, returns PushError::Full(v). If the receiver had been dropped, returns PushError::Closed(v). The passed-in value is returned so that pushing may be attempted later.

Source

pub fn extend<I: Iterator<Item = T>>( &mut self, it: I, ) -> Result<I, ClosedError<I>>

Pushes a sequence of items into the ring buffer.

Will stop when the iterator runs out of items or the ring buffer runs out of space, whichever comes first. Returns the iterator with the remaining items. It’s recommended to use a FusedIterator.

In the case of the iterator panicing, already-pushed items may not appear to the consumer, and may leak instead.

This can be slightly more efficient than pushing individually, as the atomic operations for updating the counters will not update until all the items are pushed. If T is Copy, consider using RingBufferProducer::push_slice instead.

Returns Err(ClosedError(it)) if the receiver had been dropped, returning the iterator without touching it.

Source

pub fn uninitialized_view( &mut self, ) -> Result<UninitWriteView<'_, T, PW, CW>, ClosedError<()>>

Gets a view into the currently writable portion of this ring buffer.

This returns a wrapper over two slices, which, when concatenated, form the available space in order. The user should write items to the slices in order, then call UninitWriteView::produce to make them available to the consumer.

As opposed to RingBufferProducer::view, this does not do any initialization of the available slots, and so the returned slices have elements of MaybeUninit<T>. Its up to the user to write to the slots via MaybeUninit::write and call UninitWriteView::produce with the correct amount.

This can be used, for example, to read bytes directly to the ring buffer, via [std::io::ReadBuf], without having to use another buffer and paying for the overhead of initializing the values.

Source

pub fn available_len(&self) -> usize

Gets how many items that can be written.

If the consumer side is being used on another thread, the actual amount may increase after this function has returned.

Source

pub fn capacity(&self) -> usize

Returns the maximum number of elements that the buffer can hold.

Source§

impl<T: Default, PW: OptionalWaiter, CW: OptionalWaiter> RingBufferProducer<T, PW, CW>

Source

pub fn view(&mut self) -> Result<WriteView<'_, T, PW, CW>, ClosedError<()>>

Gets a view into the currently writable portion of this ring buffer.

This returns a wrapper over two slices, which, when concatenated, form the available space in order. The user should write items to the slices in order, then call UninitWriteView::produce to make them available to the consumer.

As opposed to RingBufferProducer::uninitialized_view, this initializes each element with Default::default, meaning there will be no uninitialized values in the buffer and the interface can be used in safe code.

This can be used, for example, to read bytes directly to the ring buffer, via std::io::Read::read, without having to use another buffer.

Source

pub fn sized_view( &mut self, size: usize, ) -> Result<WriteView<'_, T, PW, CW>, ClosedError<()>>

Gets a view into the currently writable portion of this ring buffer, limited by a size.

Works similar to RingBufferProducer::view, but caps the number of elements in the view to the specified number. This can be used to avoid default-initializing an excessive number of elements.

Source§

impl<T: Copy, PW: OptionalWaiter, CW: OptionalWaiter> RingBufferProducer<T, PW, CW>

Source

pub fn push_slice(&mut self, values: &[T]) -> Result<usize, ClosedError<()>>

Pushes a slice of copyable values.

This memcpy’s the slice into the buffer, so it should be more efficient than RingBufferProducer::push or RingBufferProducer::extend.

Returns the amount of items pushed, which may be less than the length of the slice is the buffer fills up.

If the receiver had been dropped, returns Err(ClosedError(())) without touching the values slice.

Source§

impl<T, PW, CW: OptionalWaiter> RingBufferProducer<T, PW, CW>
where PW: for<'a> Waiter<'a> + OptionalWaiter,

Source

pub fn wait<'a>(&'a mut self) -> <PW as Waiter<'a>>::Future

Waits for space to become available in the buffer, or for the consumer to be closed.

This method is only available if the buffer was created with a producer waker.

How the wait happens is up to the waiter - it may block, return a future, etc. However, it should return immediately if space is currently available.

Source

pub fn waiter(&mut self) -> &PW

Gets the waiter.

Can be used to alter settings of the waiter, for example, by setting a timeout.

Trait Implementations§

Source§

impl<T, PW: OptionalWaiter, CW: OptionalWaiter> Drop for RingBufferProducer<T, PW, CW>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, PW, CW> Freeze for RingBufferProducer<T, PW, CW>

§

impl<T, PW, CW> RefUnwindSafe for RingBufferProducer<T, PW, CW>

§

impl<T, PW, CW> Send for RingBufferProducer<T, PW, CW>
where T: Send,

§

impl<T, PW, CW> Sync for RingBufferProducer<T, PW, CW>
where T: Send,

§

impl<T, PW, CW> Unpin for RingBufferProducer<T, PW, CW>

§

impl<T, PW, CW> UnwindSafe for RingBufferProducer<T, PW, CW>

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