#[repr(C)]
pub struct ConcurrentQueue { pub msg_size: usize, pub msg_capacity: usize, /* private fields */ }
Expand description

An unsized bounded concurrent queue (Fifo) that makes use of atomics and does not use pointers internally. This allows the queue to be created inside a buffer of type &[u8] via ConcurrentQueue::init_at. The required buffer size can be requested in advance via ConcurrentQueue::size by providing the size and maximum number of entries. # Example

// Create a ConcurrentQueue inside of a Vec<u8> buffer object
let required_size = ConcurrentQueue::size(1, 4);
let mut  buffer = vec![0u8; required_size];
ConcurrentQueue::init_at(&mut buffer, 1, 4);
let queue1 = unsafe { ConcurrentQueue::load_from(&buffer) };
let queue2 = unsafe { ConcurrentQueue::load_from(&buffer) };

// Let's push some values in the queue
assert!(queue1.push(&[1]).is_some());
assert!(queue2.push(&[2]).is_some());

// Now pop them using the Fifo method
assert_eq!(queue2.pop().unwrap()[0], 1);
assert_eq!(queue1.pop().unwrap()[0], 2);

// When the queue is empty, pop will return None
assert_eq!(queue1.pop(), None);
assert_eq!(queue2.pop(), None);

Fields§

§msg_size: usize§msg_capacity: usize

Implementations§

source§

impl ConcurrentQueue

source

pub fn size(element_size: usize, capacity: usize) -> usize

Calculates the required buffer size to fit a MessageQueue object with capacity maximum elements and a fixed size of element_size bytes per element.

source

pub fn init_at(buffer: &mut [u8], element_size: usize, capacity: usize) -> &Self

Creates a new empty ConcurrentQueue in given buffer. Even though this function returns a reference to the newly created ConcurrentQueue, it should be dropped to release the mutable reference to the buffer.

§Panics

If the buffer size is not exactly the required size to fit this ConcurrentQueue object.

source

pub unsafe fn load_from(buffer: &[u8]) -> &Self

Loads a ConcurrentQueue from the specified buffer.

§Safety

The buffer must contain exactly one valid ConcurrentQueue, which has to be initialized through ConcurrentQueue::init_at. Also mutating or reading raw values from the buffer may result in UB, because the ConcurrentQueue relies on internal safety mechanisms to prevent UB due to shared mutable state.

source

pub fn get(&self, idx: usize) -> Option<&[u8]>

Gets an element from the queue at a specific index

source

pub fn push(&self, data: &[u8]) -> Option<&mut [u8]>

Pushes an element to the back of the queue. If there was space, a mutable reference to the inserted element is returned.

source

pub fn push_then<F: FnOnce(&mut [u8])>( &self, set_element: F, ) -> Option<&mut [u8]>

Pushes an uninitialized element and then calls a closure to set its memory in-place. If there was space, a mutable reference to the inserted element is returned.

source

pub fn pop(&self) -> Option<Box<[u8]>>

Tries to pop an element from the front of the queue.

source

pub fn pop_then<F: FnOnce(&[u8]) -> T, T>(&self, map_element: F) -> Option<T>

Calls a mapping closure on the first element that is about to be popped from the queue. Only the return value of the closure is returned by this function. If the popped element is needed as owned data, consider using ConcurrentQueue::pop instead.

source

pub fn peek_then<T, F: FnOnce(Option<&[u8]>) -> T>(&self, f: F) -> T

source

pub fn len(&self) -> usize

Returns the current length of this queue

source

pub fn is_empty(&self) -> bool

source

pub fn clear(&self)

Trait Implementations§

source§

impl Debug for ConcurrentQueue

source§

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

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

impl Pointee for ConcurrentQueue

§

type Metadata = usize

The type for metadata in pointers and references to Self.
source§

impl Send for ConcurrentQueue

source§

impl Sync for ConcurrentQueue

Auto Trait Implementations§

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