Struct VariantsHelper

Source
pub struct VariantsHelper<'v, V: VariantInfo<F>, F: FieldInfo> { /* private fields */ }
Expand description

Utility struct to work with enum’s variants

Implementations§

Source§

impl<'v, V: VariantInfo<F>, F: FieldInfo> VariantsHelper<'v, V, F>

Source

pub fn new(variants: &'v [V]) -> Self

Builds a new VariantsHelper

Source

pub fn filtering_variants<P>(self, predicate: P) -> Self
where P: Fn(&V) -> bool + 'v,

Remove all variants v for which predicate(&v) returns false. This method operates in place, visiting each element exactly once in the original order, and preserves the order of the retained elements.

Source

pub fn with_variant_attributes<P>(self, predicate: P) -> Self
where P: Fn(&V) -> Option<TokenStream> + 'v,

Adds an arbitrary number of attributes to each variant.

Source

pub fn include_extra_variants( self, include_extra_variants: impl IntoIterator<Item = (impl ToTokens, Option<impl ToTokens>)>, ) -> Self

Include extra variants by including the given left and right sides (#left if right is None or #left => #right) at the end.

Source

pub fn ignore_all_extra_variants(self, right_side: Option<TokenStream>) -> Self

Wether to ignore all extra variants with the given right side _ => #right, if Some.

It should be used only when collecting a match.

Source

pub fn include_wrapper(self, include_wrapper: bool) -> Self

Wether to include the wrapper (curly braces), defaults to true.

Source

pub fn left_collector<C>(self, left_collector: C) -> Self
where C: Fn(&V, FieldsHelper<'_, F>) -> TokenStream + 'v,

Specifies the left collector.

Defaults to VariantsCollector::variant_definition

Source

pub fn right_collector<C>(self, right_collector: C) -> Self
where C: Fn(&V, FieldsHelper<'_, F>) -> TokenStream + 'v,

Specifies the right collector.

Defaults to VariantsCollector::empty

Source

pub fn collect(self) -> TokenStream

Collects the fields.

§Examples

If using the default collectors:

{
    Variant1,
    Variant2 = 5,
    Variant3 {
        field_1: String,
        field_2: i32,
    },
}

If using VariantsCollector::variant_fields_collector(quote!(SelfTy)) left collector and VariantsCollector::variant_fields_collector(quote!(OtherTy) right collector:

{
    SelfTy::Variant1 => OtherTy::Variant1,
    SelfTy::Variant2 {
        field_1: field_1,
        field_2: field_2,
    } => OtherTy::Variant2 {
        field_1: field_1,
        field_2: field_2,
    },
}

Auto Trait Implementations§

§

impl<'v, V, F> Freeze for VariantsHelper<'v, V, F>

§

impl<'v, V, F> !RefUnwindSafe for VariantsHelper<'v, V, F>

§

impl<'v, V, F> !Send for VariantsHelper<'v, V, F>

§

impl<'v, V, F> !Sync for VariantsHelper<'v, V, F>

§

impl<'v, V, F> Unpin for VariantsHelper<'v, V, F>

§

impl<'v, V, F> !UnwindSafe for VariantsHelper<'v, V, F>

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.