SyncCell

Trait SyncCell 

Source
pub trait SyncCell<T> {
    // Required methods
    fn from_value(value: T) -> Self;
    fn get_mut(&mut self) -> &mut T;
    fn try_with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>;
    fn try_store(&self, value: T) -> Result<(), T>;

    // Provided method
    fn swap(&self, value: T) { ... }
}
Expand description

A thread-safe cell that can be used as atomic for any type.

It is already implemented for NaiveRWLock, std::sync::Mutex, std::sync::RwLock.

Required Methods§

Source

fn from_value(value: T) -> Self

Creates a new SyncCell with the given value.

Source

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

Returns a mutable reference to the inner value.

Source

fn try_with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>

Tries to load the value and call the provided function with it. Returns None if the SyncCell is locked.

For LockFreeSyncCell, this method always returns Some.

Source

fn try_store(&self, value: T) -> Result<(), T>

Tries to store the value. It returns Ok(()) or Err(value) if the SyncCell is locked.

For LockFreeSyncCell, this method always returns Ok.

Provided Methods§

Source

fn swap(&self, value: T)

Tries to store the value using busy-waiting with Backoff.

For LockFreeSyncCell, this method never blocks.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> SyncCell<T> for Mutex<T>

Source§

fn from_value(value: T) -> Self

Source§

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

Source§

fn try_with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>

Source§

fn try_store(&self, value: T) -> Result<(), T>

Source§

impl<T> SyncCell<T> for RwLock<T>

Source§

fn from_value(value: T) -> Self

Source§

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

Source§

fn try_with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>

Source§

fn try_store(&self, value: T) -> Result<(), T>

Implementors§

Source§

impl<T> SyncCell<T> for NaiveRWLock<T>