ArrayQueue

Struct ArrayQueue 

Source
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>

Source

pub const fn new() -> Self

Creates a new ArrayQueue.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the queue.

Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

Source

pub fn is_empty(&self) -> bool

Returns true if the queue is empty.

Source

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);
Source

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);
Source

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 valid
Source

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 valid
Source

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.

Source

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.

Source

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.

Source

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));
Source

pub fn pop(&mut self) -> Option<T>

Removes the first element and returns it, or None if the queue is empty.

Source

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));
Source

pub unsafe fn extend_from_slice( &mut self, slice: &[T], ) -> Result<(), NotEnoughSpace>

Pushes a slice to the queue.

It returns an error if the queue does not have enough space.

§Safety

It T is not Copy, the caller should forget the values.

Source

pub fn clear_with<F>(&mut self, f: F)
where F: FnMut(T),

Clears with calling the provided function on each element.

Source

pub fn clear(&mut self)

Drops all elements in the queue and set the length to 0.

Source

pub fn iter(&self) -> impl ExactSizeIterator<Item = &T>

Returns a reference iterator over the queue.

Source

pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T>

Returns a mutable reference iterator over the queue.

Source

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.

Trait Implementations§

Source§

impl<T, const N: usize> Default for ArrayQueue<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const N: usize> Drop for ArrayQueue<T, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, const N: usize> From<[T; N]> for ArrayQueue<T, N>

Source§

fn from(array: [T; N]) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for ArrayQueue<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for ArrayQueue<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for ArrayQueue<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for ArrayQueue<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for ArrayQueue<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for ArrayQueue<T, N>
where T: UnwindSafe,

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.