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§
Source§impl<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);
Source§impl<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);