EnumKeyPath

Struct EnumKeyPath 

Source
pub struct EnumKeyPath<Enum = (), Variant = (), ExtractFn = fn(&()) -> Option<&()>, EmbedFn = fn(())>
where ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static, EmbedFn: Fn(Variant) -> Enum + 'static,
{ /* private fields */ }
Expand description

EnumKeyPath - A keypath for enum variants that supports both extraction and embedding Uses generic type parameters instead of dynamic dispatch for zero-cost abstraction

This struct serves dual purpose:

  1. As a concrete keypath instance: EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
  2. As a namespace for static factory methods: EnumKeyPath::readable_enum(...)

Implementations§

Source§

impl<Enum, Variant, ExtractFn, EmbedFn> EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static, EmbedFn: Fn(Variant) -> Enum + 'static,

Source

pub fn new(extractor: ExtractFn, embedder: EmbedFn) -> Self

Create a new EnumKeyPath with extractor and embedder functions

Source

pub fn get<'r>(&self, enum_value: &'r Enum) -> Option<&'r Variant>

Extract the value from an enum variant

Source

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

Embed a value into the enum variant

Source

pub fn as_optional(&self) -> &OptionalKeyPath<Enum, Variant, ExtractFn>

Get the underlying OptionalKeyPath for composition

Source

pub fn to_optional(self) -> OptionalKeyPath<Enum, Variant, ExtractFn>

Convert to OptionalKeyPath (loses embedding capability but gains composition)

Source§

impl EnumKeyPath

Source

pub fn readable_enum<Enum, Variant, ExtractFn, EmbedFn>( embedder: EmbedFn, extractor: ExtractFn, ) -> EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static, EmbedFn: Fn(Variant) -> Enum + 'static,

Create a readable enum keypath with both extraction and embedding Returns an EnumKeyPath that supports both get() and embed() operations

Source

pub fn for_variant<Enum, Variant, ExtractFn>( extractor: ExtractFn, ) -> OptionalKeyPath<Enum, Variant, impl for<'r> Fn(&'r Enum) -> Option<&'r Variant>>
where ExtractFn: Fn(&Enum) -> Option<&Variant>,

Extract from a specific enum variant

Source

pub fn for_match<Enum, Output, MatchFn>( matcher: MatchFn, ) -> KeyPath<Enum, Output, impl for<'r> Fn(&'r Enum) -> &'r Output>
where MatchFn: Fn(&Enum) -> &Output,

Match against multiple variants (returns a tagged union)

Source

pub fn for_ok<T, E>() -> OptionalKeyPath<Result<T, E>, T, impl for<'r> Fn(&'r Result<T, E>) -> Option<&'r T>>

Extract from Result<T, E> - Ok variant

Source

pub fn for_err<T, E>() -> OptionalKeyPath<Result<T, E>, E, impl for<'r> Fn(&'r Result<T, E>) -> Option<&'r E>>

Extract from Result<T, E> - Err variant

Source

pub fn for_some<T>() -> OptionalKeyPath<Option<T>, T, impl for<'r> Fn(&'r Option<T>) -> Option<&'r T>>

Extract from Option - Some variant

Source

pub fn for_option<T>() -> OptionalKeyPath<Option<T>, T, impl for<'r> Fn(&'r Option<T>) -> Option<&'r T>>

Extract from Option - Some variant (alias for for_some for consistency)

Source

pub fn for_box<T>() -> KeyPath<Box<T>, T, impl for<'r> Fn(&'r Box<T>) -> &'r T>

Unwrap Box -> T

Source

pub fn for_arc<T>() -> KeyPath<Arc<T>, T, impl for<'r> Fn(&'r Arc<T>) -> &'r T>

Unwrap Arc -> T

Source

pub fn for_rc<T>() -> KeyPath<Rc<T>, T, impl for<'r> Fn(&'r Rc<T>) -> &'r T>

Unwrap Rc -> T

Source

pub fn for_box_mut<T>() -> WritableKeyPath<Box<T>, T, impl for<'r> Fn(&'r mut Box<T>) -> &'r mut T>

Unwrap Box -> T (mutable)

Auto Trait Implementations§

§

impl<Enum, Variant, ExtractFn, EmbedFn> Freeze for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: Freeze, ExtractFn: Freeze,

§

impl<Enum, Variant, ExtractFn, EmbedFn> RefUnwindSafe for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: RefUnwindSafe, ExtractFn: RefUnwindSafe, Enum: RefUnwindSafe, Variant: RefUnwindSafe,

§

impl<Enum, Variant, ExtractFn, EmbedFn> Send for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: Send, ExtractFn: Send, Enum: Send, Variant: Send,

§

impl<Enum, Variant, ExtractFn, EmbedFn> Sync for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: Sync, ExtractFn: Sync, Enum: Sync, Variant: Sync,

§

impl<Enum, Variant, ExtractFn, EmbedFn> Unpin for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: Unpin, ExtractFn: Unpin, Enum: Unpin, Variant: Unpin,

§

impl<Enum, Variant, ExtractFn, EmbedFn> UnwindSafe for EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
where EmbedFn: UnwindSafe, ExtractFn: UnwindSafe, Enum: UnwindSafe, Variant: 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.