Struct yewtil::ptr::Mrc[][src]

pub struct Mrc<T> { /* fields omitted */ }
Expand description

Mutable Reference Counted pointer

The Mrc has similar semantics to std::rc::Rc pointer, with notable differences that it does not support Weak pointers, it supports std::ops::DerefMut via the possibly allocating make_mut function, and that it can create immutable handles to its data (Irc).

This should make it just slightly more size efficient and performant than Rc, and should be more ergonomic to use than Rc given that you can mutably assign to it without much ceremony.

Passing Irc pointers to children guarantee that no intermediate component can modify the value behind the pointer. This makes it ideal for passing around configuration data where some components can ergonomicly “modify” and cheaply pass the pointers back to parent components, while other components can only read it.

Note

Assigning to an Mrc within Yew when you have passed shared copies of the ptr to child components, will always end up cloning the value stored in the Mrc. Rc makes this performance cost explicit, by making you use Rc::make_mut(). Because cloning is unavoidable in the context it was designed for, Mrc opts to provide nicer ergonomics around assignment.

Example

use yewtil::ptr::Mrc;

let mut mrc = Mrc::new(5);
*mrc = 10; // This just replaces the value because the mrc isn't shared.

assert_eq!(*mrc, 10);

let clone = mrc.clone();
*mrc = 20; // This operation clones the value and allocates space for it.

assert_eq!(*clone, 10);
assert_eq!(*mrc, 20);

Implementations

impl<T> Mrc<T>[src]

pub fn new(value: T) -> Self[src]

Allocates a value behind a Mrc pointer.

pub fn get_mut(&mut self) -> Option<&mut T>[src]

Attempts to get a mutable reference to the wrapped value.

If the pointer is not shared, it will return Some, whereas if multiple Mrcs or Ircs point to the value, this will return None.

Example

use yewtil::ptr::Mrc;
let mut mrc = Mrc::new(0);
assert!(mrc.get_mut().is_some());

let _clone = mrc.clone();
assert!(mrc.get_mut().is_none());

pub fn try_unwrap(self) -> Result<T, Self>[src]

Tries to extract the value from the Mrc, returning the Mrc if there is one or more other pointers to the value.

Example

use yewtil::ptr::Mrc;
let mrc = Mrc::new(0);

let clone = mrc.clone();
let mrc = mrc.try_unwrap().expect_err("Should not be able to unwrap");

std::mem::drop(clone);
let value = mrc.try_unwrap().expect("Should get value");

pub fn get_count(&self) -> usize[src]

Gets the reference count of the Mrc.

An exclusive Mrc will have a count of 1. The count is incremented on any cloning action and is decremented when drop is called.

Example

use yewtil::ptr::Mrc;
let mrc = Mrc::new(0);
assert_eq!(mrc.get_count(), 1);

let _clone = mrc.clone();
assert_eq!(mrc.get_count(), 2);

std::mem::drop(_clone);
assert_eq!(mrc.get_count(), 1);

pub fn is_exclusive(&self) -> bool[src]

Returns true if no other pointers to the value exist.

use yewtil::ptr::Mrc;
let mrc = Mrc::new(0);
assert!(mrc.is_exclusive());

let _clone = mrc.clone();
assert!(!mrc.is_exclusive());

std::mem::drop(_clone);
assert!(mrc.is_exclusive());

pub fn irc(&self) -> Irc<T>[src]

Returns an immutable reference counted pointer, pointing to the same value and reference count.

Example

use yewtil::ptr::{Mrc, Irc};
let mrc: Mrc<usize> =  Mrc::new(0);
let _irc: Irc<usize> = mrc.irc();

assert!(!mrc.is_exclusive());

pub fn into_irc(self) -> Irc<T>[src]

Converts this Mrc into an Irc.

Example

use yewtil::ptr::{Mrc, Irc};
let mrc: Mrc<usize> =  Mrc::new(0);
let irc: Irc<usize> = mrc.into_irc();

assert!(irc.is_exclusive());

pub fn ptr_eq(lhs: &Self, rhs: &Self) -> bool[src]

Checks pointers for equality.

Example

use yewtil::ptr::Mrc;
let mrc1 = Mrc::new(0);
let mrc2 = Mrc::new(0);
assert_eq!(mrc1, mrc2);
assert!(!Mrc::ptr_eq(&mrc1, &mrc2))

impl<T: Clone> Mrc<T>[src]

pub fn make_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the value if it has exclusive access. If it does not have exclusive access, it will make a clone of the data to acquire exclusive access.

Example

let mut mrc: Mrc<usize> = Mrc::new(0);

let _mut_ref: &mut usize = mrc.make_mut();
assert_eq!(mrc.get_count(), 1);

let clone = mrc.clone();
assert_eq!(mrc.get_count(), 2);

let _mut_ref: &mut usize = mrc.make_mut();
assert_eq!(mrc.get_count(), 1);
assert!(!Mrc::ptr_eq(&mrc, &clone))

pub fn unwrap_clone(self) -> T[src]

Consumes the Mrc and returns the value from the Mrc if it is not shared or clones the value if another Mrc or Irc has access to it.

pub fn clone_inner(&self) -> T[src]

Clones the value wrapped by the Mrc..

Trait Implementations

impl<T: Clone> AsMut<T> for Mrc<T>[src]

fn as_mut(&mut self) -> &mut T[src]

Performs the conversion.

impl<T> AsRef<T> for Mrc<T>[src]

fn as_ref(&self) -> &T[src]

Performs the conversion.

impl<T> Borrow<T> for Mrc<T>[src]

fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T: Clone> BorrowMut<T> for Mrc<T>[src]

fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> Clone for Mrc<T>[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for Mrc<T>[src]

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

Formats the value using the given formatter. Read more

impl<T: Default> Default for Mrc<T>[src]

fn default() -> Self[src]

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

impl<T> Deref for Mrc<T>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T: Clone> DerefMut for Mrc<T>[src]

fn deref_mut(&mut self) -> &mut Self::Target[src]

Mutably dereferences the value.

impl<T> Drop for Mrc<T>[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl<T: Hash> Hash for Mrc<T>[src]

fn hash<H: Hasher>(&self, state: &mut H)[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl<T: Ord> Ord for Mrc<T>[src]

fn cmp(&self, other: &Self) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl<T: PartialEq> PartialEq<Mrc<T>> for Mrc<T>[src]

fn eq(&self, other: &Self) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<T: PartialOrd> PartialOrd<Mrc<T>> for Mrc<T>[src]

fn partial_cmp(&self, other: &Self) -> Option<Ordering>[src]

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T: Eq> Eq for Mrc<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Mrc<T>

impl<T> !Send for Mrc<T>

impl<T> !Sync for Mrc<T>

impl<T> Unpin for Mrc<T>

impl<T> !UnwindSafe for Mrc<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

pub fn equivalent(&self, key: &K) -> bool[src]

Compare self to key and return true if they are equal.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, V> IntoOptPropValue<V> for T where
    T: IntoPropValue<Option<V>>, 
[src]

pub fn into_opt_prop_value(self) -> Option<V>[src]

Convert self to an optional value of a Properties struct.

impl<T> IntoPropValue<Option<T>> for T[src]

pub fn into_prop_value(self) -> Option<T>[src]

Convert self to a value of a Properties struct.

impl<T> IntoPropValue<T> for T[src]

pub fn into_prop_value(self) -> T[src]

Convert self to a value of a Properties struct.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<T> Any for T where
    T: Any

impl<T> CloneAny for T where
    T: Any + Clone