Struct atomicring::AtomicRingQueue
source · pub struct AtomicRingQueue<T> { /* private fields */ }
Expand description
A constant-size almost lock-free concurrent ring buffer with blocking poll support
See AtomicRingQueue for implementation details
Examples
// create an AtomicRingQueue with capacity of 1024 elements
let ring = ::atomicring::AtomicRingQueue::with_capacity(900);
// try_pop removes an element of the buffer and returns None if the buffer is empty
assert_eq!(None, ring.try_pop());
// push_overwrite adds an element to the buffer, overwriting the oldest element if the buffer is full:
ring.push_overwrite(10);
assert_eq!(10, ring.pop());
assert_eq!(None, ring.try_pop());
Implementations§
source§impl<T> AtomicRingQueue<T>
impl<T> AtomicRingQueue<T>
sourcepub fn with_capacity(capacity: usize) -> AtomicRingQueue<T>
pub fn with_capacity(capacity: usize) -> AtomicRingQueue<T>
Constructs a new empty AtomicRingQueue
Examples
// create an AtomicRingQueue with capacity of 1024 elements
let ring = ::atomicring::AtomicRingQueue::with_capacity(900);
// try_pop removes an element of the buffer and returns None if the buffer is empty
assert_eq!(None, ring.try_pop());
// push_overwrite adds an element to the buffer, overwriting the oldest element if the buffer is full:
ring.push_overwrite(10);
assert_eq!(10, ring.pop());
assert_eq!(None, ring.try_pop());
sourcepub fn try_push(&self, content: T) -> Result<(), T>
pub fn try_push(&self, content: T) -> Result<(), T>
Try to push an object to the atomic ring buffer. If the buffer has no capacity remaining, the pushed object will be returned to the caller as error.
sourcepub fn push_overwrite(&self, content: T)
pub fn push_overwrite(&self, content: T)
Pushes an object to the atomic ring buffer. If the buffer is full, another object will be popped to make room for the new object.
sourcepub fn try_pop(&self) -> Option<T>
pub fn try_pop(&self) -> Option<T>
Pop an object from the ring buffer, returns None if the buffer is empty
sourcepub fn pop(&self) -> T
pub fn pop(&self) -> T
Pop an object from the ring buffer, waits indefinitely if the buf is empty
sourcepub fn pop_until(&self, deadline: Instant) -> Option<T>
pub fn pop_until(&self, deadline: Instant) -> Option<T>
Pop an object from the ring buffer, waiting until the given instant if the buffer is empty. Returns None on timeout
sourcepub fn pop_for(&self, timeout: Duration) -> Option<T>
pub fn pop_for(&self, timeout: Duration) -> Option<T>
Pop an object from the ring buffer, waiting until the given instant if the buffer is empty. Returns None on timeout
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of objects stored in the ring buffer that are not in process of being removed.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns the true if ring buffer is empty. Equivalent to self.len() == 0
sourcepub fn remaining_cap(&self) -> usize
pub fn remaining_cap(&self) -> usize
Returns the remaining capacity of the ring buffer.
This is equal to self.cap() - self.len() - pending writes + pending reads
.
Trait Implementations§
impl<T: Send> Send for AtomicRingQueue<T>
If T is Send, AtomicRingQueue is Send + Sync
impl<T: Send> Sync for AtomicRingQueue<T>
Any particular T
should never accessed concurrently, so T does not need to be Sync.
If T is Send, AtomicRingQueue is Send + Sync