Struct NestedRef

Source
pub struct NestedRef<'a, T, N>
where T: ?Sized, N: ArrayLength<Ref<'a, ()>>,
{ /* private fields */ }
Expand description

Shared reference to data contained in one or more nested RefCells.

This has the same interface as Ref, with the additional methods map_ref and map_ref_mut that form references into nested RefCells.

§Examples

// Create a `RefCell` two levels deep and a `NestedRef` into it
let rc = RefCell::new(RefCell::new(Cell::new(0)));
let nr = NestedRef::new(rc.borrow());
let nr = NestedRef::map_ref(nr, RefCell::borrow);
assert_eq!(nr.get(), 0);

// Mutate through `NestedRef`
nr.set(1);
assert_eq!(nr.get(), 1);

// Mutate through independent `Ref`
rc.borrow().borrow().set(2);
assert_eq!(nr.get(), 2);

Implementations§

Source§

impl<'a, T> NestedRef<'a, T, U0>
where T: ?Sized,

Source

pub fn new(inner: Ref<'a, T>) -> Self

Creates a new reference inside a single RefCell

Source§

impl<'a, T, N> NestedRef<'a, T, N>
where T: ?Sized, N: ArrayLength<Ref<'a, ()>>,

Source

pub fn clone(orig: &Self) -> Self

Clones the reference, like Ref::clone.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

Source

pub fn map<U, F>(orig: Self, f: F) -> NestedRef<'a, U, N>
where U: ?Sized, F: FnOnce(&T) -> &U,

Creates a reference to a component of the borrowed data, like Ref::map.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

Source

pub fn filter_map<U, F>(orig: Self, f: F) -> Result<NestedRef<'a, U, N>, Self>
where U: ?Sized, F: FnOnce(&T) -> Option<&U>,

Creates a reference to an optional component of the borrowed data, like Ref::filter_map. The original reference is returned inside an Err if the closure returns None.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

Source

pub fn map_split<U, V, F>( orig: Self, f: F, ) -> (NestedRef<'a, U, N>, NestedRef<'a, V, N>)
where U: ?Sized, V: ?Sized, F: FnOnce(&T) -> (&U, &V),

Splits a reference into multiple references for different components of the borrowed data, like Ref::map_split.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

Source§

impl<'a, T, N> NestedRef<'a, T, N>
where T: ?Sized, N: ArrayLength<Ref<'a, ()>> + Add<B1>, <N as Add<B1>>::Output: ArrayLength<Ref<'a, ()>>, GenericArray<Ref<'a, ()>, N>: Lengthen<Ref<'a, ()>, Longer = GenericArray<Ref<'a, ()>, Add1<N>>>,

Source

pub fn map_ref<U, F>(orig: Self, f: F) -> NestedRef<'a, U, Add1<N>>
where U: ?Sized, F: FnOnce(&T) -> Ref<'_, U>,

Creates a shared reference to a component of the borrowed data that is contained in a nested RefCell.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

§Examples
let c = RefCell::new(('a', RefCell::new(2)));
let b1 = NestedRef::new(c.borrow());
let b2 = NestedRef::map_ref(b1, |t| t.1.borrow());
assert_eq!(*b2, 2);
Source

pub fn map_ref_mut<U, F>(orig: Self, f: F) -> NestedRefMut<'a, U, Add1<N>>
where U: ?Sized, F: FnOnce(&T) -> RefMut<'_, U>,

Creates an exclusive reference to a component of the borrowed data that is contained in a nested RefCell.

This is an associated function, because a method would interfere with methods of the same name on the contents of the RefCell.

§Examples
let c = RefCell::new(('a', RefCell::new(2)));
let b1 = NestedRef::new(c.borrow());
let mut b2 = NestedRef::map_ref_mut(b1, |t| t.1.borrow_mut());
assert_eq!(*b2, 2);
*b2 = 3;

Trait Implementations§

Source§

impl<'a, T, N> Deref for NestedRef<'a, T, N>
where T: ?Sized, N: ArrayLength<Ref<'a, ()>>,

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.

Auto Trait Implementations§

§

impl<'a, T, N> Freeze for NestedRef<'a, T, N>
where <N as ArrayLength<Ref<'a, ()>>>::ArrayType: Freeze, T: ?Sized,

§

impl<'a, T, N> !RefUnwindSafe for NestedRef<'a, T, N>

§

impl<'a, T, N> !Send for NestedRef<'a, T, N>

§

impl<'a, T, N> !Sync for NestedRef<'a, T, N>

§

impl<'a, T, N> Unpin for NestedRef<'a, T, N>
where <N as ArrayLength<Ref<'a, ()>>>::ArrayType: Unpin, T: ?Sized,

§

impl<'a, T, N> !UnwindSafe for NestedRef<'a, T, N>

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<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> Same for T

Source§

type Output = T

Should always be Self
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.