Struct VecArray

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

A Vec but entirely on the stack.

§Example

use vector_array::vec::VecArray;

let mut vec: VecArray<_, 10> = VecArray::new();
vec.push(9).unwrap();
assert_eq!(vec[0], 9);

Implementations§

Source§

impl<T, const CAP: usize> VecArray<T, CAP>
where T: Default,

Source

pub fn new() -> Self

Initializes all elements with defaults (does not increment length)

§Example
use vector_array::vec::VecArray;

let mut vec: VecArray<_, 10> = VecArray::new();
vec.push(9).unwrap();
assert_eq!(vec[0], 9);

Use ::new_no_default if type doesn’t implement default

Source§

impl<T, const CAP: usize> VecArray<T, CAP>

Source

pub fn new_no_default() -> Self

Creates a new VecArray. Use ::new if type has default especially if type contains pointers/references (think String, Box, etc)

§Example
use vector_array::vec::VecArray;

let mut vec: VecArray<_, 10> = VecArray::new_no_default();
vec.push(9).unwrap();
assert_eq!(vec[0], 9);
§Safety

There may be problems with drops if your type contains references for example. There also may be problems if you try to index in to parts of the array which are no yet initialized but this is nearly impossible.

Source

pub fn new_arr(arr: [T; CAP], len: usize) -> Self

Creates a new VecArray. Use when type doesnt implement default and (drop) safety is a problem.

Source

pub fn push(&mut self, value: T) -> Result<(), ArrTooSmall>

Pushes an element.

§Example
use vector_array::vec::VecArray;

let mut vec: VecArray<_, 10> = VecArray::new();
vec.push(9).unwrap();
assert_eq!(vec[0], 9);
Source

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

Removes the last element

§Example
use vector_array::vec::VecArray;

let mut vec: VecArray<_, 10> = VecArray::new();
vec.push(9).unwrap();
assert_eq!(vec.pop(), Some(9));
§Safety

Returns memory which will realistically wont be used anymore

Source

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

Removes an element.

§Panics

If index is greater than or equal to length

§Example
use vector_array::vec::VecArray;
let mut vec: VecArray<_, 10> = VecArray::new();
vec.push(9).unwrap();
vec.remove(0);
assert!(vec.is_empty());
§Safety

Copied from Vec source code

Source

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

§Panics

If index is greater than or equal to length or new length is greater than CAP

§Example
use vector_array::{vec_arr, VecArray};

let mut vec: VecArray<_, 10> = vec_arr![1, 2, 3];
vec.insert(1, 4);
assert_eq!(vec, vec_arr![1, 4, 2, 3]);
vec.insert(2, 5);
assert_eq!(vec, vec_arr![1, 4, 5, 2, 3]);
§Safety

Copied from Vec source code

Source

pub fn swap(&mut self, index1: usize, index2: usize)

Swaps two elements in the vec.

§Panics

Panics if one of the indexes are out of bounds.

§Examples
use vector_array::{vec_arr, VecArray};

let mut vec: VecArray<_, 10> = vec_arr![0, 1, 2, 3];
vec.swap(2, 3);
assert_eq!(vec, vec_arr![0, 1, 3, 2]);
Source

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

Source

pub fn set(&mut self, index: usize, value: T) -> Result<(), ArrTooSmall>

Source

pub fn truncate(&mut self, len: usize)

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub unsafe fn get_arr(self) -> [T; CAP]

Returns the entire array

§Safety

Can point to uninitialized memory, causes a segfault if memory is not properly initialized

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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

Source

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

Source

pub fn clear(&mut self)

Source

pub fn capacity(&self) -> usize

Trait Implementations§

Source§

impl<T: Clone, const CAP: usize> Clone for VecArray<T, CAP>

Source§

fn clone(&self) -> VecArray<T, CAP>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, const CAP: usize> Debug for VecArray<T, CAP>
where T: Debug,

Source§

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

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

impl<T, const CAP: usize> Default for VecArray<T, CAP>
where T: Default,

Does the same as ::new

Source§

fn default() -> Self

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

impl<T, const CAP: usize> From<Vec<T>> for VecArray<T, CAP>
where T: Default,

Source§

fn from(value: Vec<T>) -> Self

§Panics

If inputs length is greater than CAP

Source§

impl<T, const CAP: usize> From<VecArray<T, CAP>> for Vec<T>

Source§

fn from(val: VecArray<T, CAP>) -> Self

Converts to this type from the input type.
Source§

impl<T, const CAP: usize> Index<usize> for VecArray<T, CAP>

Source§

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

§Panics

If index is greater than or equal to length

Use .get instead

Source§

type Output = T

The returned type after indexing.
Source§

impl<T, const CAP: usize> IndexMut<usize> for VecArray<T, CAP>

Source§

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

§Panics

If index is greater than or equal to length

Use .set instead

Source§

impl<T, const CAP: usize> IntoIterator for VecArray<T, CAP>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<VecArray<T, CAP> as IntoIterator>::Item, CAP>

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<T, const CAP: usize> PartialEq for VecArray<T, CAP>
where T: PartialEq,

Source§

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

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

const 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 CAP: usize> Freeze for VecArray<T, CAP>
where T: Freeze,

§

impl<T, const CAP: usize> RefUnwindSafe for VecArray<T, CAP>
where T: RefUnwindSafe,

§

impl<T, const CAP: usize> Send for VecArray<T, CAP>
where T: Send,

§

impl<T, const CAP: usize> Sync for VecArray<T, CAP>
where T: Sync,

§

impl<T, const CAP: usize> Unpin for VecArray<T, CAP>
where T: Unpin,

§

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