Struct Lobby

Source
pub struct Lobby<T, const N: usize> { /* private fields */ }
Expand description

A const-size queue-like data structure.

Implementations§

Source§

impl<T, const N: usize> Lobby<T, N>

Source

pub const fn new(arr: [Option<T>; N]) -> Self

Create a new Lobby. The caller must pass in an array of all None.

§Limitation

Until some workaround/fix for #44796 becomes available, an initial array must be passed in.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None, None]);
lobby.push(0);
Source

pub const fn first(&self) -> Option<&T>

Get the head item.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
assert_eq!(None, lobby.first());

lobby.push(0);
assert_eq!(Some(&0), lobby.first());

lobby.push(1);
assert_eq!(Some(&0), lobby.first());
Source

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

Get a mutable reference to the head item. See Self::first.

Source

pub const fn last(&self) -> Option<&T>

Get the tail item.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
assert_eq!(None, lobby.last());

lobby.push(0);
assert_eq!(Some(&0), lobby.last());

lobby.push(1);
assert_eq!(Some(&1), lobby.last());
Source

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

Get a mutable reference to the head item. See Self::last.

Source

pub const fn nth(&self, n: usize) -> Option<&T>

Get the nth item.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
assert_eq!(None, lobby.nth(1));

lobby.push(0);
assert_eq!(None, lobby.nth(1));

lobby.push(1);
assert_eq!(Some(&1), lobby.nth(1));
§Panics

Panics if n is greater than or equal to N.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
lobby.push(0);

let _ = lobby.nth(3);
Source

pub fn nth_mut(&mut self, n: usize) -> Option<&mut T>

Get a mutable reference to the nth item. See Self::nth.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);

lobby.push(0);
lobby.push(1);

let v1 = lobby.nth_mut(1).unwrap();
*v1 = 3;

assert_eq!(Some(&3), lobby.nth(1));
§Panics

Panics under the same conditions as Self::nth.

Source

pub fn push(&mut self, v: T) -> Option<T>

Push a new item to the lobby, returning the head if the lobby is currently full.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);

lobby.push(0);
assert_eq!(Some(&0), lobby.first());

lobby.push(1);
assert_eq!(Some(&0), lobby.first());

lobby.push(2);
assert_eq!(Some(&0), lobby.first());

lobby.push(3);
assert_eq!(Some(&1), lobby.first());
Source

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

Shift out the head item from the lobby.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
lobby.push(0);
lobby.push(1);
lobby.push(2);

assert_eq!(Some(0), lobby.shift());
assert_eq!(Some(1), lobby.shift());
assert_eq!(Some(2), lobby.shift());
assert_eq!(None, lobby.shift());
Source

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

Pop off the tail item from the lobby.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
lobby.push(0);
lobby.push(1);
lobby.push(2);

assert_eq!(Some(2), lobby.pop());
assert_eq!(Some(1), lobby.pop());
assert_eq!(Some(0), lobby.pop());
assert_eq!(None, lobby.pop());
Source

pub const fn is_empty(&self) -> bool

Returns true if the Lobby is empty, false if it is not.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
assert!(lobby.is_empty());

lobby.push(0);
assert!(!lobby.is_empty());

lobby.shift();
assert!(lobby.is_empty());
Source

pub const fn is_full(&self) -> bool

Returns true if the Lobby is full, false if it is not.

use lobby_queue::Lobby;

let mut lobby = Lobby::new([None, None, None]);
assert!(!lobby.is_full());

lobby.push(0);
assert!(!lobby.is_full());

lobby.push(1);
assert!(!lobby.is_full());

lobby.push(2);
assert!(lobby.is_full());
Source

pub const fn len(&self) -> usize

Returns the current number of items stored in the lobby.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the lobby, which will always be N.

Source§

impl<T, const N: usize> Lobby<T, N>

Source

pub fn iter(&self) -> Iter<'_, T, N>

Returns an iterator over the items in the lobby.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T, N>

Returns an iterator that allows modifying each value.

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for Lobby<T, N>

Source§

fn clone(&self) -> Lobby<T, N>

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: Debug, const N: usize> Debug for Lobby<T, N>

Source§

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

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

impl<T, const N: usize> IntoIterator for Lobby<T, N>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T, N>

Creates an iterator from a value. Read more
Source§

impl<T, const N: usize> PartialEq<[Option<T>; N]> for Lobby<T, N>
where T: PartialEq,

Source§

fn eq(&self, other: &[Option<T>; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: usize> PartialEq for Lobby<T, N>
where T: PartialEq,

Source§

fn eq(&self, other: &Lobby<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for Lobby<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> 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.