pub struct MutRc<T>(_);Expand description
A temporarily-mutable version of Rc.
MutRc<T> is essentially equivalent to Rc<RefCell<T>> except that it can be “finalized” into an Rc<T> once mutation is no longer needed.
This operation preserves the original aliasing topology, and is useful for initializing aliasing structures
that initially need mutability, but later can be converted to an immutable form.
All operations on MutRc are guaranteed to not panic.
Implementations§
source§impl<T> MutRc<T>
impl<T> MutRc<T>
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new, unaliased instance of MutRc<T> with the given initial value.
sourcepub fn with<U, F: FnOnce(&T) -> U>(&self, f: F) -> Result<U, BorrowError>
pub fn with<U, F: FnOnce(&T) -> U>(&self, f: F) -> Result<U, BorrowError>
Accesses the shared value immutably, optionally returning the result of the callback.
This operation can fail if the shared value is already borrowed mutably (i.e., if called from within MutRc::with_mut on the same shared value).
sourcepub fn with_mut<U, F: FnOnce(&mut T) -> U>(
&self,
f: F
) -> Result<U, MutateError>
pub fn with_mut<U, F: FnOnce(&mut T) -> U>( &self, f: F ) -> Result<U, MutateError>
Accesses the shared value mutably, optionally returning the result of the callback.
This operation can fail if the shared value is already borrowed (i.e., if called from within MutRc::with or MutRc::with_mut on the same shared value),
or if there exists an aliasing Rc<T> returned by MutRc::finalize on the same shared value.
If recursion is needed, but mutation is not, consider using MutRc::with instead.
sourcepub fn finalize(&self) -> Result<Rc<T>, BorrowError>
pub fn finalize(&self) -> Result<Rc<T>, BorrowError>
Finalizes the value into an (immutable) aliasing instance of Rc<T>.
While this aliasing Rc<T> exists, all subsequent calls to MutRc::with_mut on the same shared value will fail.
This operation can fail if the shared value is already borrowed mutably (i.e., if called from within MutRc::with_mut on the same shared value).