pub struct NestedRef<'a, T, N>{ /* private fields */ }
Expand description
Shared reference to data contained in one or more nested RefCell
s.
This has the same interface as Ref
, with the additional methods
map_ref
and map_ref_mut
that form references
into nested RefCell
s.
§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, N> NestedRef<'a, T, N>
impl<'a, T, N> NestedRef<'a, T, N>
Sourcepub fn clone(orig: &Self) -> Self
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
.
Sourcepub fn filter_map<U, F>(orig: Self, f: F) -> Result<NestedRef<'a, U, N>, Self>
pub fn filter_map<U, F>(orig: Self, f: F) -> Result<NestedRef<'a, U, N>, Self>
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
.
Sourcepub fn map_split<U, V, F>(
orig: Self,
f: F,
) -> (NestedRef<'a, U, N>, NestedRef<'a, V, N>)
pub fn map_split<U, V, F>( orig: Self, f: F, ) -> (NestedRef<'a, U, N>, NestedRef<'a, V, N>)
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>
impl<'a, T, N> NestedRef<'a, T, N>
Sourcepub fn map_ref<U, F>(orig: Self, f: F) -> NestedRef<'a, U, Add1<N>>
pub fn map_ref<U, F>(orig: Self, f: F) -> NestedRef<'a, U, Add1<N>>
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);
Sourcepub fn map_ref_mut<U, F>(orig: Self, f: F) -> NestedRefMut<'a, U, Add1<N>>
pub fn map_ref_mut<U, F>(orig: Self, f: F) -> NestedRefMut<'a, U, Add1<N>>
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;