pub struct ArrayQueue<T, const N: usize> { /* private fields */ }Expand description
ArrayQueue is a queue, but it uses an array on a stack and can’t be resized.
§Example
use orengine_utils::ArrayQueue;
let mut queue = ArrayQueue::<u32, 2>::new();
queue.push(1).unwrap();
queue.push(2).unwrap();
assert_eq!(queue.pop(), Some(1));
assert_eq!(queue.pop(), Some(2));
assert_eq!(queue.pop(), None);Implementations§
Source§impl<T, const N: usize> ArrayQueue<T, N>
impl<T, const N: usize> ArrayQueue<T, N>
Sourcepub fn as_slices(&self) -> (&[T], &[T])
pub fn as_slices(&self) -> (&[T], &[T])
Returns a pair of slices that represent the occupied region of the queue.
§Example
use orengine_utils::ArrayQueue;
let mut array_queue = ArrayQueue::<u32, 4>::new();
array_queue.push(1).unwrap();
array_queue.push(2).unwrap();
let should_be: (&[u32], &[u32]) = (&[1, 2], &[]);
assert_eq!(array_queue.as_slices(), should_be);
array_queue.push(3).unwrap();
array_queue.push(4).unwrap();
assert_eq!(array_queue.pop(), Some(1));
assert_eq!(array_queue.pop(), Some(2));
array_queue.push(5).unwrap();
let should_be: (&[u32], &[u32]) = (&[3, 4], &[5]);
assert_eq!(array_queue.as_slices(), should_be);Sourcepub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
Returns a pair of mutable slices that represent the occupied region of the queue.
§Example
use orengine_utils::ArrayQueue;
let mut array_queue = ArrayQueue::<u32, 4>::new();
array_queue.push(1).unwrap();
array_queue.push(2).unwrap();
let should_be: (&mut [u32], &mut [u32]) = (&mut [1, 2], &mut []);
assert_eq!(array_queue.as_mut_slices(), should_be);
array_queue.push(3).unwrap();
array_queue.push(4).unwrap();
assert_eq!(array_queue.pop(), Some(1));
assert_eq!(array_queue.pop(), Some(2));
array_queue.push(5).unwrap();
let should_be: (&mut [u32], &mut [u32]) = (&mut [3, 4], &mut [5]);
assert_eq!(array_queue.as_mut_slices(), should_be);Sourcepub unsafe fn inc_head_by(&mut self, number: usize)
pub unsafe fn inc_head_by(&mut self, number: usize)
Increases the head index by the specified number and decreases the length by the same number.
§Safety
The caller must ensure usage of items that become available after this function.
§Example
use orengine_utils::ArrayQueue;
let mut queue = ArrayQueue::from([1, 2, 3, 4]);
queue.pop().unwrap();
queue.push(5).unwrap();
let slices = queue.as_mut_slices();
let should_be: (&mut [u32], &mut [u32]) = (&mut [2, 3, 4], &mut [5]);
assert_eq!(slices, should_be);
for item in slices.0.iter_mut() {
// Do something with items
unsafe { std::ptr::drop_in_place(item); } // Ensure the items are dropped
}
// Here head is 1 and len is 4
let slices = queue.as_slices();
let as_previous: (&[u32], &[u32]) = (&[2, 3, 4], &[5]); // But the queue is still the same, while 3 elements were read
assert_eq!(slices, as_previous);
unsafe { queue.inc_head_by(3); }
// Here head is 0 (4 is wrapped around), and len is 1
let slices = queue.as_slices();
let should_be: (&[u32], &[u32]) = (&[5], &[]);
assert_eq!(slices, should_be); // Now it is validSourcepub unsafe fn dec_len_by(&mut self, number: usize)
pub unsafe fn dec_len_by(&mut self, number: usize)
Decreases the length by the specified number.
§Safety
The caller must ensure that the length is not less than the specified number. And the caller must ensure usage of items that become available after this function.
§Example
use orengine_utils::ArrayQueue;
let mut queue = ArrayQueue::from([1, 2, 3, 4]);
queue.pop().unwrap();
queue.pop().unwrap();
queue.push(5).unwrap();
queue.push(6).unwrap();
let slices = queue.as_mut_slices();
let should_be: (&mut [u32], &mut [u32]) = (&mut [3, 4], &mut [5, 6]);
assert_eq!(slices, should_be);
for item in slices.1.iter_mut() {
// Do something with items
unsafe { std::ptr::drop_in_place(item); } // Ensure the items are dropped
}
// Here head is 2 and len is 4
let slices = queue.as_slices();
let as_previous: (&[u32], &[u32]) = (&[3, 4], &[5, 6]); // But the queue is still the same, while 2 elements were read
assert_eq!(slices, as_previous);
unsafe { queue.dec_len_by(2); }
// Here head is 2 and len is 2
let slices = queue.as_slices();
let should_be: (&[u32], &[u32]) = (&[3, 4], &[]);
assert_eq!(slices, should_be); // Now it is validSourcepub unsafe fn push_unchecked(&mut self, value: T)
pub unsafe fn push_unchecked(&mut self, value: T)
Appends an element to the back of the queue.
§Safety
The caller must ensure that the queue is not full.
Sourcepub fn push(&mut self, value: T) -> Result<(), T>
pub fn push(&mut self, value: T) -> Result<(), T>
Appends an element to the back of the queue or returns Err(value) if the queue is full.
Sourcepub unsafe fn push_priority_value_unchecked(&mut self, value: T)
pub unsafe fn push_priority_value_unchecked(&mut self, value: T)
Pushes the provided value to the front of the queue.
§Safety
The caller must ensure that the queue is not full.
Sourcepub fn push_priority_value(&mut self, value: T) -> Result<(), T>
pub fn push_priority_value(&mut self, value: T) -> Result<(), T>
Pushes the provided value to the front of the queue
or returns Err(value) if the queue is full.
§Example
use orengine_utils::ArrayQueue;
let mut queue = ArrayQueue::<u32, 3>::new();
queue.push_priority_value(1).unwrap(); // [1, _, _]
queue.push(2).unwrap(); // [1, 2, _]
queue.push_priority_value(3).unwrap(); // [3, 1, 2]
assert_eq!(queue.pop(), Some(3));
assert_eq!(queue.pop(), Some(1));
assert_eq!(queue.pop(), Some(2));Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the first element and returns it, or None if the queue is empty.
Sourcepub fn pop_less_priority_value(&mut self) -> Option<T>
pub fn pop_less_priority_value(&mut self) -> Option<T>
Removes the last element and returns it, or None if the queue is empty.
§Example
use orengine_utils::ArrayQueue;
let mut queue = ArrayQueue::<u32, 3>::new();
queue.push(1).unwrap(); // [1, _, _]
queue.push(2).unwrap(); // [1, 2, _]
queue.push(3).unwrap(); // [1, 2, 3]
assert_eq!(queue.pop_less_priority_value(), Some(3));
assert_eq!(queue.pop(), Some(1));
assert_eq!(queue.pop(), Some(2));Sourcepub unsafe fn extend_from_slice(
&mut self,
slice: &[T],
) -> Result<(), NotEnoughSpace>
pub unsafe fn extend_from_slice( &mut self, slice: &[T], ) -> Result<(), NotEnoughSpace>
Sourcepub fn clear_with<F>(&mut self, f: F)where
F: FnMut(T),
pub fn clear_with<F>(&mut self, f: F)where
F: FnMut(T),
Clears with calling the provided function on each element.
Sourcepub fn iter(&self) -> impl ExactSizeIterator<Item = &T>
pub fn iter(&self) -> impl ExactSizeIterator<Item = &T>
Returns a reference iterator over the queue.
Sourcepub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T>
Returns a mutable reference iterator over the queue.
Sourcepub unsafe fn refill_with(
&mut self,
f: impl FnOnce(&mut [MaybeUninit<T>; N]) -> usize,
)
pub unsafe fn refill_with( &mut self, f: impl FnOnce(&mut [MaybeUninit<T>; N]) -> usize, )
Refills the queue with elements provided by the function.
§Safety
The caller must ensure that the queue is empty before refilling.