Struct AtomicRef
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, SusanImportant 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§
§impl<T> AtomicRef<T>
impl<T> AtomicRef<T>
pub fn new(value: T) -> AtomicRef<T>
pub fn new(value: T) -> AtomicRef<T>
Creates a new instance of AtomicRef. This call boxes provided value and allocates it
on a heap.
pub fn get(&self) -> Option<Arc<T>>
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.
pub fn swap(&self, value: T) -> Option<Arc<T>>
pub fn swap(&self, value: T) -> Option<Arc<T>>
Atomically replaces currently stored value with a new one, returning the last stored value.
pub fn take(&self) -> Option<Arc<T>>
pub fn take(&self) -> Option<Arc<T>>
Atomically replaces currently stored value with a null, returning the last stored value.
pub fn update<F>(&self, f: F)
pub fn update<F>(&self, f: F)
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.
Trait Implementations§
impl<T> Eq for AtomicRef<T>where
T: Eq,
impl<T> Send for AtomicRef<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more