VecCell

Struct VecCell 

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

Source

pub fn new() -> Self

Creates a new empty VecCell.

§Examples
use vec_cell::VecCell;

let vec_cell: VecCell<i32> = VecCell::new();
Source

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);
Source

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())
}
Source

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);
 
Source

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);
Source

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())
}
Source

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);
 
Source

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);
Source

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());
Source

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);
Source

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);
Source

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);
}
Source

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);
}
Source

pub fn insert(&mut self, index: usize, element: T)

Insert an element at posiion index.

§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]);

vec_cell.insert(1, 3);
assert_eq!(*vec_cell.borrow(1), 3);
Source

pub fn remove(&mut self, index: usize) -> T

Removes and returns the element at position index.

§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]);

let removed_element = vec_cell.remove(1);
assert_eq!(removed_element, 1);
Source

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);
Source

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>

Source

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);
Source

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());
}

Trait Implementations§

Source§

impl<T: Debug> Debug for VecCell<T>

Source§

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

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

impl<T> Default for VecCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Clone> From<&[T]> for VecCell<T>

Source§

fn from(data: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone> From<&Vec<T>> for VecCell<T>

Source§

fn from(data: &Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone> From<&mut [T]> for VecCell<T>

Source§

fn from(data: &mut [T]) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone> From<&mut Vec<T>> for VecCell<T>

Source§

fn from(data: &mut Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for VecCell<T>

Source§

fn from(data: Vec<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for VecCell<T>

§

impl<T> !RefUnwindSafe for VecCell<T>

§

impl<T> Send for VecCell<T>
where T: Send,

§

impl<T> !Sync for VecCell<T>

§

impl<T> Unpin for VecCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for VecCell<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.