Struct Slock

Source
pub struct Slock<T> { /* private fields */ }
Expand description

The Slock object.

An atomically reference counted read/write lock with special safety features to avoid deadlocks.

When used correctly (no nesting lock access functions), deadlocks should be impossible.

Implementations§

Source§

impl<T> Slock<T>

Source

pub fn new(value: T) -> Self

Create a new Slock with a given initial value.

Source

pub async fn map<F, U>(&self, mapper: F) -> Result<U, Elapsed>
where F: FnOnce(&T) -> U,

Extract inner values from within a Slock

let name = lock.map(|v| v.name).await;
Source

pub async fn set<F>(&self, setter: F)
where F: FnOnce(T) -> T,

A setter for changing the internal data of the lock.

let lock = Slock::new(1i32);

lock.set(|v| v + 1).await;
lock.set(|_| 6).await;
Source

pub fn split(&self) -> Self

👎Deprecated: Use clone() instead

Create’s a new lock pointing to the same data. Modifying the data in the new lock will result in seeing the same change in the old lock.

let lock = Slock::new(0i32);
let the_same_lock = lock.split();
Source

pub async fn hook<F>(&self, hook: F)
where F: FnMut(&T) + 'static,

Subscribe to changes in the lock.

hook will be called any time Slock::set is called.

Source§

impl<T: Clone> Slock<T>

Source

pub async fn get_clone(&self) -> T

Returns a clone of the lock’s data.

Source

pub async fn clone_deep(&self) -> Self

Create a new lock with data clone from this one.

Source§

impl<T> Slock<Vec<T>>

Source

pub async fn push(&self, value: T)

Asyncronously push to a vec. Note that due to the nature of async code, order cannot be guaranteed.

Source§

impl<T> Slock<Slock<T>>

Source

pub async fn flatten(&self) -> Slock<T>

Converts from Slock<Slock<T>> to Slock<T>

Source§

impl<K: Eq + Hash + Copy, V> Slock<HashMap<K, Slock<V>>>

Source

pub fn new_map() -> Slock<HashMap<K, Slock<V>>>

Create a new Slock powered HashMap

Source

pub async fn insert<F>(&self, key: K, setter: F)
where F: FnOnce(Option<V>) -> V,

Insert / modify a value in the map at a given key.

Source

pub async fn from_key(&self, key: K) -> Option<Slock<V>>

Get a value from the map at a given key.

Source§

impl<T: Copy> Slock<T>

Source

pub async fn get(&self) -> T

If a lock’s data implements copy, this will return an owned copy of it.

Trait Implementations§

Source§

impl<T> Clone for Slock<T>

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more
Source§

impl<T: Send> Send for Slock<T>

Source§

impl<T: Send> Sync for Slock<T>

Auto Trait Implementations§

§

impl<T> Freeze for Slock<T>

§

impl<T> !RefUnwindSafe for Slock<T>

§

impl<T> Unpin for Slock<T>

§

impl<T> !UnwindSafe for Slock<T>

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.