pub struct Kp<R, V, Root, Value, MutRoot, MutValue, G, S>where
Root: Borrow<R>,
MutRoot: BorrowMut<R>,
MutValue: BorrowMut<V>,
G: Fn(Root) -> Option<Value>,
S: Fn(MutRoot) -> Option<MutValue>,{
pub get: G,
pub set: S,
pub _p: PhantomData<(R, V, Root, Value, MutRoot, MutValue)>,
}Expand description
Kp — typed keypath with getter/setter closures. See also AKp for type-erased keypaths.
§Mutation: get vs get_mut (setter path)
- get uses the
getclosure (getter):Fn(Root) -> Option<Value> - get_mut uses the
setclosure (setter):Fn(MutRoot) -> Option<MutValue>
When mutating through a Kp, the setter path is used—get_mut invokes the set closure,
not the get closure. The getter is for read-only access only.
For manual reference-shaped paths, constrain_get and constrain_set help closures satisfy
for<'b> Fn(&'b R) -> Option<&'b V>; use Kp::get_ref / Kp::get_mut_ref to call them explicitly.
Fields§
§get: GGetter closure: used by Kp::get for read-only access when G satisfies the HRTB.
set: SSetter closure: used by Kp::get_mut for mutation when S satisfies the HRTB.
_p: PhantomData<(R, V, Root, Value, MutRoot, MutValue)>Implementations§
Source§impl<'a, R, V> Kp<R, V, &'a R, &'a V, &'a mut R, &'a mut V, for<'b> fn(&'b R) -> Option<&'b V>, for<'b> fn(&'b mut R) -> Option<&'b mut V>>
impl<'a, R, V> Kp<R, V, &'a R, &'a V, &'a mut R, &'a mut V, for<'b> fn(&'b R) -> Option<&'b V>, for<'b> fn(&'b mut R) -> Option<&'b mut V>>
Sourcepub fn to_dynamic(self) -> KpDynamic<R, V>
pub fn to_dynamic(self) -> KpDynamic<R, V>
Converts this keypath to KpDynamic for dynamic dispatch and storage (e.g. in a struct field).
Source§impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
Sourcepub fn into_dynamic(self) -> KpDynamic<R, V>
pub fn into_dynamic(self) -> KpDynamic<R, V>
Erases getter/setter type into KpDynamic so you can store composed paths (e.g. after KpTrait::then).
#[derive(Kp)] methods return KpType (fn pointers); chaining with .then() produces opaque closures.
Neither matches a fixed KpType<…> field type—use KpDynamic<R, V> and .into_dynamic() (or
KpType::to_dynamic for a single segment).
§Safety
This uses a small amount of unsafe internally: it re-interprets &R / &mut R as Root / MutRoot.
That matches every Kp built from this crate’s public API (Kp::new on reference-shaped handles,
#[derive(Kp)], and KpTrait::then / Kp::then on those paths). Do not call this on a custom Kp
whose Root / MutRoot are not layout-compatible with &R / &mut R or whose getters keep borrows
alive past the call.
Source§impl<R, V> Kp<R, V, &'static R, &'static V, &'static mut R, &'static mut V, Box<dyn for<'b> Fn(&'b R) -> Option<&'b V> + Send + Sync>, Box<dyn for<'b> Fn(&'b mut R) -> Option<&'b mut V> + Send + Sync>>
impl<R, V> Kp<R, V, &'static R, &'static V, &'static mut R, &'static mut V, Box<dyn for<'b> Fn(&'b R) -> Option<&'b V> + Send + Sync>, Box<dyn for<'b> Fn(&'b mut R) -> Option<&'b mut V> + Send + Sync>>
Sourcepub fn from_closures<G, S>(get: G, set: S) -> Self
pub fn from_closures<G, S>(get: G, set: S) -> Self
Build a keypath from two closures (e.g. when they capture a variable like an index).
Same pattern as Kp::new in lock.rs; use this when the keypath captures variables.
Source§impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
pub fn new(get: G, set: S) -> Self
Sourcepub fn get(&self, root: Root) -> Option<Value>
pub fn get(&self, root: Root) -> Option<Value>
Read through the getter closure. For reference-shaped keypaths built with constrain_get
/ constrain_set, you can also call this as kp.get(root) with root: Root (often &R).
Sourcepub fn get_ref<'a>(&self, root: &'a R) -> Option<&'a V>
pub fn get_ref<'a>(&self, root: &'a R) -> Option<&'a V>
Higher-ranked read when G: for<'b> Fn(&'b R) -> Option<&'b V> (e.g. manual keypaths using
constrain_get). Prefer get for generic Kp including mapped values.
Sourcepub fn get_mut_ref<'a>(&self, root: &'a mut R) -> Option<&'a mut V>
pub fn get_mut_ref<'a>(&self, root: &'a mut R) -> Option<&'a mut V>
Higher-ranked write when S: for<'b> Fn(&'b mut R) -> Option<&'b mut V>.
pub fn then<SV, G2, S2>( self, next: Kp<V, SV, &'static V, &'static SV, &'static mut V, &'static mut SV, G2, S2>, ) -> Kp<R, SV, &'static R, &'static SV, &'static mut R, &'static mut SV, impl for<'b> Fn(&'b R) -> Option<&'b SV>, impl for<'b> Fn(&'b mut R) -> Option<&'b mut SV>>
Trait Implementations§
Source§impl<R, V, Root, Value, MutRoot, MutValue, G, S> AccessorTrait<R, V, Root, Value, MutRoot, MutValue, G, S> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R, V, Root, Value, MutRoot, MutValue, G, S> AccessorTrait<R, V, Root, Value, MutRoot, MutValue, G, S> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
Source§fn get_optional(&self, root: Option<Root>) -> Option<Value>
fn get_optional(&self, root: Option<Root>) -> Option<Value>
Like get, but takes an optional root: returns None if root is None, otherwise the result of the getter.
Source§fn get_mut_optional(&self, root: Option<MutRoot>) -> Option<MutValue>
fn get_mut_optional(&self, root: Option<MutRoot>) -> Option<MutValue>
Like get_mut, but takes an optional root: returns None if root is None, otherwise the result of the setter.
Source§fn get_or_else<F>(&self, root: Root, f: F) -> Valuewhere
F: FnOnce() -> Value,
fn get_or_else<F>(&self, root: Root, f: F) -> Valuewhere
F: FnOnce() -> Value,
Returns the value if the keypath succeeds, otherwise calls f and returns its result.
Source§fn get_mut_or_else<F>(&self, root: MutRoot, f: F) -> MutValuewhere
F: FnOnce() -> MutValue,
fn get_mut_or_else<F>(&self, root: MutRoot, f: F) -> MutValuewhere
F: FnOnce() -> MutValue,
Returns the mutable value if the keypath succeeds, otherwise calls f and returns its result.
Source§impl<R, V, Root, Value, MutRoot, MutValue, G, S> ChainExt<R, V, Root, Value, MutRoot, MutValue> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R, V, Root, Value, MutRoot, MutValue, G, S> ChainExt<R, V, Root, Value, MutRoot, MutValue> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
Source§fn then_lock<Lock, Mid, V2, LockValue, MidValue, Value2, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>(
self,
lock_kp: LockKp<V, Lock, Mid, V2, Value, LockValue, MidValue, Value2, MutValue, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>,
) -> KpThenLockKp<R, V, V2, Root, Value, Value2, MutRoot, MutValue, MutValue2, Self, LockKp<V, Lock, Mid, V2, Value, LockValue, MidValue, Value2, MutValue, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>>where
V: 'static + Clone,
V2: 'static,
Value: Borrow<V>,
Value2: Borrow<V2>,
MutValue: BorrowMut<V>,
MutValue2: BorrowMut<V2>,
LockValue: Borrow<Lock>,
MidValue: Borrow<Mid>,
MutLock: BorrowMut<Lock>,
MutMid: BorrowMut<Mid>,
G1: Fn(Value) -> Option<LockValue>,
S1: Fn(MutValue) -> Option<MutLock>,
L: LockAccess<Lock, MidValue> + LockAccess<Lock, MutMid>,
G2: Fn(MidValue) -> Option<Value2>,
S2: Fn(MutMid) -> Option<MutValue2>,
fn then_lock<Lock, Mid, V2, LockValue, MidValue, Value2, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>(
self,
lock_kp: LockKp<V, Lock, Mid, V2, Value, LockValue, MidValue, Value2, MutValue, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>,
) -> KpThenLockKp<R, V, V2, Root, Value, Value2, MutRoot, MutValue, MutValue2, Self, LockKp<V, Lock, Mid, V2, Value, LockValue, MidValue, Value2, MutValue, MutLock, MutMid, MutValue2, G1, S1, L, G2, S2>>where
V: 'static + Clone,
V2: 'static,
Value: Borrow<V>,
Value2: Borrow<V2>,
MutValue: BorrowMut<V>,
MutValue2: BorrowMut<V2>,
LockValue: Borrow<Lock>,
MidValue: Borrow<Mid>,
MutLock: BorrowMut<Lock>,
MutMid: BorrowMut<Mid>,
G1: Fn(Value) -> Option<LockValue>,
S1: Fn(MutValue) -> Option<MutLock>,
L: LockAccess<Lock, MidValue> + LockAccess<Lock, MutMid>,
G2: Fn(MidValue) -> Option<Value2>,
S2: Fn(MutMid) -> Option<MutValue2>,
Source§fn then_async<AsyncKp>(
self,
async_kp: AsyncKp,
) -> KpThenAsyncKeyPath<R, V, <AsyncKp::Value as KeyPathValueTarget>::Target, Root, Value, AsyncKp::Value, MutRoot, MutValue, AsyncKp::MutValue, Self, AsyncKp>where
Value: Borrow<V>,
MutValue: BorrowMut<V>,
AsyncKp: AsyncKeyPathLike<Value, MutValue>,
AsyncKp::Value: KeyPathValueTarget + Borrow<<AsyncKp::Value as KeyPathValueTarget>::Target>,
AsyncKp::MutValue: BorrowMut<<AsyncKp::Value as KeyPathValueTarget>::Target>,
<AsyncKp::Value as KeyPathValueTarget>::Target: 'static,
fn then_async<AsyncKp>(
self,
async_kp: AsyncKp,
) -> KpThenAsyncKeyPath<R, V, <AsyncKp::Value as KeyPathValueTarget>::Target, Root, Value, AsyncKp::Value, MutRoot, MutValue, AsyncKp::MutValue, Self, AsyncKp>where
Value: Borrow<V>,
MutValue: BorrowMut<V>,
AsyncKp: AsyncKeyPathLike<Value, MutValue>,
AsyncKp::Value: KeyPathValueTarget + Borrow<<AsyncKp::Value as KeyPathValueTarget>::Target>,
AsyncKp::MutValue: BorrowMut<<AsyncKp::Value as KeyPathValueTarget>::Target>,
<AsyncKp::Value as KeyPathValueTarget>::Target: 'static,
.get(&root).await on the returned keypath.Source§impl<R: Clone, V: Clone, Root, Value: Clone, MutRoot, MutValue, G, S> Clone for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R: Clone, V: Clone, Root, Value: Clone, MutRoot, MutValue, G, S> Clone for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
Source§impl<R, V, Root, Value, MutRoot, MutValue, G, S> CoercionTrait<R, V, Root, Value, MutRoot, MutValue, G, S> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
impl<R, V, Root, Value, MutRoot, MutValue, G, S> CoercionTrait<R, V, Root, Value, MutRoot, MutValue, G, S> for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
Source§fn into_set(self) -> impl Fn(MutRoot) -> Option<MutValue>
fn into_set(self) -> impl Fn(MutRoot) -> Option<MutValue>
set fn is converting fn pointer to Fn closure
Source§fn into_get(self) -> impl Fn(Root) -> Option<Value>
fn into_get(self) -> impl Fn(Root) -> Option<Value>
get fn is converting fn pointer to Fn closure