Struct Array

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

An array type like [T; N], but providing some methods of Vec.

Implementations§

Source§

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

Source

pub const fn new() -> Self

Creates a new empty Array.

§Examples
use my_ecs::ds::Array;

let arr = Array::<i32, 2>::new();
Source

pub const fn capacity(&self) -> usize

Returns capacity, which is N.

§Examples
use my_ecs::ds::Array;

let arr = Array::<i32, 2>::new();
assert_eq!(arr.capacity(), 2);
Source

pub const fn len(&self) -> usize

Returns number of items.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
assert_eq!(arr.len(), 1);
Source

pub const fn is_empty(&self) -> bool

Returns true if the array is empty.

§Examples
use my_ecs::ds::Array;

let arr = Array::<i32, 2>::new();
assert!(arr.is_empty());
Source

pub const fn is_full(&self) -> bool

Returns true if the array contains N items.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
assert!(arr.is_full());
Source

pub fn get(&self, index: usize) -> Option<&T>

Retrieves a shared reference to an item at the given index.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
assert_eq!(arr.get(0), Some(&0));
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Retrieves a mutable reference to an item at the given index.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
assert_eq!(arr.get_mut(0), Some(&mut 0));
Source

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

Returns iterator traversing all items.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
for item in arr.iter() {
    println!("{item}");
}
Source

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

Returns mutable iterator traversing all items.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
for item in arr.iter_mut() {
    *item += 1;
}
Source

pub fn as_slice(&self) -> &[T]

Returns &[T] from the array.

§Examples
use my_ecs::ds::Array;

let arr = Array::<i32, 2>::new();
let slice: &[i32] = arr.as_slice();
Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns &mut [T] from the array.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
let slice: &mut [i32] = arr.as_mut_slice();
Source

pub fn clear(&mut self)

Removes all items in the array.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.clear();
assert!(arr.is_empty());
Source

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

Appends a given item at the end of the array.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
Source

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

Takes out an item from the end of the array.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
assert_eq!(arr.pop(), Some(1));
assert_eq!(arr.pop(), Some(0));
Source

pub fn back(&self) -> Option<&T>

Returns shared reference to the last item.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
assert_eq!(arr.back(), Some(&1));
Source

pub fn front(&self) -> Option<&T>

Returns shared reference to the first item.

§Examples
use my_ecs::ds::Array;

let mut arr = Array::<i32, 2>::new();
arr.push(0);
arr.push(1);
assert_eq!(arr.front(), Some(&0));

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, const N: usize> Index<usize> for Array<T, N>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, const N: usize> IndexMut<usize> for Array<T, N>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.