[−][src]Struct yewtil::ptr::Mrc
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 Mrc
s or Irc
s 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]
impl<T> AsRef<T> for Mrc<T>
[src]
impl<T> Borrow<T> for Mrc<T>
[src]
impl<T: Clone> BorrowMut<T> for Mrc<T>
[src]
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Clone for Mrc<T>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Debug> Debug for Mrc<T>
[src]
impl<T: Default> Default for Mrc<T>
[src]
impl<T> Deref for Mrc<T>
[src]
impl<T: Clone> DerefMut for Mrc<T>
[src]
impl<T> Drop for Mrc<T>
[src]
impl<T: Eq> Eq for Mrc<T>
[src]
impl<T: Hash> Hash for Mrc<T>
[src]
fn hash<H: Hasher>(&self, state: &mut H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T: Ord> Ord for Mrc<T>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<T: PartialEq> PartialEq<Mrc<T>> for Mrc<T>
[src]
impl<T: PartialOrd> PartialOrd<Mrc<T>> 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]
T: 'static + ?Sized,
impl<T> Any for T where
T: Any,
T: Any,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> CloneAny for T where
T: Clone + Any,
T: Clone + Any,
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
fn equivalent(&self, key: &K) -> bool
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,