VecRef

Struct VecRef 

Source
pub struct VecRef<'a, T: ?Sized> { /* private fields */ }
Expand description

Wraps a borrowed reference from a VecCell.

When an instance of VecRef is created, the immutable borrow counter of its parent VecCell is incremented. Once that instance is Dropped, the immutable borrow counter is decremented.

This type implements Deref, and this is the main way to access the contained value:

let mut vec: VecCell<usize> = VecCell::new();
vec.push(2);
vec.push(15);

let vec_ref: VecRef<usize> = vec.borrow(0).unwrap();

let value: usize = *vec_ref; // equivalent to `vec_ref.deref()`

assert_eq!(value, 2);

Implementations§

Source§

impl<'a, T: ?Sized> VecRef<'a, T>

Source

pub fn map<'b, U: ?Sized, F>(original: VecRef<'b, T>, f: F) -> VecRef<'b, U>
where F: FnOnce(&T) -> &U,

Transforms a VecRef<'_, T> into a VecRef<'_, U> from a function that maps &T to &U.

This function does not use self and must be called explicitly via VecRef::map(value, function).

§Examples

This function comes in hand when you need to return a reference to a value in a VecCell from within a function/scope. For instance, the following is disallowed:

fn return_favorite_value<'a>(array: &'a VecCell<Vec<u8>>) -> &'a u8 {
    &array.get(42).unwrap().get()[7]
}

Instead, you would write it as follows:

fn return_favorite_value<'a>(array: &'a VecCell<Vec<u8>>) -> VecRef<'a, u8> {
    VecRef::map(array.borrow(42).unwrap(), |vec| &vec[7])
}
Source

pub fn try_map<'b, U: ?Sized, F, E>( original: VecRef<'b, T>, f: F, ) -> Result<VecRef<'b, U>, E>
where F: FnOnce(&T) -> Result<&U, E>,

Variant of VecRef::map, where the callback (f) may fail.

f must return a Result; if it returns Ok(x), then try_map returns Ok(VecRef(x)). Otherwise, it returns Err(err).

§Example
let vec: VecCell<Option<usize>> = VecCell::from(vec![Some(3), None]);

let ref_number: VecRef<Option<usize>> = vec.borrow(0).unwrap();
// Note: VecRef::try_map uses `Result`s, but we need `Option`s, so we convert to and from them
let ref_number: Option<VecRef<usize>> = VecRef::try_map(
    ref_number,
    |option| option.as_ref().ok_or(())
).ok();
assert!(ref_number.is_some());

let ref_none = vec.borrow(1).unwrap();
let ref_none = VecRef::try_map(ref_none, |option| option.as_ref().ok_or(())).ok();
assert!(ref_none.is_none());
Source§

impl<'a, T: Sized> VecRef<'a, [T]>

Source

pub fn borrow(&self, index: usize) -> Option<VecRef<'a, T>>

Returns an immutable borrow to the index-th element of the array. Returns None if index is out of bounds.

This method is only available for VecRef<[T]>.

§Example
let mut vec: VecCell<usize> = VecCell::with_capacity(10);
for x in 0..10 {
    vec.push(x);
}

let range = vec.borrow_range(2..5).unwrap();
assert_eq!(range.len(), 3);
let elem = range.borrow(2).unwrap(); // Corresponds to element 4 of `vec`
assert_eq!(elem, 4);

Trait Implementations§

Source§

impl<'a, T: Clone + ?Sized> Clone for VecRef<'a, T>

Source§

fn clone(&self) -> VecRef<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug + ?Sized> Debug for VecRef<'a, T>

Source§

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

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

impl<'a, T: ?Sized> Deref for VecRef<'a, T>

Source§

fn deref(&self) -> &Self::Target

Dereferences the VecRef, returning a reference to the borrowed value.

This cannot fail, as the borrowed value is already immutably borrowed.

Source§

type Target = T

The resulting type after dereferencing.
Source§

impl<'a, T: Display + ?Sized> Display for VecRef<'a, T>

Source§

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

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

impl<'a, T: PartialEq + ?Sized> PartialEq<T> for VecRef<'a, T>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: PartialEq + ?Sized> PartialEq for VecRef<'a, T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a, T> Freeze for VecRef<'a, T>
where T: ?Sized,

§

impl<'a, T> !RefUnwindSafe for VecRef<'a, T>

§

impl<'a, T> !Send for VecRef<'a, T>

§

impl<'a, T> !Sync for VecRef<'a, T>

§

impl<'a, T> Unpin for VecRef<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for VecRef<'a, T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.