ArraySetCell

Struct ArraySetCell 

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

ArraySetCell is a fixed-capacity, vector-like array with interior mutability and no ordering guarantees.

Implementations§

Source§

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

Source

pub const CAPACITY: usize = CAP

The capacity of the ArraySetCell.

§Example
use arraysetcell::ArraySetCell;

assert_eq!(ArraySetCell::<u8, 16>::CAPACITY, 16);
Source

pub fn new() -> Self

Create a new empty ArraySetCell.

The maximum capacity is given by the generic parameter CAP.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 16>::new();
array.push(1);
array.push(2);
assert_eq!(array.capacity(), 16);
assert_eq!(array.into_vec(), &[1, 2]);
Source

pub fn len(&self) -> usize

Return the number of elements in the ArraySetCell.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::from([1, 2, 3]);
array.pop();
assert_eq!(array.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns whether the ArraySetCell is empty.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::from([1]);
array.pop();
assert_eq!(array.is_empty(), true);
Source

pub const fn capacity(&self) -> usize

Return the capacity of the ArraySetCell.

§Example
use arraysetcell::ArraySetCell;

let array = ArraySetCell::from([1, 2, 3]);
assert_eq!(array.capacity(), 3);
Source

pub fn is_full(&self) -> bool

Return true if the ArraySetCell is completely filled to its capacity, false otherwise.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 1>::new();
assert!(!array.is_full());
array.push(1);
assert!(array.is_full());
Source

pub fn remaining_capacity(&self) -> usize

Returns the capacity left in the ArraySetCell.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::from([1, 2, 3]);
array.pop();
assert_eq!(array.remaining_capacity(), 1);
Source

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

Push element into the set.

The order of insertions is not guaranteed, i.e. the last item pushed may not be the first item popped.

§Panics

Panic if the vector is already full. If you need to avoid panics, use try_push instead.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 2>::new();

array.push(1);
array.push(2);

assert_eq!(array.into_vec(), &[1, 2]);
Source

pub fn try_push(&mut self, element: T) -> Result<(), CapacityError<T>>

Push element into the set.

The order of insertions is not guaranteed, i.e. the last item pushed may not be the first item popped.

Unlike ArraySetCell::push, this method does not panic.

use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 2>::new();
let push1 = array.try_push(1);
let push2 = array.try_push(2);
let push3 = array.try_push(3); // overflow

assert!(push1.is_ok());
assert!(push2.is_ok());
assert!(push3.is_err());

assert_eq!(array.into_vec(), &[1, 2]);
Source

pub fn clear(&self)

Remove all elements in the vector.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
assert_eq!(array.len(), 2);

array.clear();
assert!(array.is_empty());
assert_eq!(array.len(), 0);

let vec = array.into_vec();
assert_eq!(vec, &[]);
Source

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

Remove the last element in the vector and return it.

Return Some( element ) if the vector is non-empty, else None.

use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 2>::new();

array.push(1);

assert_eq!(array.pop(), Some(1));
assert_eq!(array.pop(), None);
Source

pub fn into_vec(self) -> Vec<T>

Converts the ArraySetCell into a Vec<T>.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
let vec = array.into_vec();

assert_eq!(vec, &[1, 3]);
assert_eq!(vec.capacity(), 2);
Source

pub fn filter_mut<F, O>(&mut self, f: F) -> Option<O>
where F: FnMut(&mut T) -> Option<O>,

Filters elements in the list, returning either Some(result) or None to continue searching. The element may be mutated in place.

§Arguments
  • f - The filter function. May mutate the result; return Some(result) to terminate the search.
§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
let result = array.filter_mut(|x| if *x > 2 { Some(*x) } else { None });

assert_eq!(result, Some(3));
Source

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

Return a raw pointer to the set’s buffer.

Source

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

Return a raw mutable pointer to the set’s buffer.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&mut T) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all elements e such that f(&mut e) returns false. This method operates in place and preserves the order of the retained elements.

use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::from([1, 2, 3, 4, 11, 20]);
array.retain(|x| *x & 1 != 0 );
assert_eq!(array.into_vec(), &[1, 3, 11]);

Trait Implementations§

Source§

impl<T, const CAP: usize> Debug for ArraySetCell<T, CAP>

Source§

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

Creates a debug string representation of the array.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 16>::default();
array.push(1);
array.push(2);
assert_eq!(format!("{:?}", array), "len=2 cap=16");
Source§

impl<T, const CAP: usize> Default for ArraySetCell<T, CAP>

Source§

fn default() -> Self

Create a new empty ArraySetCell.

The maximum capacity is given by the generic parameter CAP.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<_, 16>::default();
array.push(1);
array.push(2);
assert_eq!(array.capacity(), 16);
assert_eq!(array.into_vec(), &[1, 2]);
Source§

impl<T, const CAP: usize> From<[Cell<Option<T>>; CAP]> for ArraySetCell<T, CAP>

Source§

fn from(value: [Cell<Option<T>>; CAP]) -> Self

Constructs an ArraySetCell from an array of Cell<Option<T>>.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::<u32, 3>::from([
    Cell::new(Some(1)),
    Cell::new(None),
    Cell::new(Some(3))
]);
assert_eq!(array.len(), 2);
Source§

impl<T, const CAP: usize> From<[Option<T>; CAP]> for ArraySetCell<T, CAP>

Source§

fn from(value: [Option<T>; CAP]) -> Self

Constructs an ArraySetCell from an array of Option<T>.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
assert_eq!(array.len(), 2);
Source§

impl<T, const CAP: usize> From<[T; CAP]> for ArraySetCell<T, CAP>

Source§

fn from(value: [T; CAP]) -> Self

Constructs an ArraySetCell from an array of T.

§Example
use arraysetcell::ArraySetCell;

let mut array = ArraySetCell::from([1, 2, 3]);
assert_eq!(array.len(), 3);
Source§

impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for [Cell<Option<T>>; CAP]

Source§

fn from(value: ArraySetCell<T, CAP>) -> Self

Converts an ArraySetCell into an [Cell<Option<T>>; N].

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::from([Some(1), None, Some(3)]);
let into: [Cell<Option<u32>>; 3] = array.into();

assert_eq!(into, [Cell::new(Some(1)), Cell::new(Some(3)), Cell::new(None)]);
Source§

impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for [Option<T>; CAP]

Source§

fn from(value: ArraySetCell<T, CAP>) -> Self

Converts an ArraySetCell into an [Option<T>; N].

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::from([Some(1), None, Some(3)]);
let into: [Option<u32>; 3] = array.into();

assert_eq!(into, [Some(1), Some(3), None]);
Source§

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

Source§

fn from(value: ArraySetCell<T, CAP>) -> Self

Converts an ArraySetCell into a Vec<Option<T>>.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
let vec: Vec<Option<u32>> = array.into();

assert_eq!(vec, &[Some(1), Some(3), None]);
assert_eq!(vec.capacity(), 3);
Source§

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

Source§

fn from(value: ArraySetCell<T, CAP>) -> Self

Converts an ArraySetCell into a Vec<T>.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
let vec: Vec<u32> = array.into();

assert_eq!(vec, &[1, 3]);
assert_eq!(vec.capacity(), 2);
Source§

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

Source§

fn into_iter(self) -> Self::IntoIter

Returns an iterator.

§Example
use std::cell::Cell;
use arraysetcell::ArraySetCell;

let array = ArraySetCell::<u32, 3>::from([Some(1), None, Some(3)]);
let mut iter = array.into_iter();

assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = ArraySetCellIntoIter<T, CAP>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

impl<T, const CAP: usize> !Freeze for ArraySetCell<T, CAP>

§

impl<T, const CAP: usize> !RefUnwindSafe for ArraySetCell<T, CAP>

§

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

§

impl<T, const CAP: usize> !Sync for ArraySetCell<T, CAP>

§

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

§

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