Struct vec_cell::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 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 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, Global>> 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, Global>> 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, Global>> for VecCell<T>

source§

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

Converts to this type from the input type.

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.