pub struct VariantInfo<'a> {
pub prefix: Option<&'a Ident>,
// some fields omitted
}
A wrapper around a syn::DeriveInput
's variant which provides utilities
for destructuring Variant
s with match
expressions.
Returns a slice of the bindings in this Variant.
Returns a mut slice of the bindings in this Variant.
Returns a VariantAst
object which contains references to the
underlying syn
AST node which this Variant
was created from.
True if any bindings were omitted due to a filter
call.
Generates the match-arm pattern which could be used to match against this Variant.
let di: syn::DeriveInput = parse_quote! {
enum A {
B(i32, i32),
C(u32),
}
};
let s = Structure::new(&di);
assert_eq!(
s.variants()[0].pat().to_string(),
quote!{
A::B(ref __binding_0, ref __binding_1,)
}.to_string()
);
Generates the token stream required to construct the current variant.
The init array initializes each of the fields in the order they are
written in variant.ast().fields
.
let di: syn::DeriveInput = parse_quote! {
enum A {
B(usize, usize),
C{ v: usize },
}
};
let s = Structure::new(&di);
assert_eq!(
s.variants()[0].construct(|_, i| quote!(#i)).to_string(),
quote!{
A::B(0usize, 1usize,)
}.to_string()
);
assert_eq!(
s.variants()[1].construct(|_, i| quote!(#i)).to_string(),
quote!{
A::C{ v: 0usize, }
}.to_string()
);
Runs the passed-in function once for each bound field, passing in a BindingInfo
.
and generating a match
arm which evaluates the returned tokens.
This method will ignore fields which are ignored through the filter
method.
let di: syn::DeriveInput = parse_quote! {
enum A {
B(i32, i32),
C(u32),
}
};
let s = Structure::new(&di);
assert_eq!(
s.variants()[0].each(|bi| quote!(println!("{:?}", #bi))).to_string(),
quote!{
A::B(ref __binding_0, ref __binding_1,) => {
{ println!("{:?}", __binding_0) }
{ println!("{:?}", __binding_1) }
}
}.to_string()
);
Runs the passed-in function once for each bound field, passing in the
result of the previous call, and a BindingInfo
. generating a match
arm which evaluates to the resulting tokens.
This method will ignore fields which are ignored through the filter
method.
let di: syn::DeriveInput = parse_quote! {
enum A {
B(i32, i32),
C(u32),
}
};
let s = Structure::new(&di);
assert_eq!(
s.variants()[0].fold(quote!(0), |acc, bi| quote!(#acc + #bi)).to_string(),
quote!{
A::B(ref __binding_0, ref __binding_1,) => {
0 + __binding_0 + __binding_1
}
}.to_string()
);
Filter the bindings created by this Variant
object. This has 2 effects:
-
The bindings will no longer appear in match arms generated by methods
on this Variant
or its subobjects.
-
Impl blocks created with the bound_impl
or unsafe_bound_impl
method only consider type parameters referenced in the types of
non-filtered fields.
let di: syn::DeriveInput = parse_quote! {
enum A {
B{ a: i32, b: i32 },
C{ a: u32 },
}
};
let mut s = Structure::new(&di);
s.variants_mut()[0].filter(|bi| {
bi.ast().ident == Some(syn::Ident::new("b", proc_macro2::Span::call_site()))
});
assert_eq!(
s.each(|bi| quote!(println!("{:?}", #bi))).to_string(),
quote!{
A::B{ b: ref __binding_1, .. } => {
{ println!("{:?}", __binding_1) }
}
A::C{ a: ref __binding_0, } => {
{ println!("{:?}", __binding_0) }
}
}.to_string()
);
Remove the binding at the given index.
Panics if the index is out of range.
Updates the BindStyle
for each of the passed-in fields by calling the
passed-in function for each BindingInfo
.
let di: syn::DeriveInput = parse_quote! {
enum A {
B(i32, i32),
C(u32),
}
};
let mut s = Structure::new(&di);
s.variants_mut()[0].bind_with(|bi| BindStyle::RefMut);
assert_eq!(
s.each(|bi| quote!(println!("{:?}", #bi))).to_string(),
quote!{
A::B(ref mut __binding_0, ref mut __binding_1,) => {
{ println!("{:?}", __binding_0) }
{ println!("{:?}", __binding_1) }
}
A::C(ref __binding_0,) => {
{ println!("{:?}", __binding_0) }
}
}.to_string()
);
Updates the binding name for each fo the passed-in fields by calling the
passed-in function for each BindingInfo
.
The function will be called with the BindingInfo
and its index in the
enclosing variant.
The default name is __binding_{}
where {}
is replaced with an
increasing number.
let di: syn::DeriveInput = parse_quote! {
enum A {
B{ a: i32, b: i32 },
C{ a: u32 },
}
};
let mut s = Structure::new(&di);
s.variants_mut()[0].binding_name(|bi, i| bi.ident.clone().unwrap());
assert_eq!(
s.each(|bi| quote!(println!("{:?}", #bi))).to_string(),
quote!{
A::B{ a: ref a, b: ref b, } => {
{ println!("{:?}", a) }
{ println!("{:?}", b) }
}
A::C{ a: ref __binding_0, } => {
{ println!("{:?}", __binding_0) }
}
}.to_string()
);
Returns a list of the type parameters which are referenced in this
field's type.
If the field contains any macros in type position, all parameters will
be considered bound. This is because we cannot determine which type
parameters are bound by type macros.
let di: syn::DeriveInput = parse_quote! {
struct A<T, U> {
a: Option<T>,
b: U,
}
};
let mut s = Structure::new(&di);
assert_eq!(
s.variants()[0].bindings()[0].referenced_ty_params(),
&[&(syn::Ident::new("T", proc_macro2::Span::call_site()))]
);
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
Formats the value using the given formatter. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)