Skip to main content

PKp

Struct PKp 

Source
pub struct PKp<Root> { /* private fields */ }

Implementations§

Source§

impl<Root> PKp<Root>
where Root: 'static,

Source

pub fn new<'a, V>(keypath: KpType<'a, Root, V>) -> Self
where V: Any + 'static,

Create a new PKp from a KpType (the common reference-based keypath)

Source

pub fn from<'a, V>(keypath: KpType<'a, Root, V>) -> Self
where V: Any + 'static,

Create a PKp from a KpType (alias for new())

Source

pub fn get<'r>(&self, root: &'r Root) -> Option<&'r dyn Any>

Get the value as a trait object

Source

pub fn value_type_id(&self) -> TypeId

Get the TypeId of the Value type

Source

pub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<&'a Value>

Try to downcast the result to a specific type

Source

pub fn kind_name(&self) -> String

Get a human-readable name for the value type

Source

pub fn for_arc(&self) -> PKp<Arc<Root>>

Adapt this keypath to work with Arc instead of Root

Source

pub fn for_box(&self) -> PKp<Box<Root>>

Adapt this keypath to work with Box instead of Root

Source

pub fn for_rc(&self) -> PKp<Rc<Root>>

Adapt this keypath to work with Rc instead of Root

Source

pub fn for_option(&self) -> PKp<Option<Root>>

Adapt this keypath to work with Option instead of Root

Source

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

Source

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));
Source

pub fn filter<Value, F>(&self, predicate: F) -> PKp<Root>
where Value: Any + 'static, F: Fn(&Value) -> bool + 'static,

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.