diff --git a/Cargo.toml b/Cargo.toml index 6c36a9a..a012c84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust-key-paths" -version = "2.9.1" +version = "2.9.2" edition = "2024" authors = ["Codefonsi "] license = "MPL-2.0" diff --git a/examples/basics.rs b/examples/basics.rs index 1276088..7e4cc94 100644 --- a/examples/basics.rs +++ b/examples/basics.rs @@ -34,11 +34,11 @@ 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}) @@ -46,14 +46,12 @@ impl Rectangle { // ) // } - // 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) @@ -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 \ No newline at end of file +// Box diff --git a/examples/basics_casepath.rs b/examples/basics_casepath.rs index 9e15cfc..474d9fb 100644 --- a/examples/basics_casepath.rs +++ b/examples/basics_casepath.rs @@ -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>, - identity: String, scfs2: Arc>, scfs3: Arc>, // scfs4: Arc>, @@ -89,9 +89,8 @@ impl SomeComplexStruct { }; Self { + id: String::from("SomeComplexStruct"), scsf: Some(Box::new(inner.clone())), - identity: String::from("SomeComplexStruct"), - // Arc> scfs2: Arc::new(std::sync::Mutex::new(inner.clone())), scfs3: Arc::new(std::sync::RwLock::new(inner.clone())), diff --git a/examples/box_keypath.rs b/examples/box_keypath.rs index 94f603b..97cf216 100644 --- a/examples/box_keypath.rs +++ b/examples/box_keypath.rs @@ -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); } diff --git a/src/async_lock.rs b/src/async_lock.rs index ecc117d..ad4de19 100644 --- a/src/async_lock.rs +++ b/src/async_lock.rs @@ -1866,7 +1866,10 @@ impl<'a, T: 'static + Send + Sync> AsyncLockLike>) -> Option<&'a mut T> { + async fn lock_write( + &self, + lock: &mut std::sync::Arc>, + ) -> Option<&'a mut T> { let mut guard = lock.lock().await; let ptr = &mut *guard as *mut T; unsafe { Some(&mut *ptr) } @@ -1948,7 +1951,10 @@ impl<'a, T: 'static + Send + Sync> AsyncLockLike>) -> Option<&'a mut T> { + async fn lock_write( + &self, + lock: &mut std::sync::Arc>, + ) -> Option<&'a mut T> { // SHALLOW CLONE: Only Arc refcount is incremented let mut guard = lock.write().await; let ptr = &mut *guard as *mut T; diff --git a/src/lib.rs b/src/lib.rs index aa1a8a7..f77ae03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ // type Getter where Root: std::borrow::Borrow, Value: std::borrow::Borrow = fn(Root) -> Option; // type Setter = fn(&'r mut R) -> Option<&'r mut V>; -use std::sync::{Arc}; +use std::sync::Arc; // Export the lock module pub mod lock; @@ -1226,8 +1226,7 @@ where } } -pub trait AccessorTrait -{ +pub trait AccessorTrait { /// 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) -> Option; // { @@ -1252,7 +1251,7 @@ pub trait AccessorTrait #[inline] fn get_mut_or_else(&self, root: MutRoot, f: F) -> MutValue where - F: FnOnce() -> MutValue,; + F: FnOnce() -> MutValue; // { // self.get_mut(root).unwrap_or_else(f) // } @@ -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, @@ -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; /// get fn is converting fn pointer to Fn closure fn into_get(self) -> impl Fn(Root) -> Option; - } pub trait HofTrait: @@ -1757,7 +1755,6 @@ where G: Fn(Root) -> Option, S: Fn(MutRoot) -> Option, { - fn then( self, next: Kp, @@ -1783,15 +1780,14 @@ where move |root: MutRoot| (self.set)(root).and_then(|value| (next.set)(value)), ) } - + fn get(&self, root: Root) -> Option { (self.get)(root) } - + fn get_mut(&self, root: MutRoot) -> Option { (self.set)(root) } - } impl @@ -1805,7 +1801,7 @@ where G: Fn(Root) -> Option, S: Fn(MutRoot) -> Option, { - fn for_arc<'b>( + fn for_arc<'b>( &self, ) -> Kp< std::sync::Arc, @@ -1866,13 +1862,12 @@ where ) } - /// set fn is converting fn pointer to Fn closure #[inline] fn into_set(self) -> impl Fn(MutRoot) -> Option { self.set } - + /// get fn is converting fn pointer to Fn closure #[inline] fn into_get(self) -> impl Fn(Root) -> Option { @@ -1906,15 +1901,13 @@ 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) -> Option - { + fn get_optional(&self, root: Option) -> Option { 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) -> Option - { + fn get_mut_optional(&self, root: Option) -> Option { root.and_then(|r| (self.set)(r)) } @@ -1922,7 +1915,7 @@ where #[inline] fn get_or_else(&self, root: Root, f: F) -> Value where - F: FnOnce() -> Value + F: FnOnce() -> Value, { (self.get)(root).unwrap_or_else(f) } @@ -1986,7 +1979,6 @@ where { } - impl Kp where Root: std::borrow::Borrow, @@ -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)