Skip to main content

EnumKp

Struct EnumKp 

Source
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:

  1. As a concrete keypath instance for extracting and embedding enum variants
  2. 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>
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,

Source

pub fn new( extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>, embedder: E, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>

Create a new EnumKp with extractor and embedder functions

Source

pub fn get(&self, enum_value: Root) -> Option<Value>

Extract the variant from an enum (returns None if wrong variant)

Source

pub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue>

Extract the variant mutably from an enum (returns None if wrong variant)

Source

pub fn embed(&self, value: Variant) -> Enum

Embed a value into the enum variant

Source

pub fn get_ref<'a>(&self, enum_value: &'a Enum) -> Option<&'a Variant>
where G: for<'b> Fn(&'b Enum) -> Option<&'b Variant>,

Read when the extractor uses reference-shaped getters (e.g. #[derive(Kp)] enum variants).

Source

pub fn embed_fn(&self) -> E
where E: Copy,

Copyable embed fn when the embedder is a function pointer.

Source

pub fn then<Leaf, G2, S2, E2>( self, inner: EnumKp<Variant, Leaf, &'static Variant, &'static Leaf, &'static mut Variant, &'static mut Leaf, G2, S2, E2>, ) -> EnumKp<Enum, Leaf, &'static Enum, &'static Leaf, &'static mut Enum, &'static mut Leaf, impl for<'b> Fn(&'b Enum), impl for<'b> Fn(&'b mut Enum), impl Fn(Leaf) + Copy>
where Enum: 'static, Variant: 'static, Leaf: 'static, G: for<'b> Fn(&'b Enum) -> Option<&'b Variant>, S: for<'b> Fn(&'b mut Enum) -> Option<&'b mut Variant>, E: Fn(Variant) -> Enum + Copy, G2: for<'b> Fn(&'b Variant) -> Option<&'b Leaf>, S2: for<'b> Fn(&'b mut Variant) -> Option<&'b mut Leaf>, E2: Fn(Leaf) -> Variant + Copy,

Append an inner casepath (Swift CasePaths append).

Composes extract and embed through two single-payload variants:

let cp = RootAction::app_cp()
    .then(AppAction::panel_cp())
    .then(PanelAction::widget_cp());

cp.get_ref(&root);              // extract leaf by reference
cp.embed(WidgetAction::Submit); // embed leaf into root enum
Source

pub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>

Get the underlying Kp for composition with other keypaths

Source

pub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>

Convert to Kp (loses embedding capability but gains composition)

Source

pub fn map<MappedValue, F>( &self, mapper: F, ) -> EnumKp<Enum, MappedValue, Root, MappedValue, MutRoot, MappedValue, impl Fn(Root), impl Fn(MutRoot), impl Fn(MappedValue)>
where F: Fn(&Variant) -> MappedValue + Copy + 'static, Variant: 'static, MappedValue: 'static, E: Fn(Variant) -> Enum + Copy + 'static,

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

pub fn filter<F>( &self, predicate: F, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, impl Fn(Root), impl Fn(MutRoot), E>
where F: Fn(&Variant) -> bool + Copy + 'static, Variant: 'static, E: Copy,

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

Trait Implementations§

Source§

impl<Parent, Child, G, S, E> Casepath<Parent, Child> for EnumKp<Parent, Child, &'static Parent, &'static Child, &'static mut Parent, &'static mut Child, G, S, E>
where Parent: 'static, Child: Clone + 'static, G: for<'b> Fn(&'b Parent) -> Option<&'b Child>, S: for<'b> Fn(&'b mut Parent) -> Option<&'b mut Child>, E: Fn(Child) -> Parent + Clone,

Source§

fn extract(&self, parent: &Parent) -> Option<Child>

Source§

fn wrap(&self, child: Child) -> Parent

Source§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Clone for 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> + Clone, S: Fn(MutRoot) -> Option<MutValue> + Clone, E: Fn(Variant) -> Enum + Clone,

Source§

fn clone( &self, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, R, V> Copy for EnumKp<R, V, &'a R, &'a V, &'a mut R, &'a mut V, for<'b> fn(&'b R) -> Option<&'b V>, for<'b> fn(&'b mut R) -> Option<&'b mut V>, fn(V) -> R>
where R: 'static, V: 'static,

Source§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Debug for 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,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Display for 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,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Send for 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> + Send, S: Fn(MutRoot) -> Option<MutValue> + Send, E: Fn(Variant) -> Enum + Send,

Source§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Sync for 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> + Sync, S: Fn(MutRoot) -> Option<MutValue> + Sync, E: Fn(Variant) -> Enum + Sync,

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>
where E: Freeze, G: Freeze, S: Freeze,

§

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> Unpin for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
where E: Unpin, G: Unpin, S: Unpin, Enum: Unpin, Variant: Unpin, Root: Unpin, Value: Unpin, MutRoot: Unpin, MutValue: Unpin,

§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> UnsafeUnpin 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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V