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

A circular array that allows infinite pushes into a fixed-size array.

Implementations§

source§

impl<const N: usize, T> CircularArray<N, T>
where T: Copy + Default + Debug,

source

pub fn new() -> Self

source

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

§example
    #[test]
    #[allow(non_snake_case)]
    fn test_Index_and_IndexMut() {
            let mut arr = CircularArray::<3, u32>::new();
            arr.push(0);
            arr.push(0);
            arr.push(0);
            arr.push(0);
            arr.push(0);
            arr[0] = 1;
            arr[1] = 2;
            arr[2] = 3;
            assert_eq!(arr[0], 1);
            assert_eq!(arr[1], 2);
            assert_eq!(arr[2], 3);
        }
source

pub fn to_array(&self) -> [T; N]

§Examples
    #[test]
    fn test_to_array() {
        let mut arr = CircularArray::<3, u32>::new();
        arr.push(1);
        arr.push(2);
        arr.push(3);
        assert_eq!(arr.to_array(), [1, 2, 3]);
        arr.push(4);
        assert_eq!(arr.to_array(), [2, 3, 4]);
    }

Trait Implementations§

source§

impl<const N: usize, T: Debug> Debug for CircularArray<N, T>

source§

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

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

impl<T, const N: usize> Index<usize> for CircularArray<N, T>
where [T]: Index<usize>, T: Default + Copy,

§

type Output = <[T] as Index<usize>>::Output

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 CircularArray<N, T>
where [T]: Index<usize>, T: Default + Copy, usize: Add<usize>,

source§

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

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

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>,

§

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.