[−][src]Struct synstructure::VariantInfo
A wrapper around a syn::DeriveInput
's variant which provides utilities
for destructuring Variant
s with match
expressions.
Fields
prefix: Option<&'a Ident>
Methods
impl<'a> VariantInfo<'a>
[src]
impl<'a> VariantInfo<'a>
pub fn bindings(&self) -> &[BindingInfo<'a>]
[src]
pub fn bindings(&self) -> &[BindingInfo<'a>]
Returns a slice of the bindings in this Variant.
pub fn bindings_mut(&mut self) -> &mut [BindingInfo<'a>]
[src]
pub fn bindings_mut(&mut self) -> &mut [BindingInfo<'a>]
Returns a mut slice of the bindings in this Variant.
pub fn ast(&self) -> VariantAst<'a>
[src]
pub fn ast(&self) -> VariantAst<'a>
Returns a VariantAst
object which contains references to the
underlying syn
AST node which this Variant
was created from.
pub fn omitted_bindings(&self) -> bool
[src]
pub fn omitted_bindings(&self) -> bool
True if any bindings were omitted due to a filter
call.
pub fn pat(&self) -> TokenStream
[src]
pub fn pat(&self) -> TokenStream
Generates the match-arm pattern which could be used to match against this Variant.
Example
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() );
pub fn construct<F, T>(&self, func: F) -> TokenStream where
F: FnMut(&Field, usize) -> T,
T: ToTokens,
[src]
pub fn construct<F, T>(&self, func: F) -> TokenStream where
F: FnMut(&Field, usize) -> T,
T: ToTokens,
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
.
Example
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() );
pub fn each<F, R>(&self, f: F) -> TokenStream where
F: FnMut(&BindingInfo) -> R,
R: ToTokens,
[src]
pub fn each<F, R>(&self, f: F) -> TokenStream where
F: FnMut(&BindingInfo) -> R,
R: ToTokens,
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.
Example
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() );
pub fn fold<F, I, R>(&self, init: I, f: F) -> TokenStream where
F: FnMut(TokenStream, &BindingInfo) -> R,
I: ToTokens,
R: ToTokens,
[src]
pub fn fold<F, I, R>(&self, init: I, f: F) -> TokenStream where
F: FnMut(TokenStream, &BindingInfo) -> R,
I: ToTokens,
R: ToTokens,
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.
Example
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() );
ⓘImportant traits for &'a mut Rpub fn filter<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> bool,
[src]
pub fn filter<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> bool,
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
orunsafe_bound_impl
method only consider type parameters referenced in the types of non-filtered fields.
Example
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() );
ⓘImportant traits for &'a mut Rpub fn remove_binding(&mut self, idx: usize) -> &mut Self
[src]
pub fn remove_binding(&mut self, idx: usize) -> &mut Self
ⓘImportant traits for &'a mut Rpub fn bind_with<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> BindStyle,
[src]
pub fn bind_with<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> BindStyle,
Updates the BindStyle
for each of the passed-in fields by calling the
passed-in function for each BindingInfo
.
Example
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() );
ⓘImportant traits for &'a mut Rpub fn binding_name<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&Field, usize) -> Ident,
[src]
pub fn binding_name<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&Field, usize) -> Ident,
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.
Example
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() );
pub fn referenced_ty_params(&self) -> Vec<&'a Ident>
[src]
pub fn referenced_ty_params(&self) -> Vec<&'a Ident>
Returns a list of the type parameters which are referenced in this field's type.
Caveat
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.
Example
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()))] );
Trait Implementations
impl<'a> Debug for VariantInfo<'a>
[src]
impl<'a> Debug for VariantInfo<'a>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a> Clone for VariantInfo<'a>
[src]
impl<'a> Clone for VariantInfo<'a>
fn clone(&self) -> VariantInfo<'a>
[src]
fn clone(&self) -> VariantInfo<'a>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<'a> PartialEq for VariantInfo<'a>
[src]
impl<'a> PartialEq for VariantInfo<'a>
fn eq(&self, other: &VariantInfo<'a>) -> bool
[src]
fn eq(&self, other: &VariantInfo<'a>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &VariantInfo<'a>) -> bool
[src]
fn ne(&self, other: &VariantInfo<'a>) -> bool
This method tests for !=
.
impl<'a> Eq for VariantInfo<'a>
[src]
impl<'a> Eq for VariantInfo<'a>
impl<'a> Hash for VariantInfo<'a>
[src]
impl<'a> Hash for VariantInfo<'a>
Auto Trait Implementations
impl<'a> !Send for VariantInfo<'a>
impl<'a> !Send for VariantInfo<'a>
impl<'a> !Sync for VariantInfo<'a>
impl<'a> !Sync for VariantInfo<'a>
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
[src]
fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)
[src]
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
ⓘImportant traits for &'a mut Rfn borrow(&self) -> &T
[src]
fn borrow(&self) -> &T
Immutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
ⓘImportant traits for &'a mut Rfn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<E> SpecializationError for E
[src]
impl<E> SpecializationError for E
fn not_found<S, T>(trait_name: &'static str, method_name: &'static str) -> E where
T: ?Sized,
[src]
fn not_found<S, T>(trait_name: &'static str, method_name: &'static str) -> E where
T: ?Sized,
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Create an error for a missing method specialization. Defaults to panicking with type, trait & method names. S
is the encoder/decoder state type, T
is the type being encoded/decoded, and the arguments are the names of the trait and method that should've been overridden. Read more
impl<T> Erased for T
[src]
impl<T> Erased for T
impl<T> Send for T where
T: ?Sized,
[src]
impl<T> Send for T where
T: ?Sized,
impl<T> Sync for T where
T: ?Sized,
[src]
impl<T> Sync for T where
T: ?Sized,
impl<T> Erased for T
[src]
impl<T> Erased for T