MutRc

Struct MutRc 

Source
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>

Source

pub fn new(value: T) -> Self

Creates a new, unaliased instance of MutRc<T> with the given initial value.

Source

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).

Source

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.

Source

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).

Source

pub fn get(&self) -> Result<T, BorrowError>
where T: Copy,

Gets a copy of the currently stored value.

Source

pub fn get_clone(&self) -> Result<T, BorrowError>
where T: Clone,

Gets a clone of the currently stored value.

Source

pub fn take(&self) -> Result<T, MutateError>
where T: Default,

Takes the currently stored value and replaces it with the default value.

Source

pub fn replace(&self, value: T) -> Result<T, MutateError>

Replaces the currently stored value and returns the previous value.

Source

pub fn set(&self, value: T) -> Result<(), MutateError>

Sets the currently stored value.

Source

pub fn ptr_eq(this: &MutRc<T>, other: &MutRc<T>) -> bool

Checks if two instances of MutRc<T> are aliases to the same value.

Source

pub fn downgrade(this: &Self) -> MutWeak<T>

Downgrades this MutRc into a MutWeak.

Trait Implementations§

Source§

impl<T> Clone for MutRc<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for MutRc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for MutRc<T>

Source§

fn default() -> MutRc<T>

Returns the “default value” for a type. Read more
Source§

impl<T> From<T> for MutRc<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for MutRc<T>

§

impl<T> !RefUnwindSafe for MutRc<T>

§

impl<T> !Send for MutRc<T>

§

impl<T> !Sync for MutRc<T>

§

impl<T> Unpin for MutRc<T>

§

impl<T> !UnwindSafe for MutRc<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.