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]
impl<T> Mrc<T>
[src]pub fn get_mut(&mut self) -> Option<&mut T>
[src]
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]
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]
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]
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]
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());
impl<T: Clone> Mrc<T>
[src]
impl<T: Clone> Mrc<T>
[src]pub fn make_mut(&mut self) -> &mut 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]
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]
pub fn clone_inner(&self) -> T
[src]Clones the value wrapped by the Mrc
..
Trait Implementations
impl<T: Clone> BorrowMut<T> for Mrc<T>
[src]
impl<T: Clone> BorrowMut<T> for Mrc<T>
[src]fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T: Ord> Ord for Mrc<T>
[src]
impl<T: Ord> Ord for Mrc<T>
[src]impl<T: PartialOrd> PartialOrd<Mrc<T>> for Mrc<T>
[src]
impl<T: PartialOrd> PartialOrd<Mrc<T>> for Mrc<T>
[src]fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[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]
#[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]
#[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
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> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[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]
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]pub fn equivalent(&self, key: &K) -> bool
[src]
pub fn equivalent(&self, key: &K) -> bool
[src]Compare self to key
and return true
if they are equal.
impl<T, V> IntoOptPropValue<V> for T where
T: IntoPropValue<Option<V>>,
[src]
impl<T, V> IntoOptPropValue<V> for T where
T: IntoPropValue<Option<V>>,
[src]pub fn into_opt_prop_value(self) -> 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]
impl<T> IntoPropValue<Option<T>> for T
[src]pub fn into_prop_value(self) -> Option<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]
impl<T> IntoPropValue<T> for T
[src]pub fn into_prop_value(self) -> 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]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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> Any for T where
T: Any,
T: Any,
impl<T> CloneAny for T where
T: Any + Clone,
T: Any + Clone,