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>
impl<T, const CAP: usize> ArraySetCell<T, CAP>
Sourcepub const CAPACITY: usize = CAP
pub const CAPACITY: usize = CAP
The capacity of the ArraySetCell.
§Example
use arraysetcell::ArraySetCell;
assert_eq!(ArraySetCell::<u8, 16>::CAPACITY, 16);Sourcepub fn new() -> Self
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]);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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);Sourcepub const fn capacity(&self) -> usize
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);Sourcepub fn is_full(&self) -> bool
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());Sourcepub fn remaining_capacity(&self) -> usize
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);Sourcepub fn push(&mut self, element: T)
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]);Sourcepub fn try_push(&mut self, element: T) -> Result<(), CapacityError<T>>
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]);Sourcepub fn clear(&self)
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, &[]);Sourcepub fn pop(&mut self) -> Option<T>
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);Sourcepub fn into_vec(self) -> Vec<T>
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);Sourcepub fn filter_mut<F, O>(&mut self, f: F) -> Option<O>
pub fn filter_mut<F, O>(&mut self, f: F) -> 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; returnSome(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));Sourcepub fn as_mut_ptr(&mut self) -> *mut Option<T>
pub fn as_mut_ptr(&mut self) -> *mut Option<T>
Return a raw mutable pointer to the set’s buffer.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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>
impl<T, const CAP: usize> Debug for ArraySetCell<T, CAP>
Source§impl<T, const CAP: usize> Default for ArraySetCell<T, CAP>
impl<T, const CAP: usize> Default for ArraySetCell<T, CAP>
Source§fn default() -> Self
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>
impl<T, const CAP: usize> From<[Cell<Option<T>>; CAP]> for ArraySetCell<T, CAP>
Source§fn from(value: [Cell<Option<T>>; CAP]) -> Self
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<ArraySetCell<T, CAP>> for [Cell<Option<T>>; CAP]
impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for [Cell<Option<T>>; CAP]
Source§fn from(value: ArraySetCell<T, CAP>) -> Self
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]
impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for [Option<T>; CAP]
Source§fn from(value: ArraySetCell<T, CAP>) -> Self
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>>
impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for Vec<Option<T>>
Source§fn from(value: ArraySetCell<T, CAP>) -> Self
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>
impl<T, const CAP: usize> From<ArraySetCell<T, CAP>> for Vec<T>
Source§fn from(value: ArraySetCell<T, CAP>) -> Self
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>
impl<T, const CAP: usize> IntoIterator for ArraySetCell<T, CAP>
Source§fn into_iter(self) -> Self::IntoIter
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);