#[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: usizeImplementations§
source§impl ConcurrentQueue
impl ConcurrentQueue
sourcepub fn size(element_size: usize, capacity: usize) -> usize
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.
sourcepub fn init_at(buffer: &mut [u8], element_size: usize, capacity: usize) -> &Self
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.
sourcepub unsafe fn load_from(buffer: &[u8]) -> &Self
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.
sourcepub fn get(&self, idx: usize) -> Option<&[u8]>
pub fn get(&self, idx: usize) -> Option<&[u8]>
Gets an element from the queue at a specific index
sourcepub fn push(&self, data: &[u8]) -> Option<&mut [u8]>
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.
sourcepub fn push_then<F: FnOnce(&mut [u8])>(
&self,
set_element: F,
) -> Option<&mut [u8]>
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.
sourcepub fn pop_then<F: FnOnce(&[u8]) -> T, T>(&self, map_element: F) -> Option<T>
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.