Skip to content
Merged

Lite #36

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-key-paths"
version = "2.9.1"
version = "2.9.2"
edition = "2024"
authors = ["Codefonsi <info@codefonsi.com>"]
license = "MPL-2.0"
Expand Down
14 changes: 5 additions & 9 deletions examples/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,24 @@ struct Rectangle {
impl Rectangle {
// fn kp() -> KpType<'static, Rectangle, String> {
// KpType::new(
// |root| {
// |root| {
// let x = root.name.borrow();
// let y = &*x as *const String;
// Some(unsafe { &*y })}
// ,|root| {
// ,|root| {
// let mut x = root.name.borrow_mut();
// let y = &mut *x as *mut String;
// Some(unsafe {&mut *y})
// }
// )
// }


// fn kp() -> KpType<'static, Rectangle, std::cell::Ref<'static, String>> {
// KpType::new(
// |root| { Some(&'static root.name.borrow()) }
// ,|_| { None }
// )
// }

}
// Standalone fn pointers for keypath (reference: lib.rs identity_typed / Kp with fn types)

Expand Down Expand Up @@ -111,13 +109,11 @@ fn main() {
println!("Updated rectangle: {:?}", rect);
}


fn that_takes(f: fn(&Rectangle) -> Option<&Size>) -> for<'a> fn(&'a Rectangle) -> String {
|_root| { "working".to_string() }
|_root| "working".to_string()
}


// fn
// fn
// impl Fn
// Fn, FnMut, FnOnce
// Box<dyn Fn>
// Box<dyn Fn>
5 changes: 2 additions & 3 deletions examples/basics_casepath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::Arc;
// cargo run --example basics_casepath --features parking_lot
#[derive(Debug, Kp)]
struct SomeComplexStruct {
id: String,
scsf: Option<Box<SomeOtherStruct>>,
identity: String,
scfs2: Arc<std::sync::Mutex<SomeOtherStruct>>,
scfs3: Arc<std::sync::RwLock<SomeOtherStruct>>,
// scfs4: Arc<RefMut<SomeOtherStruct>>,
Expand Down Expand Up @@ -89,9 +89,8 @@ impl SomeComplexStruct {
};

Self {
id: String::from("SomeComplexStruct"),
scsf: Some(Box::new(inner.clone())),
identity: String::from("SomeComplexStruct"),

// Arc<std::sync::Mutex/RwLock<T>>
scfs2: Arc::new(std::sync::Mutex::new(inner.clone())),
scfs3: Arc::new(std::sync::RwLock::new(inner.clone())),
Expand Down
8 changes: 4 additions & 4 deletions examples/box_keypath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ fn main() {
.then(DarkStruct::dsf())
.get(&instance);
assert_eq!(dsf, Some(&"dark_value".to_string()));

/*
Kp - struct 8 genric
KpType - typealias 2 genric
KpTrait - to enforce use to use kp
KpTraitType - 2 gen
KpTrait - to enforce use to use kp
KpTraitType - 2 gen
Accessor - acccessor fns into a triat - get, get_mut
OptionalAccessor - get_optionl, get_mut_optional
HofExt - map, filter, flatmap .....
Chain - then
CoercionTrait - to_box, to_arc
ChainExt - then_async, then_pin_fut, then_sync
*/
*/
println!("{:?}", instance);
}
10 changes: 8 additions & 2 deletions src/async_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,10 @@ impl<'a, T: 'static + Send + Sync> AsyncLockLike<std::sync::Arc<tokio::sync::Mut
}

#[inline]
async fn lock_write(&self, lock: &mut std::sync::Arc<tokio::sync::Mutex<T>>) -> Option<&'a mut T> {
async fn lock_write(
&self,
lock: &mut std::sync::Arc<tokio::sync::Mutex<T>>,
) -> Option<&'a mut T> {
let mut guard = lock.lock().await;
let ptr = &mut *guard as *mut T;
unsafe { Some(&mut *ptr) }
Expand Down Expand Up @@ -1948,7 +1951,10 @@ impl<'a, T: 'static + Send + Sync> AsyncLockLike<std::sync::Arc<tokio::sync::RwL
unsafe { Some(&mut *ptr) }
}

async fn lock_write(&self, lock: &mut std::sync::Arc<tokio::sync::RwLock<T>>) -> Option<&'a mut T> {
async fn lock_write(
&self,
lock: &mut std::sync::Arc<tokio::sync::RwLock<T>>,
) -> Option<&'a mut T> {
// SHALLOW CLONE: Only Arc refcount is incremented
let mut guard = lock.write().await;
let ptr = &mut *guard as *mut T;
Expand Down
33 changes: 12 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// type Getter<R, V, Root, Value> where Root: std::borrow::Borrow<R>, Value: std::borrow::Borrow<V> = fn(Root) -> Option<Value>;
// type Setter<R, V> = fn(&'r mut R) -> Option<&'r mut V>;

use std::sync::{Arc};
use std::sync::Arc;

// Export the lock module
pub mod lock;
Expand Down Expand Up @@ -1226,8 +1226,7 @@ where
}
}

pub trait AccessorTrait<R, V, Root, Value, MutRoot, MutValue, G, S>
{
pub trait AccessorTrait<R, V, Root, Value, MutRoot, MutValue, G, S> {
/// Like [get](Kp::get), but takes an optional root: returns `None` if `root` is `None`, otherwise the result of the getter.
fn get_optional(&self, root: Option<Root>) -> Option<Value>;
// {
Expand All @@ -1252,7 +1251,7 @@ pub trait AccessorTrait<R, V, Root, Value, MutRoot, MutValue, G, S>
#[inline]
fn get_mut_or_else<F>(&self, root: MutRoot, f: F) -> MutValue
where
F: FnOnce() -> MutValue,;
F: FnOnce() -> MutValue;
// {
// self.get_mut(root).unwrap_or_else(f)
// }
Expand Down Expand Up @@ -1283,7 +1282,7 @@ where
R: 'b,
V: 'b,
Root: for<'a> From<&'a R>,
MutRoot: for<'a> From<&'a mut R>,;
MutRoot: for<'a> From<&'a mut R>;

fn for_box<'a>(
&self,
Expand All @@ -1301,14 +1300,13 @@ where
R: 'a,
V: 'a,
Root: for<'b> From<&'b R>,
MutRoot: for<'b> From<&'b mut R>,;
MutRoot: for<'b> From<&'b mut R>;

/// set fn is converting fn pointer to Fn closure
fn into_set(self) -> impl Fn(MutRoot) -> Option<MutValue>;

/// get fn is converting fn pointer to Fn closure
fn into_get(self) -> impl Fn(Root) -> Option<Value>;

}

pub trait HofTrait<R, V, Root, Value, MutRoot, MutValue, G, S>:
Expand Down Expand Up @@ -1757,7 +1755,6 @@ where
G: Fn(Root) -> Option<Value>,
S: Fn(MutRoot) -> Option<MutValue>,
{

fn then<SV, SubValue, MutSubValue, G2, S2>(
self,
next: Kp<V, SV, Value, SubValue, MutValue, MutSubValue, G2, S2>,
Expand All @@ -1783,15 +1780,14 @@ where
move |root: MutRoot| (self.set)(root).and_then(|value| (next.set)(value)),
)
}

fn get(&self, root: Root) -> Option<Value> {
(self.get)(root)
}

fn get_mut(&self, root: MutRoot) -> Option<MutValue> {
(self.set)(root)
}

}

impl<R, V, Root, Value, MutRoot, MutValue, G, S>
Expand All @@ -1805,7 +1801,7 @@ where
G: Fn(Root) -> Option<Value>,
S: Fn(MutRoot) -> Option<MutValue>,
{
fn for_arc<'b>(
fn for_arc<'b>(
&self,
) -> Kp<
std::sync::Arc<R>,
Expand Down Expand Up @@ -1866,13 +1862,12 @@ where
)
}


/// set fn is converting fn pointer to Fn closure
#[inline]
fn into_set(self) -> impl Fn(MutRoot) -> Option<MutValue> {
self.set
}

/// get fn is converting fn pointer to Fn closure
#[inline]
fn into_get(self) -> impl Fn(Root) -> Option<Value> {
Expand Down Expand Up @@ -1906,23 +1901,21 @@ where
{
/// Like [get](Kp::get), but takes an optional root: returns `None` if `root` is `None`, otherwise the result of the getter.
#[inline]
fn get_optional(&self, root: Option<Root>) -> Option<Value>
{
fn get_optional(&self, root: Option<Root>) -> Option<Value> {
root.and_then(|r| (self.get)(r))
}

/// Like [get_mut](Kp::get_mut), but takes an optional root: returns `None` if `root` is `None`, otherwise the result of the setter.
#[inline]
fn get_mut_optional(&self, root: Option<MutRoot>) -> Option<MutValue>
{
fn get_mut_optional(&self, root: Option<MutRoot>) -> Option<MutValue> {
root.and_then(|r| (self.set)(r))
}

/// Returns the value if the keypath succeeds, otherwise calls `f` and returns its result.
#[inline]
fn get_or_else<F>(&self, root: Root, f: F) -> Value
where
F: FnOnce() -> Value
F: FnOnce() -> Value,
{
(self.get)(root).unwrap_or_else(f)
}
Expand Down Expand Up @@ -1986,7 +1979,6 @@ where
{
}


impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
where
Root: std::borrow::Borrow<R>,
Expand Down Expand Up @@ -2040,7 +2032,6 @@ where
move |root: MutRoot| (self.set)(root).and_then(|value| (next.set)(value)),
)
}

}
/// Zip two keypaths together to create a tuple
/// Works only with KpType (reference-based keypaths)
Expand Down
Loading