Struct yrs::atomic::AtomicRef

source ·
pub struct AtomicRef<T>(/* private fields */);
Expand description

Atomic reference holding a value, that’s supposed to be shared - potentially between multiple threads. Internally this value is hidden behind Arc reference, which is returned during AtomicRef::get method. This cell doesn’t allow to return &mut references to stored object. Instead updates can be performed as lock-free operation mutating function passed over during AtomicRef::update call.

Example:

use yrs::atomic::AtomicRef;

let atom = AtomicRef::new(vec!["John"]);
atom.update(|users| {
    let mut users_copy = users.cloned().unwrap_or_else(Vec::default);
    users_copy.push("Susan");
    users_copy
});
let users = atom.get(); // John, Susan

Important note: since AtomicRef::update may call provided function multiple times (in scenarios, when another thread intercepted update with its own update call), provided function should be idempotent and preferably quick to execute.

Implementations§

source§

impl<T> AtomicRef<T>

source

pub fn new(value: T) -> Self

Creates a new instance of AtomicRef. This call boxes provided value and allocates it on a heap.

source

pub fn get(&self) -> Option<Arc<T>>

Returns a reference to current state hold by the AtomicRef. Keep in mind that after acquiring it, it may not present the current view of the state, but instead be changed by the concurrent AtomicRef::update call.

source

pub fn swap(&self, value: T) -> Option<Arc<T>>

Atomically replaces currently stored value with a new one, returning the last stored value.

source

pub fn take(&self) -> Option<Arc<T>>

Atomically replaces currently stored value with a null, returning the last stored value.

source

pub fn update<F>(&self, f: F)
where F: Fn(Option<&T>) -> T,

Updates stored value in place using provided function f, which takes read-only refrence to the most recently known state and producing new state in the result.

Important note: since AtomicRef::update may call provided function multiple times (in scenarios, when another thread intercepted update with its own update call), provided function should be idempotent and preferably quick to execute.

source§

impl<T: Copy> AtomicRef<T>

source

pub fn get_owned(&self) -> Option<T>

Returns a current state copy hold by the AtomicRef. Keep in mind that after acquiring it, it may not present the current view of the state, but instead be changed by the concurrent AtomicRef::update call.

Trait Implementations§

source§

impl<T: Debug> Debug for AtomicRef<T>

source§

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

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

impl<T> Default for AtomicRef<T>

source§

fn default() -> Self

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

impl<T> Drop for AtomicRef<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> PartialEq for AtomicRef<T>
where T: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Eq for AtomicRef<T>
where T: Eq,

source§

impl<T> Send for AtomicRef<T>

source§

impl<T> Sync for AtomicRef<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicRef<T>

§

impl<T> RefUnwindSafe for AtomicRef<T>

§

impl<T> Unpin for AtomicRef<T>

§

impl<T> UnwindSafe for AtomicRef<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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.