pub struct VecCell<T> { /* private fields */ }Expand description
A Vec with interior mutability and dynamically checked borrow rules
when interacting with its elements.
Implementations§
Source§impl<T> VecCell<T>
impl<T> VecCell<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty VecCell.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::new();Sourcepub fn borrow(&self, index: usize) -> ElementRef<'_, T>
pub fn borrow(&self, index: usize) -> ElementRef<'_, T>
Immutably borrows element with specified index.
The borrow lasts until the returned ElementRef exits the scope.
During this period any number of immutable borrows can be obtained but no mutable ones.
§Panics
Panics if the element is already borrowed mutably or index is out of bounds.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
assert_eq!(*vec_cell.borrow(0), 0);
assert_eq!(*vec_cell.borrow(1), 1);
assert_eq!(*vec_cell.borrow(2), 2);Sourcepub fn try_borrow(&self, index: usize) -> Result<ElementRef<'_, T>, BorrowError>
pub fn try_borrow(&self, index: usize) -> Result<ElementRef<'_, T>, BorrowError>
Immutably borrows an element with specified index, returns an error if the element is already borrowed mutably or its index is out of bounds.
The borrow lasts until the returned ElementRef exits the scope.
During this period any number of immutable borrows can be obtained but no mutable ones.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0]);
{
let borrow = vec_cell.try_borrow(0);
assert!(vec_cell.try_borrow(0).is_ok());
}
{
assert!(vec_cell.try_borrow(10).is_err());
}
{
let borrow_mut = vec_cell.try_borrow_mut(0);
assert!(vec_cell.try_borrow(0).is_err())
}Sourcepub unsafe fn borrow_unchecked(&self, index: usize) -> &T
pub unsafe fn borrow_unchecked(&self, index: usize) -> &T
Returns a reference to an element, without doing bounds and aliasing checking.
§Safety
Calling this method with an out-of-bounds index or if the element is already boroowed mutably is undefined behavior.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
// let borrow_mut_unchecked = vec_cell.borrow_mut_unchecked(1); <--- UB
let borrow_unchecked = unsafe { vec_cell.borrow_unchecked(1) };
assert_eq!(borrow_unchecked, &1);
Sourcepub fn borrow_mut(&self, index: usize) -> ElementRefMut<'_, T>
pub fn borrow_mut(&self, index: usize) -> ElementRefMut<'_, T>
Mutably borrows element with specified index.
The borrow lasts until the returned ElementRefMut exits the scope.
During this period no borrows can be obtained.
§Panics
Panics if the element is already borrowed or index is out of bounds.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0]);
let mut borrow_mut = vec_cell.borrow_mut(0);
*borrow_mut = 42;
assert_eq!(*borrow_mut, 42);Sourcepub fn try_borrow_mut(
&self,
index: usize,
) -> Result<ElementRefMut<'_, T>, BorrowError>
pub fn try_borrow_mut( &self, index: usize, ) -> Result<ElementRefMut<'_, T>, BorrowError>
Mutably borrows an element with specified index, returns an error if the element is already borrowed or its index is out of bounds.
The borrow lasts until the returned ElementRefMut exits the scope.
During this period no borrows can be obtained.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0]);
{
assert!(vec_cell.try_borrow_mut(0).is_ok());
assert!(vec_cell.try_borrow_mut(10).is_err())
}
{
let borrow = vec_cell.try_borrow(0);
assert!(vec_cell.try_borrow_mut(0).is_err())
}Sourcepub unsafe fn borrow_mut_unchecked(&self, index: usize) -> &mut T
pub unsafe fn borrow_mut_unchecked(&self, index: usize) -> &mut T
Returns a mutable reference to an element, without doing bounds and aliasing checking.
§Safety
Calling this method with an out-of-bounds index or if the element is already boroowed is undefined behavior.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
// let borrow_unchecked = vec_cell.borrow_unchecked(1); <--- UB
let borrow_mut_unchecked = unsafe { vec_cell.borrow_mut_unchecked(1) };
assert_eq!(borrow_mut_unchecked, &mut 1);
*borrow_mut_unchecked = 2;
assert_eq!(borrow_mut_unchecked, &mut 2);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in VecCell.
§Examples
use vec_cell::VecCell;
let vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
assert_eq!(vec_cell.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if VecCell contains no elements.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::new();
assert!(vec_cell.is_empty());
vec_cell.push(0);
assert!(!vec_cell.is_empty());Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Appends an element to the back of a VecCell.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::new();
vec_cell.push(0);
assert_eq!(*vec_cell.borrow(0), 0);Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from a VecCell and returns it, or None if it is empty.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
assert_eq!(vec_cell.pop(), Some(2));
assert_eq!(vec_cell.pop(), Some(1));
assert_eq!(vec_cell.pop(), Some(0));
assert_eq!(vec_cell.pop(), None);Sourcepub fn append(&mut self, other: &mut VecCell<T>)
pub fn append(&mut self, other: &mut VecCell<T>)
Move all elements from other to self.
§Examples
use vec_cell::VecCell;
let mut vec_cell1: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
let mut vec_cell2: VecCell<i32> = VecCell::from(vec![3, 4, 5]);
vec_cell1.append(&mut vec_cell2);
for i in 0..6 {
assert_eq!(*vec_cell1.borrow(i), i as i32);
}Sourcepub fn append_vec(&mut self, vec: &mut Vec<T>)
pub fn append_vec(&mut self, vec: &mut Vec<T>)
Move all elements from vec to self.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2]);
let mut vec: Vec<i32> = vec![3, 4, 5];
vec_cell.append_vec(&mut vec);
for i in 0..6 {
assert_eq!(*vec_cell.borrow(i), i as i32);
}Sourcepub fn swap_remove(&mut self, index: usize) -> T
pub fn swap_remove(&mut self, index: usize) -> T
Removes and returns the element at position index.
The removed element is replaced by the last element.
§Panics
Panics if index is out of bounds.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2, 3, 4, 5]);
let removed_element = vec_cell.swap_remove(1);
assert_eq!(removed_element, 1);
assert_eq!(*vec_cell.borrow(1), 5);Sourcepub fn replace(&self, index: usize, value: T) -> T
pub fn replace(&self, index: usize, value: T) -> T
Replaces and returns the element at position index with value.
§Panics
Panics if index is out of bounds or element at position index is already borrowed.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2, 3, 4, 5]);
let replaced_element = vec_cell.replace(1, 10);
assert_eq!(replaced_element, 1);
assert_eq!(*vec_cell.borrow(1), 10);Source§impl<T: Default> VecCell<T>
impl<T: Default> VecCell<T>
Sourcepub fn take(&self, index: usize) -> T
pub fn take(&self, index: usize) -> T
Takes the element at position index and replaces it with Default value.
§Panics
Panics if index is out of bounds or element at position index is already borrowed.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2, 3, 4, 5]);
let taken_element = vec_cell.take(1);
assert_eq!(taken_element, 1);
assert_eq!(*vec_cell.borrow(1), 0);Sourcepub fn try_take(&self, index: usize) -> Result<T, BorrowError>
pub fn try_take(&self, index: usize) -> Result<T, BorrowError>
Takes the element at position index and replaces it with Default value.
If index is out of bounds or element at position index
is already borrowed, returns an error.
§Examples
use vec_cell::VecCell;
let mut vec_cell: VecCell<i32> = VecCell::from(vec![0, 1, 2, 3, 4, 5]);
{
let taken_element = vec_cell.try_take(1);
assert_eq!(taken_element.unwrap(), 1);
assert_eq!(*vec_cell.borrow(1), 0);
}
{
let taken_element = vec_cell.try_take(10);
assert!(taken_element.is_err());
}
{
let borrow_mut = vec_cell.borrow_mut(2);
let taken_element = vec_cell.try_take(2);
assert!(taken_element.is_err());
}