Struct CircularArray

Source
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 + Display,

Source

pub fn new() -> Self

Source

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

§example
use circular_array::CircularArray;
#[test]
fn test_push() {
    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]);
}
Source

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

§Examples
    use circular_array::CircularArray;
#[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]);
    }
Source

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

§example
use circular_array::CircularArray;
let mut arr = CircularArray::<3, u32>::new();
arr.push(1);
arr.push(2);
arr.push(3);
let mut iter: circular_array::iter::CircularArrayIter<3, u32> = arr.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), None);
Source

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

§Example
use circular_array::CircularArray;
#[test]
fn test_last() {
    let mut arr = CircularArray::<3, u32>::new();
    assert_eq!(arr.last(), None);
    arr.push(1);
    assert_eq!(arr.last(), Some(1).as_ref());
    arr.push(2);
    arr.push(3);
    arr.push(4);
    assert_eq!(arr.last(), Some(4).as_ref());
}
Source

pub fn len(&self) -> usize

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,

Source§

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

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.