pub struct EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>where
Root: Borrow<Enum>,
Value: Borrow<Variant>,
MutRoot: BorrowMut<Enum>,
MutValue: BorrowMut<Variant>,
G: Fn(Root) -> Option<Value>,
S: Fn(MutRoot) -> Option<MutValue>,
E: Fn(Variant) -> Enum,{ /* private fields */ }Expand description
EnumKp - A keypath for enum variants that supports both extraction and embedding
Leverages the existing Kp architecture where optionals are built-in via Option
This struct serves dual purposes:
- As a concrete keypath instance for extracting and embedding enum variants
- As a namespace for static factory methods:
EnumKp::for_ok(),EnumKp::for_some(), etc.
Implementations§
Source§impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
Sourcepub fn new(
extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>,
embedder: E,
) -> Self
pub fn new( extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>, embedder: E, ) -> Self
Create a new EnumKp with extractor and embedder functions
Sourcepub fn get(&self, enum_value: Root) -> Option<Value>
pub fn get(&self, enum_value: Root) -> Option<Value>
Extract the variant from an enum (returns None if wrong variant)
Sourcepub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue>
pub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue>
Extract the variant mutably from an enum (returns None if wrong variant)
Sourcepub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
pub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
Get the underlying Kp for composition with other keypaths
Sourcepub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
pub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
Convert to Kp (loses embedding capability but gains composition)
Sourcepub fn map<MappedValue, F>(
&self,
mapper: F,
) -> EnumKp<Enum, MappedValue, Root, MappedValue, MutRoot, MappedValue, impl Fn(Root) -> Option<MappedValue>, impl Fn(MutRoot) -> Option<MappedValue>, impl Fn(MappedValue) -> Enum>
pub fn map<MappedValue, F>( &self, mapper: F, ) -> EnumKp<Enum, MappedValue, Root, MappedValue, MutRoot, MappedValue, impl Fn(Root) -> Option<MappedValue>, impl Fn(MutRoot) -> Option<MappedValue>, impl Fn(MappedValue) -> Enum>
Map the variant value through a transformation function
§Example
use rust_key_paths::enum_ok;
let result: Result<String, i32> = Ok("hello".to_string());
let ok_kp = enum_ok();
let len_kp = ok_kp.map(|s: &String| s.len());
assert_eq!(len_kp.get(&result), Some(5));Sourcepub fn filter<F>(
&self,
predicate: F,
) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, impl Fn(Root) -> Option<Value>, impl Fn(MutRoot) -> Option<MutValue>, E>
pub fn filter<F>( &self, predicate: F, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, impl Fn(Root) -> Option<Value>, impl Fn(MutRoot) -> Option<MutValue>, E>
Filter the variant value based on a predicate Returns None if the predicate fails or if wrong variant
§Example
use rust_key_paths::enum_ok;
let result: Result<i32, String> = Ok(42);
let ok_kp = enum_ok();
let positive_kp = ok_kp.filter(|x: &i32| *x > 0);
assert_eq!(positive_kp.get(&result), Some(&42));Auto Trait Implementations§
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Freeze for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> RefUnwindSafe for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>where
E: RefUnwindSafe,
G: RefUnwindSafe,
S: RefUnwindSafe,
Enum: RefUnwindSafe,
Variant: RefUnwindSafe,
Root: RefUnwindSafe,
Value: RefUnwindSafe,
MutRoot: RefUnwindSafe,
MutValue: RefUnwindSafe,
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Send for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Sync for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Unpin for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> UnwindSafe for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>where
E: UnwindSafe,
G: UnwindSafe,
S: UnwindSafe,
Enum: UnwindSafe,
Variant: UnwindSafe,
Root: UnwindSafe,
Value: UnwindSafe,
MutRoot: UnwindSafe,
MutValue: UnwindSafe,
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