Struct mucell::MuCellStable
[-]
[+]
[src]
pub struct MuCell<T> { // some fields omitted }
A cell with the ability to mutate the value through an immutable reference when safe
Methods
impl<T> MuCell<T>
fn new(value: T) -> MuCell<T>
Construct a new cell containing the given value
fn borrow_mut(&mut self) -> &mut T
Borrow the contained object mutably.
This is genuinely and completely free.
fn borrow(&self) -> Ref<T>
Borrow the contained object immutably.
Unlike borrow_mut
, this isn’t quite free, but oh, so all but! It has a smattering of
reference counting. No branches, though, so it’s as fast as is computationally possible.
fn try_mutate<F: FnOnce(&mut T)>(&self, mutator: F) -> bool
Mutate the contained object if possible.
If any immutable references produced by calling borrow()
are active,
this will return false, not executing the function given.
If there are no immutable references active, this will execute the mutator function and return true.
Caution: you should avoid touching self
inside the mutator (not that it would really
make much sense to be touching it, anyway); most notably, you MUST NOT call borrow
on
self
inside the mutator, which includes things like the ==
implementation which borrow
the value briefly; while calling try_mutate
inside it will just return false, in debug
builds calling borrow
will panic and in release builds it will break memory safety as you
will have both a mutable and an immutable reference to the same object at the same time
(yep, it’s not quite preventing aliasing). So don’t do it.