Enum Fields

enum Fields

Data stored within an enum variant or struct.

Syntax tree enum

This type is a syntax tree enum.

Variants

Named(FieldsNamed)

Named fields of a struct or struct variant such as Point { x: f64, y: f64 }.

Unnamed(FieldsUnnamed)

Unnamed fields of a tuple struct or tuple variant such as Some(T).

Unit

Unit struct or unit variant such as None.

Implementations

impl Fields

fn iter(self: &Self) -> Iter<'_, Field>

Get an iterator over the borrowed Field items in this object. This iterator can be used to iterate over a named or unnamed struct or variant's fields uniformly.

fn iter_mut(self: &mut Self) -> IterMut<'_, Field>

Get an iterator over the mutably borrowed Field items in this object. This iterator can be used to iterate over a named or unnamed struct or variant's fields uniformly.

fn len(self: &Self) -> usize

Returns the number of fields.

fn is_empty(self: &Self) -> bool

Returns true if there are zero fields.

fn members(self: &Self) -> Members<'_>

Get an iterator over the fields of a struct or variant as Members. This iterator can be used to iterate over a named or unnamed struct or variant's fields uniformly.

Example

The following is a simplistic Clone derive for structs. (A more complete implementation would additionally want to infer trait bounds on the generic type parameters.)

# use quote::quote;
#
fn derive_clone(input: &syn::ItemStruct) -> proc_macro2::TokenStream {
    let ident = &input.ident;
    let members = input.fields.members();
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    quote! {
        impl #impl_generics Clone for #ident #ty_generics #where_clause {
            fn clone(&self) -> Self {
                Self {
                    #(#members: self.#members.clone()),*
                }
            }
        }
    }
}

For structs with named fields, it produces an expression like Self { a: self.a.clone() }. For structs with unnamed fields, Self { 0: self.0.clone() }. And for unit structs, Self {}.

impl Clone for Fields

fn clone(self: &Self) -> Self

impl Debug for Fields

fn fmt(self: &Self, formatter: &mut Formatter<'_>) -> Result

impl Eq for Fields

impl Freeze for Fields

impl From for Fields

fn from(e: FieldsUnnamed) -> Fields

impl From for Fields

fn from(e: FieldsNamed) -> Fields

impl Hash for Fields

fn hash<H>(self: &Self, state: &mut H)
where
    H: Hasher

impl IntoIterator for Fields

fn into_iter(self: Self) -> <Self as >::IntoIter

impl PartialEq for Fields

fn eq(self: &Self, other: &Self) -> bool

impl RefUnwindSafe for Fields

impl Send for Fields

impl Sync for Fields

impl ToTokens for Fields

fn to_tokens(self: &Self, tokens: &mut TokenStream)

impl Unpin for Fields

impl UnsafeUnpin for Fields

impl UnwindSafe for Fields

impl<T> Any for Fields

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Fields

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Fields

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Fields

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Fields

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for Fields

fn span(self: &Self) -> Span

impl<T> ToOwned for Fields

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Fields

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Fields

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Fields

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>