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>
impl<const N: usize> Buffer<N>
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Whether the buffer is at capacity, so Self::push would refuse.
Sourcepub fn filled_mut(&mut self) -> &mut [Frame]
pub fn filled_mut(&mut self) -> &mut [Frame]
The frames from the most recent fill, mutably (to take payloads out, say).
Sourcepub fn push(&mut self, frame: Frame) -> Result<(), Frame>
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.
Sourcepub fn drain(&mut self) -> impl ExactSizeIterator<Item = Frame> + '_
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.