Struct clone_replace::CloneReplace
source · [−]pub struct CloneReplace<T> { /* private fields */ }
Expand description
A shareable store for data which provides owned references.
A CloneReplace
stores a reference version of an enclosed data
structure. An owned snapshot of the current reference version can
be retrieved by calling access on
CloneReplace, which will preserve the reference version at that
moment until it is dropped. A mutatable snapshot of the current
reference version can be retrieved by calling
mutate on CloneReplace, and when this
goes out of scope, the reference version at that moment will be
replaced by the mutated one.
Implementations
sourceimpl<T> CloneReplace<T>
impl<T> CloneReplace<T>
sourcepub fn new(data: T) -> Self
pub fn new(data: T) -> Self
Create a new CloneReplace.
Example:
use clone_replace::CloneReplace;
struct Foo {
a: i32
}
let cr = CloneReplace::new(Foo { a: 0 });
sourcepub fn access(&self) -> Arc<T>
pub fn access(&self) -> Arc<T>
Retrieve a snapshot of the current reference version of the data.
The return value is owned, and the snapshot taken will remain unchanging until it goes out of scope. The existence of the snapshot will not prevent the reference version from evolving, so holding snapshots must be carefully considered, as it can lead to memory pressure.
Example:
use clone_replace::CloneReplace;
let c = CloneReplace::new(1);
let v = c.access();
assert_eq!(*v, 1);
sourceimpl<T: Clone> CloneReplace<T>
impl<T: Clone> CloneReplace<T>
sourcepub fn mutate(&self) -> MutateGuard<T>
pub fn mutate(&self) -> MutateGuard<T>
Create a mutable replacement for the reference data.
A copy of the current reference version of the data is created. The MutateGuard provides mutable references to that data. When the guard goes out of scope the reference version will be overwritten with the updated version.
Multiple guards can exist simultaneously, and there is no attempt to prevent loss of data from stale updates. An internally consistent version of the data, as produced by a single mutate call, will always exist, but not every mutate call will end up being reflected in a reference version of the data. This is a significantly weaker consistency guarantee than a Mutex provides, for example.
Example:
use clone_replace::CloneReplace;
let c = CloneReplace::new(1);
let mut v = c.mutate();
*v = 2;
drop(v);
assert_eq!(*c.access(), 2);
Trait Implementations
sourceimpl<T> Clone for CloneReplace<T>
impl<T> Clone for CloneReplace<T>
sourceimpl<T: Debug> Debug for CloneReplace<T>
impl<T: Debug> Debug for CloneReplace<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for CloneReplace<T> where
T: RefUnwindSafe,
impl<T> Send for CloneReplace<T> where
T: Send + Sync,
impl<T> Sync for CloneReplace<T> where
T: Send + Sync,
impl<T> Unpin for CloneReplace<T>
impl<T> UnwindSafe for CloneReplace<T> where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more