Enum Fields

pub 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) -> 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(&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) -> usize

Returns the number of fields.

fn is_empty(&self) -> bool

Returns true if there are zero fields.

fn members(&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 {}.

Trait Implementations

impl Clone for Fields

fn clone(&self) -> Self

impl IntoIterator for Fields

type Item = Field;
type IntoIter = IntoIter<Field>;
fn into_iter(self) -> Self::IntoIter

impl ToTokens for Fields

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

Auto Trait Implementations

impl !Send for Fields

impl !Sync for Fields

impl Freeze for Fields

impl RefUnwindSafe for Fields

impl Unpin for Fields

impl UnsafeUnpin for Fields

impl UnwindSafe for Fields

Blanket Implementations

impl<T> Any for Fields where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Fields where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Fields where T: ?Sized,

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

impl<T> CloneToUninit for Fields where T: Clone,

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

impl<T> From<T> for Fields

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for Fields where T: Spanned + ?Sized,

fn span(&self) -> Span

impl<T> ToOwned for Fields where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Fields where U: From<T>,

fn into(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<U> for Fields where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Fields where U: TryFrom<T>,

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