Skip to main content

ConstVec

Struct ConstVec 

Source
pub struct ConstVec<T, const MAX_SIZE: usize = DEFAULT_MAX_SIZE> { /* private fields */ }
Expand description

A version of Vec that is usable in const contexts.

ConstVec has a fixed maximum size, but it can grow and shrink within that size limit as needed.

§Example

const VEC: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec.push(3);
    vec.push(4);
    assert!(vec.pop().unwrap() == 4);
    vec
};

assert_eq!(VEC.as_ref(), &[1, 2, 3]);

Implementations§

Source§

impl<T> ConstVec<T>

Source

pub const fn new() -> Self

Create a new empty ConstVec.

Source§

impl<T, const MAX_SIZE: usize> ConstVec<T, MAX_SIZE>

Source

pub const fn new_with_max_size() -> Self

Create a new empty ConstVec with a custom maximum size.

§Example
const EMPTY: ConstVec<u8, 10> = ConstVec::new_with_max_size();
assert!(EMPTY.is_empty());
Source

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

Push a value onto the end of the ConstVec.

§Example
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec
};
assert_eq!(ONE.as_ref(), &[1]);
Source

pub const fn extend(&mut self, other: &[T])
where T: Copy,

Extend the ConstVec with the contents of a slice.

§Example
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.extend(&[1, 2, 3]);
    vec
};
assert_eq!(ONE.as_ref(), &[1, 2, 3]);
Source

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

Get a reference to the value at the given index.

§Example
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec
};
assert_eq!(ONE.get(0), Some(&1));
Source

pub const fn at(&self, index: usize) -> T
where T: Copy,

Get a copy of the value at the given index.

This panics if index is out of bounds.

Source

pub const fn len(&self) -> usize

Get the length of the ConstVec.

§Example
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec
};
assert_eq!(ONE.len(), 1);
Source

pub const fn is_empty(&self) -> bool

Check if the ConstVec is empty.

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
assert!(EMPTY.is_empty());
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec
};
assert!(!ONE.is_empty());
Source

pub const fn as_ref(&self) -> &[T]

Get a reference to the underlying slice.

§Example
const ONE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec
};
assert_eq!(ONE.as_ref(), &[1]);
Source

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

Get a reference to the underlying slice.

Source

pub const fn swap(&mut self, first: usize, second: usize)
where T: Copy,

Swap the values at the given indices.

§Example
const THREE: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec.swap(0, 1);
    vec
};
assert_eq!(THREE.as_ref(), &[2, 1]);
Source

pub const fn pop(&mut self) -> Option<T>
where T: Copy,

Pop a value off the end of the ConstVec.

§Example
const POPPED: (ConstVec<u8>, Option<u8>) = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec.push(3);
    let popped = vec.pop();
    (vec, popped)
};
assert_eq!(POPPED.0.as_ref(), &[1, 2]);
assert_eq!(POPPED.1.unwrap(), 3);
Source

pub const fn remove(&mut self, index: usize) -> Option<T>
where T: Copy,

Remove the value at the given index.

§Example
const REMOVED: (ConstVec<u8>, Option<u8>) = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec.push(3);
    let removed = vec.remove(1);
    (vec, removed)
};
assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
assert_eq!(REMOVED.1.unwrap(), 2);
Source

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

Set the value at the given index.

§Example
const TWO: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.set(0, 2);
    vec
};
assert_eq!(TWO.as_ref(), &[2]);
Source

pub const fn split_at(&self, index: usize) -> (Self, Self)
where T: Copy,

Split the ConstVec into two at the given index.

§Example
const SPLIT: (ConstVec<u8>, ConstVec<u8>) = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec.push(3);
    vec.split_at(1)
};
assert_eq!(SPLIT.0.as_ref(), &[1]);
assert_eq!(SPLIT.1.as_ref(), &[2, 3]);
Source§

impl<const MAX_SIZE: usize> ConstVec<u8, MAX_SIZE>

Source

pub const fn read(&self) -> ConstReadBuffer<'_>

Convert the ConstVec into a ConstReadBuffer.

§Example
const TWO: ConstVec<u8> = {
    let mut vec = ConstVec::new();
    vec.push(1);
    vec.push(2);
    vec
};
const READ: ConstReadBuffer = TWO.read();
assert_eq!(READ.as_ref(), &[1, 2]);

Trait Implementations§

Source§

impl<T: Clone, const MAX_SIZE: usize> Clone for ConstVec<T, MAX_SIZE>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Copy, const MAX_SIZE: usize> Copy for ConstVec<T, MAX_SIZE>

Source§

impl<T: Debug, const MAX_SIZE: usize> Debug for ConstVec<T, MAX_SIZE>

Source§

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

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

impl<T, const MAX_SIZE: usize> Default for ConstVec<T, MAX_SIZE>

Source§

fn default() -> Self

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

impl<T: Hash, const MAX_SIZE: usize> Hash for ConstVec<T, MAX_SIZE>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq, const MAX_SIZE: usize> PartialEq for ConstVec<T, MAX_SIZE>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T, const MAX_SIZE: usize> Freeze for ConstVec<T, MAX_SIZE>

§

impl<T, const MAX_SIZE: usize> RefUnwindSafe for ConstVec<T, MAX_SIZE>

§

impl<T, const MAX_SIZE: usize> Send for ConstVec<T, MAX_SIZE>
where [MaybeUninit<T>; MAX_SIZE]: Send,

§

impl<T, const MAX_SIZE: usize> Sync for ConstVec<T, MAX_SIZE>
where [MaybeUninit<T>; MAX_SIZE]: Sync,

§

impl<T, const MAX_SIZE: usize> Unpin for ConstVec<T, MAX_SIZE>
where [MaybeUninit<T>; MAX_SIZE]: Unpin,

§

impl<T, const MAX_SIZE: usize> UnsafeUnpin for ConstVec<T, MAX_SIZE>

§

impl<T, const MAX_SIZE: usize> UnwindSafe for ConstVec<T, MAX_SIZE>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.