Skip to main content

SyncKeyPathLike

Trait SyncKeyPathLike 

Source
pub trait SyncKeyPathLike<Root, Value, MutRoot, MutValue> {
    // Required methods
    fn sync_get(&self, root: Root) -> Option<Value>;
    fn sync_get_mut(&self, root: MutRoot) -> Option<MutValue>;
}
Expand description

Sync keypath that can be used as the “second” in AsyncLockKpThenLockKp for blanket impls. Also implemented for crate::Kp so crate::Kp::then_lock and crate::Kp::then_async can chain.

Required Methods§

Source

fn sync_get(&self, root: Root) -> Option<Value>

Get an immutable reference through the keypath (sync, non-blocking).

For crate::lock::LockKp, this acquires a read/write lock and returns the value. For plain crate::Kp, this navigates to the field directly.

§Example
use rust_key_paths::async_lock::SyncKeyPathLike;
use rust_key_paths::{KpType, LockKp};
use std::sync::Mutex;

#[derive(key_paths_derive::Kp)]
struct WithLocks {
    std_mutex: std::sync::Mutex<i32>,
    std_rwlock: std::sync::RwLock<String>,
}

let locks = WithLocks {
    std_mutex: Mutex::new(99),
    std_rwlock: std::sync::RwLock::new("hello".to_string()),
};
let mutex_kp = WithLocks::std_mutex();
let rwlock_kp = WithLocks::std_rwlock();
let next: KpType<i32, i32> = rust_key_paths::Kp::new(|i: &i32| Some(i), |i: &mut i32| Some(i));
let lock_kp = LockKp::new(mutex_kp, rust_key_paths::StdMutexAccess::new(), next);

// sync_get works with LockKp (same as .get())
let value = lock_kp.sync_get(&locks).unwrap();
assert_eq!(*value, 99);
Source

fn sync_get_mut(&self, root: MutRoot) -> Option<MutValue>

Get a mutable reference through the keypath (sync, non-blocking).

For crate::lock::LockKp, this acquires a write lock and returns a mutable reference. For plain crate::Kp, this navigates to the field mutably.

§Example
use rust_key_paths::async_lock::SyncKeyPathLike;
use rust_key_paths::{KpType, LockKp};
use std::sync::Mutex;

#[derive(key_paths_derive::Kp)]
struct WithLocks {
    std_mutex: std::sync::Mutex<i32>,
    std_rwlock: std::sync::RwLock<String>,
}

let mut locks = WithLocks {
    std_mutex: Mutex::new(99),
    std_rwlock: std::sync::RwLock::new("hello".to_string()),
};
let mutex_kp = WithLocks::std_mutex();
let next: KpType<i32, i32> = rust_key_paths::Kp::new(|i: &i32| Some(i), |i: &mut i32| Some(i));
let lock_kp = LockKp::new(mutex_kp, rust_key_paths::StdMutexAccess::new(), next);

// sync_get_mut works with LockKp (same as .get_mut())
let value = lock_kp.sync_get_mut(&mut locks).unwrap();
*value = 42;
assert_eq!(*locks.std_mutex.lock().unwrap(), 42);

Implementors§

Source§

impl<R, Lock, Mid, V, Root, LockValue, MidValue, Value, MutRoot, MutLock, MutMid, MutValue, G1, S1, L, G2, S2> SyncKeyPathLike<Root, Value, MutRoot, MutValue> for LockKp<R, Lock, Mid, V, Root, LockValue, MidValue, Value, MutRoot, MutLock, MutMid, MutValue, G1, S1, L, G2, S2>
where Root: Borrow<R>, LockValue: Borrow<Lock>, MidValue: Borrow<Mid>, Value: Borrow<V>, MutRoot: BorrowMut<R>, MutLock: BorrowMut<Lock>, MutMid: BorrowMut<Mid>, MutValue: BorrowMut<V>, G1: Fn(Root) -> Option<LockValue>, S1: Fn(MutRoot) -> Option<MutLock>, L: LockAccess<Lock, MidValue> + LockAccess<Lock, MutMid>, G2: Fn(MidValue) -> Option<Value>, S2: Fn(MutMid) -> Option<MutValue>, V: Clone,

Source§

impl<R, V, Root, Value, MutRoot, MutValue, G, S> SyncKeyPathLike<Root, Value, MutRoot, MutValue> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
where Root: Borrow<R>, Value: Borrow<V>, MutRoot: BorrowMut<R>, MutValue: BorrowMut<V>, G: Fn(Root) -> Option<Value>, S: Fn(MutRoot) -> Option<MutValue>,