pub struct PKp<Root> { /* private fields */ }Implementations§
Source§impl<Root> PKp<Root>where
Root: 'static,
impl<Root> PKp<Root>where
Root: 'static,
Sourcepub fn new<'a, V>(keypath: KpType<'a, Root, V>) -> Selfwhere
V: Any + 'static,
pub fn new<'a, V>(keypath: KpType<'a, Root, V>) -> Selfwhere
V: Any + 'static,
Create a new PKp from a KpType (the common reference-based keypath)
Sourcepub fn from<'a, V>(keypath: KpType<'a, Root, V>) -> Selfwhere
V: Any + 'static,
pub fn from<'a, V>(keypath: KpType<'a, Root, V>) -> Selfwhere
V: Any + 'static,
Create a PKp from a KpType (alias for new())
Sourcepub fn value_type_id(&self) -> TypeId
pub fn value_type_id(&self) -> TypeId
Get the TypeId of the Value type
Sourcepub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<&'a Value>
pub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<&'a Value>
Try to downcast the result to a specific type
Sourcepub fn for_option(&self) -> PKp<Option<Root>>
pub fn for_option(&self) -> PKp<Option<Root>>
Adapt this keypath to work with Option
Sourcepub fn for_result<E>(&self) -> PKp<Result<Root, E>>where
E: 'static,
pub fn for_result<E>(&self) -> PKp<Result<Root, E>>where
E: 'static,
Adapt this keypath to work with Result<Root, E> instead of Root
Sourcepub fn map<OrigValue, MappedValue, F>(&self, mapper: F) -> PKp<Root>where
OrigValue: Any + 'static,
MappedValue: Any + 'static,
F: Fn(&OrigValue) -> MappedValue + 'static,
pub fn map<OrigValue, MappedValue, F>(&self, mapper: F) -> PKp<Root>where
OrigValue: Any + 'static,
MappedValue: Any + 'static,
F: Fn(&OrigValue) -> MappedValue + 'static,
Map the value through a transformation function The mapped value must also implement Any for type erasure
§Example
use rust_key_paths::{Kp, KpType, PKp};
struct User { name: String }
let user = User { name: "Alice".to_string() };
let name_kp = KpType::new(|u: &User| Some(&u.name), |_| None);
let name_pkp = PKp::new(name_kp);
let len_pkp = name_pkp.map::<String, _, _>(|s| s.len());
assert_eq!(len_pkp.get_as::<usize>(&user), Some(&5));Sourcepub fn filter<Value, F>(&self, predicate: F) -> PKp<Root>
pub fn filter<Value, F>(&self, predicate: F) -> PKp<Root>
Filter the value based on a predicate with type checking Returns None if the type doesn’t match or predicate fails
§Example
use rust_key_paths::{Kp, KpType, PKp};
struct User { age: i32 }
let user = User { age: 30 };
let age_kp = KpType::new(|u: &User| Some(&u.age), |_| None);
let age_pkp = PKp::new(age_kp);
let adult_pkp = age_pkp.filter::<i32, _>(|age| *age >= 18);
assert_eq!(adult_pkp.get_as::<i32>(&user), Some(&30));Auto Trait Implementations§
impl<Root> Freeze for PKp<Root>
impl<Root> !RefUnwindSafe for PKp<Root>
impl<Root> !Send for PKp<Root>
impl<Root> !Sync for PKp<Root>
impl<Root> Unpin for PKp<Root>where
Root: Unpin,
impl<Root> !UnwindSafe for PKp<Root>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more