Struct yrs::atomic::AtomicRef

source ·
#[repr(transparent)]
pub struct AtomicRef<T>(_);
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 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.

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> Send for AtomicRef<T>

source§

impl<T> Sync for AtomicRef<T>

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V