Skip to content
Merged
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.2"
version = "2.9.3"
edition = "2024"
authors = ["Codefonsi <info@codefonsi.com>"]
license = "MPL-2.0"
Expand Down
6 changes: 6 additions & 0 deletions examples/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ fn main() {
// let kp = |root: &mut Rectangle| {(Rectangle::size().set)(root)};
// let x:fn() = || {};

let kp = Rectangle::size().get;
let kp = Rectangle::size().set;

let kp = Rectangle::size().then(Size::width());
let kp= kp.get;

// let x: fn(&Rectangle) -> Option<&Size> = Rectangle::size().get;
// let y = that_takes(x);

Expand Down
11 changes: 11 additions & 0 deletions examples/basics_casepath.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use key_paths_derive::Kp;
use parking_lot::{Mutex, RwLock};
use std::cell::{RefCell, RefMut};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;

Expand All @@ -9,6 +10,9 @@ use std::sync::Arc;
#[derive(Debug, Kp)]
struct SomeComplexStruct {
id: String,
scfs20: Option<Vec<String>>,
scfs18: Option<HashMap<String, String>>,
scfs19: Option<HashSet<String>>,
scsf: Option<Box<SomeOtherStruct>>,
scfs2: Arc<std::sync::Mutex<SomeOtherStruct>>,
scfs3: Arc<std::sync::RwLock<SomeOtherStruct>>,
Expand Down Expand Up @@ -90,6 +94,8 @@ impl SomeComplexStruct {

Self {
id: String::from("SomeComplexStruct"),
scfs18: None,
scfs19: None,
scsf: Some(Box::new(inner.clone())),
// Arc<std::sync::Mutex/RwLock<T>>
scfs2: Arc::new(std::sync::Mutex::new(inner.clone())),
Expand Down Expand Up @@ -166,6 +172,7 @@ impl SomeComplexStruct {
scfs_t_o_arc_rwo: Some(Arc::new(tokio::sync::RwLock::new(Some(SomeOtherStruct {
sosf: Box::new(None),
})))),
scfs20: Some(Vec::new()),
}
}
}
Expand Down Expand Up @@ -201,6 +208,10 @@ fn main() {
// And Arc<parking_lot::Mutex<Option<SomeOtherStruct>>>
let x_pl_m: Option<&SomeOtherStruct> = SomeComplexStruct::scfs_arc_pl_mo().get(&instance);

let x = SomeComplexStruct::scfs18_at("testing".to_string());
let x = SomeComplexStruct::scfs20();
let x = SomeComplexStruct::scfs20_at(0);
println!("x = {:?}", x);
assert!(x_pl_rw.is_some());
assert!(x_std_m.is_some());
assert!(x_std_rw.is_some());
Expand Down
2 changes: 1 addition & 1 deletion key-paths-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "key-paths-derive"
version = "2.4.0"
version = "2.6.0"
edition = "2024"
description = "Proc-macro derive to generate keypath methods"
license = "MPL-2.0"
Expand Down
626 changes: 608 additions & 18 deletions key-paths-derive/src/lib.rs

Large diffs are not rendered by default.

70 changes: 69 additions & 1 deletion key-paths-derive/tests/derive_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use key_paths_derive::{Akp, Kp, Pkp};
use rust_key_paths::KpType;
use rust_key_paths::{KpTrait, KpType};
use std::collections::{HashMap, HashSet};

#[derive(Kp, Pkp, Akp)]
struct Person {
Expand Down Expand Up @@ -161,3 +162,70 @@ fn test_any_kps() {
let age_val = kps[1].get_as::<Person, i32>(&person);
assert_eq!(age_val, Some(Some(&28)));
}

#[derive(Kp)]
struct WithOptionalCollections {
map: Option<HashMap<String, i32>>,
set: Option<HashSet<String>>,
}

#[test]
fn option_hash_map_at_returns_value_ref() {
let s = WithOptionalCollections {
map: Some(HashMap::from([("k".to_string(), 42)])),
set: Some(HashSet::from(["x".to_string()])),
};
assert_eq!(WithOptionalCollections::map_at("k".to_string()).get(&s), Some(&42));

let mut s = s;
WithOptionalCollections::map_at("k".to_string())
.get_mut(&mut s)
.map(|v| *v += 1);
assert_eq!(s.map.as_ref().unwrap().get("k"), Some(&43));

assert_eq!(
WithOptionalCollections::set_at("x".to_string()).get(&s),
Some(&"x".to_string())
);
}

#[derive(Kp)]
enum OptMapEnum {
M(Option<HashMap<u8, String>>),
}

#[test]
fn option_hash_map_enum_variant_at() {
let e = OptMapEnum::M(Some(HashMap::from([(1u8, "a".to_string())])));
assert_eq!(OptMapEnum::m_at(1u8).get(&e), Some(&"a".to_string()));
}

#[derive(Kp)]
struct WithOptionalVec {
items: Option<Vec<i32>>,
}

#[test]
fn option_vec_at_index() {
let s = WithOptionalVec {
items: Some(vec![10, 20]),
};
assert_eq!(WithOptionalVec::items_at(1).get(&s), Some(&20));

let mut s = s;
WithOptionalVec::items_at(0)
.get_mut(&mut s)
.map(|x| *x = 99);
assert_eq!(s.items, Some(vec![99, 20]));
}

#[derive(Kp)]
enum OptVecEnum {
V(Option<Vec<u8>>),
}

#[test]
fn option_vec_enum_variant_at() {
let e = OptVecEnum::V(Some(vec![1, 2, 3]));
assert_eq!(OptVecEnum::v_at(2).get(&e), Some(&3u8));
}
129 changes: 129 additions & 0 deletions src/async_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use crate::{AccessorTrait, Kp, KpTrait};
use async_trait::async_trait;
use std::fmt;
// Re-export tokio sync types for convenience
#[cfg(feature = "tokio")]
pub use tokio::sync::{Mutex as TokioMutex, RwLock as TokioRwLock};
Expand Down Expand Up @@ -301,6 +302,134 @@ pub struct AsyncLockKp<
pub(crate) next: Kp<Mid, V, MidValue, Value, MutMid, MutValue, G2, S2>,
}

impl<
R,
Lock,
Mid,
V,
Root,
LockValue,
MidValue,
Value,
MutRoot,
MutLock,
MutMid,
MutValue,
G1,
S1,
L,
G2,
S2,
> fmt::Debug
for AsyncLockKp<
R,
Lock,
Mid,
V,
Root,
LockValue,
MidValue,
Value,
MutRoot,
MutLock,
MutMid,
MutValue,
G1,
S1,
L,
G2,
S2,
>
where
Root: std::borrow::Borrow<R>,
LockValue: std::borrow::Borrow<Lock>,
MidValue: std::borrow::Borrow<Mid>,
Value: std::borrow::Borrow<V>,
MutRoot: std::borrow::BorrowMut<R>,
MutLock: std::borrow::BorrowMut<Lock>,
MutMid: std::borrow::BorrowMut<Mid>,
MutValue: std::borrow::BorrowMut<V>,
G1: Fn(Root) -> Option<LockValue> + Clone,
S1: Fn(MutRoot) -> Option<MutLock> + Clone,
L: AsyncLockLike<Lock, MidValue> + AsyncLockLike<Lock, MutMid> + Clone,
G2: Fn(MidValue) -> Option<Value> + Clone,
S2: Fn(MutMid) -> Option<MutValue> + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AsyncLockKp")
.field("root_ty", &std::any::type_name::<R>())
.field("lock_ty", &std::any::type_name::<Lock>())
.field("mid_ty", &std::any::type_name::<Mid>())
.field("value_ty", &std::any::type_name::<V>())
.finish_non_exhaustive()
}
}

impl<
R,
Lock,
Mid,
V,
Root,
LockValue,
MidValue,
Value,
MutRoot,
MutLock,
MutMid,
MutValue,
G1,
S1,
L,
G2,
S2,
> fmt::Display
for AsyncLockKp<
R,
Lock,
Mid,
V,
Root,
LockValue,
MidValue,
Value,
MutRoot,
MutLock,
MutMid,
MutValue,
G1,
S1,
L,
G2,
S2,
>
where
Root: std::borrow::Borrow<R>,
LockValue: std::borrow::Borrow<Lock>,
MidValue: std::borrow::Borrow<Mid>,
Value: std::borrow::Borrow<V>,
MutRoot: std::borrow::BorrowMut<R>,
MutLock: std::borrow::BorrowMut<Lock>,
MutMid: std::borrow::BorrowMut<Mid>,
MutValue: std::borrow::BorrowMut<V>,
G1: Fn(Root) -> Option<LockValue> + Clone,
S1: Fn(MutRoot) -> Option<MutLock> + Clone,
L: AsyncLockLike<Lock, MidValue> + AsyncLockLike<Lock, MutMid> + Clone,
G2: Fn(MidValue) -> Option<Value> + Clone,
S2: Fn(MutMid) -> Option<MutValue> + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AsyncLockKp<{}, {}, {}, {}>",
std::any::type_name::<R>(),
std::any::type_name::<Lock>(),
std::any::type_name::<Mid>(),
std::any::type_name::<V>()
)
}
}

impl<
R,
Lock,
Expand Down
Loading
Loading