Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer<const N: usize = 8>(/* private fields */);
Expand description

A reusable batch of frames, filled by group::Consumer::read_frames and drained by group::Producer::write_frames.

A fixed-capacity inline buffer: N frames of stack storage, never a heap allocation and never a spill. Allocate one per task and reuse it for the life of the group rather than one per read.

N defaults to 8. Most reads are not big batches: at the live edge a frame arrives at a time, so the capacity past the first frame or two is idle stack, and a publisher holds one buffer per in-flight group. 8 costs 384 bytes and still reads ~5x faster than a frame at a time (benches/group.rs). Ask for a larger N when you know you are draining a backlog: 32 is ~8x, for 1.5 KB.

A default on a const parameter only applies in type position, so name the type to get it: let buf: frame::Buffer = Buffer::new().

A fill stamps the group’s cache access once for the whole batch, which bounds frames rather than elapsed time. A reader that may take longer than the track’s latency_max to work through one batch calls group::Consumer::keep_alive between frames, or the rest of the group is expired out from under it.

Implementations§

Source§

impl<const N: usize> Buffer<N>

Source

pub fn new() -> Self

An empty buffer with room for N frames.

Source

pub const fn capacity(&self) -> usize

How many frames a single fill can hold.

Source

pub fn len(&self) -> usize

How many frames the buffer currently holds.

Source

pub fn is_empty(&self) -> bool

Whether the buffer holds no frames.

Source

pub fn is_full(&self) -> bool

Whether the buffer is at capacity, so Self::push would refuse.

Source

pub fn filled(&self) -> &[Frame]

The frames from the most recent fill, in order.

Source

pub fn filled_mut(&mut self) -> &mut [Frame]

The frames from the most recent fill, mutably (to take payloads out, say).

Source

pub fn push(&mut self, frame: Frame) -> Result<(), Frame>

Append a frame, handing it back if the buffer is already full.

Fill a buffer this way to hand a whole batch to group::Producer::write_frames.

Source

pub fn drain(&mut self) -> impl ExactSizeIterator<Item = Frame> + '_

Move every frame out, leaving the buffer empty.

Frames left in the iterator when it drops are dropped with it, so the buffer ends up empty either way.

Source

pub fn clear(&mut self)

Drop the current batch, leaving the buffer empty.

Trait Implementations§

Source§

impl<const N: usize> Debug for Buffer<N>

Source§

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

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

impl<const N: usize> Default for Buffer<N>

Source§

fn default() -> Buffer<N>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const N: usize = 8> !Freeze for Buffer<N>

§

impl<const N: usize> RefUnwindSafe for Buffer<N>

§

impl<const N: usize> Send for Buffer<N>
where ArrayVec<Frame, N>: Send,

§

impl<const N: usize> Sync for Buffer<N>
where ArrayVec<Frame, N>: Sync,

§

impl<const N: usize> Unpin for Buffer<N>
where ArrayVec<Frame, N>: Unpin,

§

impl<const N: usize> UnsafeUnpin for Buffer<N>

§

impl<const N: usize> UnwindSafe for Buffer<N>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> MaybeSync for T
where T: Sync,

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more