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, ) -> Self

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 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) -> Option<MappedValue>, impl Fn(MutRoot) -> Option<MappedValue>, impl Fn(MappedValue) -> Enum>
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) -> Option<Value>, impl Fn(MutRoot) -> Option<MutValue>, 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));

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

§

impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Sync for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
where E: Sync, G: Sync, S: Sync, Enum: Sync, Variant: Sync, Root: Sync, Value: Sync, MutRoot: Sync, MutValue: Sync,

§

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> 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> 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.