Skip to main content

ArrayVec

Struct ArrayVec 

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

A fixed-size array, which has vector-like operations.

ArrayVec provides a vector-like interface with a compile-time fixed capacity N. Elements are stored in a stack-allocated array, making it suitable for no_std environments and situations where heap allocation is not available or desirable.

§Type Parameters

  • T: The type of elements stored in the vector
  • N: The maximum number of elements (capacity)

§Examples

use planck_noalloc::vec::ArrayVec;

// Create a vector with capacity 4
let mut vec = ArrayVec::<String, 4>::new();
vec.push(String::from("hello"));
vec.push(String::from("world"));

assert_eq!(vec.len(), 2);

Implementations§

Source§

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

Source

pub const fn new() -> Self

Creates a new ArrayVec with no elements.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
assert_eq!(vec.len(), 0);
assert!(vec.is_empty());
Source

pub fn try_push(&mut self, value: T) -> Result<(), ArrayVecError>

Tries to push a value into the ArrayVec.

§Errors

Returns an error if the ArrayVec is full.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 1>::new();
assert!(vec.try_push(1).is_ok());
assert_eq!(vec.len(), 1);
assert!(vec.try_push(2).is_err());
Source

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

Pushes a value into the ArrayVec.

§Panics

Panics if the ArrayVec is full.

Source

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

Removes and returns the last element, or None if empty.

Source

pub fn clear(&mut self)

Removes all elements.

Source

pub fn swap_remove(&mut self, index: usize) -> T

Removes the element at index by swapping it with the last element.

§Panics

Panics if index >= len.

Source

pub fn len(&self) -> usize

Returns the number of elements in the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
assert_eq!(vec.len(), 0);
vec.push(1);
assert_eq!(vec.len(), 1);
vec.push(2);
assert_eq!(vec.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the ArrayVec is empty.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
assert!(vec.is_empty());
vec.push(1);
assert!(!vec.is_empty());
Source

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

Returns a slice of all the elements in the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
Source

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

Returns a mutable slice of all the elements in the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

assert_eq!(vec.as_mut_slice(), &mut [1, 2, 3, 4]);
Source

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

Returns an iterator over the elements of the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

for i in vec.iter() {
    assert!(*i == 1 || *i == 2 || *i == 3 || *i == 4);
    // do something
}
Source

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

Returns a mutable iterator over the elements of the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

for i in vec.iter_mut() {
    *i += 1;
    // do something
}
Source

pub fn reverse(&mut self)

Reverses the order of the elements in the ArrayVec.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

vec.reverse();

assert_eq!(vec.as_slice(), &[4, 3, 2, 1]);
Source

pub fn insert(&mut self, index: usize, value: T)

Inserts value at index, shifting all elements after it to the right.

§Panics

Panics if index > len or if the ArrayVec is full.

Source

pub fn remove(&mut self, index: usize) -> T

Removes and returns the element at index, shifting all elements after it to the left. Preserves ordering.

§Panics

Panics if index >= len.

Source

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

Returns the last element, or None if empty.

Source

pub fn is_full(&self) -> bool

Returns true if the ArrayVec is at capacity.

Source

pub fn as_ptr(&self) -> *const T

Returns a pointer to the underlying data.

Source

pub fn as_mut_ptr(&mut self) -> *mut T

Returns a mutable pointer to the underlying data.

Source§

impl<T, const U: usize> ArrayVec<T, U>
where T: Copy + PartialEq,

Source

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

Returns true if the ArrayVec contains the given value.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

assert!(vec.contains(&1));
assert!(!vec.contains(&5));
Source§

impl<T, const U: usize> ArrayVec<T, U>
where T: Copy + Ord,

Source

pub fn sort_unstable(&mut self)

Sorts the ArrayVec in place. The sort is not stable, meaning that the relative order of elements that are equal is not preserved.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

vec.sort_unstable();

assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

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

Returns a reference to the element at the given index.

§Panics

Panics if the index is out of bounds.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
assert_eq!(vec[3], 4);
Source§

type Output = T

The returned type after indexing.
Source§

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

Source§

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

Returns a mutable reference to the element at the given index.

§Panics

Panics if the index is out of bounds.

§Examples
use planck_noalloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);

assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
assert_eq!(vec[3], 4);

vec[0] = 5;
assert_eq!(vec[0], 5);
Source§

impl<'a, T, const N: usize> IntoIterator for &'a ArrayVec<T, N>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a mut ArrayVec<T, N>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnsafeUnpin for ArrayVec<T, N>
where T: UnsafeUnpin,

§

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