pub struct MutRc<T>(/* private fields */);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).
Sourcepub fn get(&self) -> Result<T, BorrowError>where
T: Copy,
pub fn get(&self) -> Result<T, BorrowError>where
T: Copy,
Gets a copy of the currently stored value.
Sourcepub fn get_clone(&self) -> Result<T, BorrowError>where
T: Clone,
pub fn get_clone(&self) -> Result<T, BorrowError>where
T: Clone,
Gets a clone of the currently stored value.
Sourcepub fn take(&self) -> Result<T, MutateError>where
T: Default,
pub fn take(&self) -> Result<T, MutateError>where
T: Default,
Takes the currently stored value and replaces it with the default value.
Sourcepub fn replace(&self, value: T) -> Result<T, MutateError>
pub fn replace(&self, value: T) -> Result<T, MutateError>
Replaces the currently stored value and returns the previous value.
Sourcepub fn set(&self, value: T) -> Result<(), MutateError>
pub fn set(&self, value: T) -> Result<(), MutateError>
Sets the currently stored value.