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>
impl<'v, V: VariantInfo<F>, F: FieldInfo> VariantsHelper<'v, V, F>
Sourcepub fn new(variants: &'v [V]) -> Self
pub fn new(variants: &'v [V]) -> Self
Builds a new VariantsHelper
Sourcepub fn filtering_variants<P>(self, predicate: P) -> Self
pub fn filtering_variants<P>(self, predicate: P) -> Self
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.
Sourcepub fn with_variant_attributes<P>(self, predicate: P) -> Self
pub fn with_variant_attributes<P>(self, predicate: P) -> Self
Adds an arbitrary number of attributes to each variant.
Sourcepub fn include_extra_variants(
self,
include_extra_variants: impl IntoIterator<Item = (impl ToTokens, Option<impl ToTokens>)>,
) -> Self
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.
Sourcepub fn ignore_all_extra_variants(self, right_side: Option<TokenStream>) -> Self
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.
Sourcepub fn include_wrapper(self, include_wrapper: bool) -> Self
pub fn include_wrapper(self, include_wrapper: bool) -> Self
Wether to include the wrapper (curly braces), defaults to true
.
Sourcepub fn left_collector<C>(self, left_collector: C) -> Self
pub fn left_collector<C>(self, left_collector: C) -> Self
Specifies the left collector.
Defaults to VariantsCollector::variant_definition
Sourcepub fn right_collector<C>(self, right_collector: C) -> Self
pub fn right_collector<C>(self, right_collector: C) -> Self
Specifies the right collector.
Defaults to VariantsCollector::empty
Sourcepub fn collect(self) -> TokenStream
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,
},
}