pub struct SpscQueue<T: Copy, const CAP: usize> { /* private fields */ }Expand description
A lock-free single-producer single-consumer queue.
Uses atomic operations for real-time safe push/pop without blocking. The capacity must be a power of two for efficient mask-based indexing.
Implementations§
Source§impl<T: Copy + Default, const CAP: usize> SpscQueue<T, CAP>
impl<T: Copy + Default, const CAP: usize> SpscQueue<T, CAP>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new SPSC queue with default policies.
The overflow policy defaults to OverflowPolicy::OverwriteOldest.
§Panics
Panics if CAP is not a power of two.
Sourcepub fn with_policies(
overflow_policy: OverflowPolicy,
default_value: Option<T>,
) -> Self
pub fn with_policies( overflow_policy: OverflowPolicy, default_value: Option<T>, ) -> Self
Create a queue with custom overflow policy and default value.
Sourcepub fn push(&self, value: T) -> QueueResult<()>
pub fn push(&self, value: T) -> QueueResult<()>
Push a value into the queue.
If the queue is full, behaviour depends on OverflowPolicy.
§Errors
Returns QueueFull when the policy is OverflowPolicy::DropNewest
or OverflowPolicy::Block and the queue is full.
§Panics
Panics when the policy is OverflowPolicy::Panic and the queue is full.
Sourcepub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Pop a value from the queue, or return the default value if empty.
Sourcepub fn stats(&self) -> QueueStatsSnapshot
pub fn stats(&self) -> QueueStatsSnapshot
Return a statistics snapshot (currently always empty).
Sourcepub fn set_default(&mut self, value: T)
pub fn set_default(&mut self, value: T)
Set the default value returned when popping from an empty queue.
Sourcepub fn overflow_policy(&self) -> OverflowPolicy
pub fn overflow_policy(&self) -> OverflowPolicy
Return the current overflow policy.
Sourcepub fn set_overflow_policy(&mut self, policy: OverflowPolicy)
pub fn set_overflow_policy(&mut self, policy: OverflowPolicy)
Set the overflow policy.