Skip to main content

Queue

Struct Queue 

Source
pub struct Queue { /* private fields */ }
Expand description

Thread-safe, bounded FIFO queue with backpressure.

Unlike crate::RingBuffer, this queue never drops frames. When the byte budget would be exceeded, push returns Error::ChannelFull and the frame is rejected. A max_bytes of 0 means the queue is unbounded.

§Thread safety

All public methods take &self and synchronize via an internal Mutex.

Implementations§

Source§

impl Queue

Source

pub fn new(max_bytes: usize) -> Self

Create a queue with the given byte limit.

A max_bytes of 0 means unbounded — pushes will never fail due to capacity.

§Warning

When max_bytes is 0 the queue has no memory limit. If the producer pushes data faster than the consumer drains it, memory usage will grow without bound, eventually causing an out-of-memory (OOM) condition. Prefer a non-zero byte limit for production use and reserve 0 (or Queue::unbounded) for cases where unbounded growth is explicitly acceptable.

Source

pub fn unbounded() -> Self

Create an unbounded queue.

Equivalent to Queue::new(0).

§Warning

An unbounded queue has no memory limit. If the consumer cannot keep up with the producer, memory usage will grow without bound. Prefer Queue::new with a reasonable byte limit for production use.

Source

pub fn push(&self, frame: &[u8]) -> Result<(), Error>

Push a frame into the queue.

Returns Ok(()) if the frame was accepted. Returns Err(Error::ChannelFull) if the frame (plus its 4-byte length prefix) would exceed max_bytes. When max_bytes is 0 (unbounded), pushes always succeed.

Source

pub fn try_pop(&self) -> Option<Vec<u8>>

Read one frame from the front of the queue (FIFO).

Returns None if the queue is empty.

Source

pub fn drain_all(&self) -> Vec<u8>

Drain all queued frames into a single binary blob and clear the queue.

§Wire format
[u32 LE frame_count]
[u32 LE len_1][bytes_1]
[u32 LE len_2][bytes_2]
...

Returns an empty Vec if the queue is empty.

Source

pub fn frame_count(&self) -> usize

Number of frames currently queued.

Source

pub fn bytes_used(&self) -> usize

Number of bytes currently used (including per-frame length prefixes).

Source

pub fn max_bytes(&self) -> usize

Maximum byte budget (0 means unbounded).

Source

pub fn clear(&self)

Clear all queued frames.

Trait Implementations§

Source§

impl Debug for Queue

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Queue

§

impl RefUnwindSafe for Queue

§

impl Send for Queue

§

impl Sync for Queue

§

impl Unpin for Queue

§

impl UnsafeUnpin for Queue

§

impl UnwindSafe for Queue

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.