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>
impl<'a, T: ?Sized> VecRef<'a, T>
Sourcepub fn map<'b, U: ?Sized, F>(original: VecRef<'b, T>, f: F) -> VecRef<'b, U>
pub fn map<'b, U: ?Sized, F>(original: VecRef<'b, T>, f: F) -> VecRef<'b, 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])
}Sourcepub fn try_map<'b, U: ?Sized, F, E>(
original: VecRef<'b, T>,
f: F,
) -> Result<VecRef<'b, U>, E>
pub fn try_map<'b, U: ?Sized, F, E>( original: VecRef<'b, T>, f: F, ) -> Result<VecRef<'b, 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]>
impl<'a, T: Sized> VecRef<'a, [T]>
Sourcepub fn borrow(&self, index: usize) -> Option<VecRef<'a, T>>
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);