const_serialize

Struct ConstVec

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

ConstVec is a version of Vec that is usable in const contexts. It has a fixed maximum size, but it can can grow and shrink within that size limit as needed.

§Example

const EMPTY: ConstVec<u8> = ConstVec::new();
// Methods that mutate the vector will return a new vector
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const FOUR: ConstVec<u8> = THREE.push(4);
// If a value is also returned, that will be placed in a tuple in the return value
// along with the new vector
const POPPED: (ConstVec<u8>, Option<u8>) = FOUR.pop();
assert_eq!(POPPED.0, THREE);
assert_eq!(POPPED.1.unwrap(), 4);

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();
Source

pub const fn push(self, value: T) -> Self

Push a value onto the end of the ConstVec

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

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

Extend the ConstVec with the contents of a slice

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.extend(&[1, 2, 3]);
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 EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
assert_eq!(ONE.get(0), Some(&1));
Source

pub const fn len(&self) -> usize

Get the length of the ConstVec

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
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> = EMPTY.push(1);
assert!(!ONE.is_empty());
Source

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

Get a reference to the underlying slice

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

pub const fn swap(self, first: usize, second: usize) -> Self
where T: Copy,

Swap the values at the given indices

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.swap(0, 1);
assert_eq!(THREE.as_ref(), &[2, 1]);
Source

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

Pop a value off the end of the ConstVec

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const POPPED: (ConstVec<u8>, Option<u8>) = THREE.pop();
assert_eq!(POPPED.0, TWO);
assert_eq!(POPPED.1.unwrap(), 3);
Source

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

Remove the value at the given index

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const REMOVED: (ConstVec<u8>, Option<u8>) = THREE.remove(1);
assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
assert_eq!(REMOVED.1.unwrap(), 2);
Source

pub const fn set(self, index: usize, value: T) -> Self

Set the value at the given index

§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.set(0, 2);
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 EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const SPLIT: (ConstVec<u8>, ConstVec<u8>) = THREE.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 EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const READ: ConstReadBuffer = TWO.read();

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a copy 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 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

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: Copy, const MAX_SIZE: usize> Copy for ConstVec<T, MAX_SIZE>

Auto Trait Implementations§

§

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

§

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

§

impl<T, const MAX_SIZE: usize> Send for ConstVec<T, MAX_SIZE>
where T: Send,

§

impl<T, const MAX_SIZE: usize> Sync for ConstVec<T, MAX_SIZE>
where T: Sync,

§

impl<T, const MAX_SIZE: usize> Unpin for ConstVec<T, MAX_SIZE>
where T: Unpin,

§

impl<T, const MAX_SIZE: usize> UnwindSafe for ConstVec<T, MAX_SIZE>
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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.