Module nested_ref

Source
Expand description

Tools for chaining borrows of std::cell::RefCell.

Useful for returning an abstract borrow, where multiple inner borrows are required. e.g.:

use std::cell::RefCell;

struct MyPrivateType {
  x: RefCell<i32>
}

pub struct MyPublicType {
  inner: RefCell<MyPrivateType>
}

impl MyPublicType {
  pub fn borrow_x(&self) -> impl std::ops::Deref<Target=i32> + '_ {
    std_util::nested_ref::NestedRef::map(self.inner.borrow(), |inner| inner.x.borrow())
  }
}

Currently only supports nested RefCells; i.e. one level of nesting.

TODO: It might be feasible to genericize the reference types, which would also add support for arbitrary levels of nesting.

Structs§

NestedRef
A nested std::cell::Ref. Useful for chaining borrows with std::cell::RefCell.
NestedRefMut
A nested std::cell::Ref. Useful for chaining a mutable borrow of a std::cell::RefCell.