Struct 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§

Source§

impl<T> CloneReplace<T>

Source

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 });
Source

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>

Source

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§

Source§

impl<T> Clone for CloneReplace<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for CloneReplace<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for CloneReplace<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CloneReplace<T>

§

impl<T> RefUnwindSafe for CloneReplace<T>
where T: RefUnwindSafe,

§

impl<T> Send for CloneReplace<T>
where T: Sync + Send,

§

impl<T> Sync for CloneReplace<T>
where T: Sync + Send,

§

impl<T> Unpin for CloneReplace<T>

§

impl<T> UnwindSafe for CloneReplace<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.