VecQueue

Struct VecQueue 

Source
pub struct VecQueue<T> { /* private fields */ }
Expand description

A queue that uses a vector to store the elements.

It is similar to std::collections::VecDeque, but it provides a few additional methods that are used by Orengine's projects.

Implementations§

Source§

impl<T> VecQueue<T>

Source

pub const fn new_const() -> Self

Creates a new VecQueue without any capacity.

Source

pub fn new() -> Self

Creates a new VecQueue with the default capacity.

Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

Source

pub fn is_empty(&self) -> bool

Returns whether the queue is empty.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given VecQueue.

The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional.

Does nothing if capacity is already sufficient.

Source

pub fn extend_to(&mut self, capacity: usize)

Extends the vector to the given capacity.

§Panics

Panics if the provided capacity is not a power of two or is less than the current capacity.

Source

pub fn push(&mut self, value: T)

Pushes a value to the queue.

Source

pub fn push_priority_value(&mut self, value: T)

Pushes the provided value to the front of the queue.

§Example
use orengine_utils::VecQueue;

let mut queue = VecQueue::new();

queue.push_priority_value(1); // [1, _, _]
queue.push(2); // [1, 2, _]
queue.push_priority_value(3); // [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>

Pops a value from the queue.

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::VecQueue;

let mut queue = VecQueue::new();

queue.push(1); // [1, _, _]
queue.push(2); // [1, 2, _]
queue.push(3); // [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])

Pushes a slice to the queue.

§Safety

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

Source

pub unsafe fn take_batch<R, F: FnOnce(&[T], &[T]) -> R>( &mut self, f: F, limit: usize, ) -> R

Accepts a function that will be called with the slices of the queue to move.

§Safety

The function should copy all elements from the provided slices.

§Example
use orengine_utils::VecQueue;

let mut queue = VecQueue::new();

for i in 0..10 {
    queue.push(i);
}

let mut receiver = Vec::with_capacity(10);

unsafe {
    let popped = queue.take_batch(|first_slice, second_slice| {
        receiver.extend_from_slice(first_slice);
        receiver.extend_from_slice(second_slice);

        first_slice.len() + second_slice.len()
    }, 8);

    assert_eq!(popped, 8);
}

assert_eq!(receiver, (0..8).collect::<Vec<_>>());
assert_eq!(queue.len(), 2);
assert_eq!(queue.pop(), Some(8));
assert_eq!(queue.pop(), Some(9));
Source

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

Clears the queue by calling the provided function on each element.

Source

pub fn clear(&mut self)

Clears the queue.

Source

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

Returns an iterator over the queue.

Source

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

Returns a mutable iterator over the queue.

Trait Implementations§

Source§

impl<T: Clone> Clone for VecQueue<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Default for VecQueue<T>

Source§

fn default() -> Self

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

impl<T> Drop for VecQueue<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for VecQueue<T>

§

impl<T> RefUnwindSafe for VecQueue<T>
where T: RefUnwindSafe,

§

impl<T> !Send for VecQueue<T>

§

impl<T> !Sync for VecQueue<T>

§

impl<T> Unpin for VecQueue<T>

§

impl<T> UnwindSafe for VecQueue<T>
where T: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.