Skip to main content

FieldsExt

Trait FieldsExt 

Source
pub trait FieldsExt {
    // Required methods
    fn is_named(&self) -> bool;
    fn is_unnamed(&self) -> bool;
    fn is_unit(&self) -> bool;
    fn as_named(&self) -> Option<&FieldsNamed>;
    fn as_unnamed(&self) -> Option<&FieldsUnnamed>;
    fn exists(&self, key: &FieldKey) -> bool;
    fn get(&self, key: &FieldKey) -> Option<&Field>;
    fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>;
    fn span(&self) -> Span;
}
Expand description

Extension methods for types that contain syn::Fields.

Provides variant predicates (is_named, is_unnamed, is_unit), conversions (as_named, as_unnamed), and field lookup via FieldKey.

Implemented for syn::Fields, syn::ItemStruct, syn::DataStruct, and syn::Variant.

§Examples

use zyn::ext::{FieldsExt, FieldKey};

#[zyn::element]
fn my_element(fields: zyn::Fields) -> zyn::TokenStream {
    if fields.is_named() {
        let f = fields.get(&"id".into());
    }
    // ...
}

Required Methods§

Source

fn is_named(&self) -> bool

Returns true if the fields are named (struct { a: T, b: U }).

Source

fn is_unnamed(&self) -> bool

Returns true if the fields are unnamed (tuple (T, U)).

Source

fn is_unit(&self) -> bool

Returns true if there are no fields (unit struct).

Source

fn as_named(&self) -> Option<&FieldsNamed>

Returns the inner syn::FieldsNamed if the fields are named.

Source

fn as_unnamed(&self) -> Option<&FieldsUnnamed>

Returns the inner syn::FieldsUnnamed if the fields are unnamed.

Source

fn exists(&self, key: &FieldKey) -> bool

Returns true if a field matching the given FieldKey exists.

Source

fn get(&self, key: &FieldKey) -> Option<&Field>

Returns the first field matching the given FieldKey, or None.

Source

fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>

Returns all fields as (FieldKey, &Field) pairs.

Named fields yield FieldKey::Named, unnamed fields yield FieldKey::Index, and unit fields yield an empty vec.

§Examples
use zyn::ext::{FieldsExt, FieldKey};

for (key, field) in fields.keyed() {
    println!("{}: {:?}", key, field.ty);
}
Source

fn span(&self) -> Span

Returns the span of the fields.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FieldsExt for Fields

Source§

fn is_named(&self) -> bool

Source§

fn is_unnamed(&self) -> bool

Source§

fn is_unit(&self) -> bool

Source§

fn as_named(&self) -> Option<&FieldsNamed>

Source§

fn as_unnamed(&self) -> Option<&FieldsUnnamed>

Source§

fn exists(&self, key: &FieldKey) -> bool

Source§

fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>

Source§

fn get(&self, key: &FieldKey) -> Option<&Field>

Source§

fn span(&self) -> Span

Source§

impl FieldsExt for Variant

Source§

fn is_named(&self) -> bool

Source§

fn is_unnamed(&self) -> bool

Source§

fn is_unit(&self) -> bool

Source§

fn as_named(&self) -> Option<&FieldsNamed>

Source§

fn as_unnamed(&self) -> Option<&FieldsUnnamed>

Source§

fn exists(&self, key: &FieldKey) -> bool

Source§

fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>

Source§

fn get(&self, key: &FieldKey) -> Option<&Field>

Source§

fn span(&self) -> Span

Source§

impl FieldsExt for DataStruct

Source§

fn is_named(&self) -> bool

Source§

fn is_unnamed(&self) -> bool

Source§

fn is_unit(&self) -> bool

Source§

fn as_named(&self) -> Option<&FieldsNamed>

Source§

fn as_unnamed(&self) -> Option<&FieldsUnnamed>

Source§

fn exists(&self, key: &FieldKey) -> bool

Source§

fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>

Source§

fn get(&self, key: &FieldKey) -> Option<&Field>

Source§

fn span(&self) -> Span

Source§

impl FieldsExt for ItemStruct

Source§

fn is_named(&self) -> bool

Source§

fn is_unnamed(&self) -> bool

Source§

fn is_unit(&self) -> bool

Source§

fn as_named(&self) -> Option<&FieldsNamed>

Source§

fn as_unnamed(&self) -> Option<&FieldsUnnamed>

Source§

fn exists(&self, key: &FieldKey) -> bool

Source§

fn keyed(&self) -> impl Iterator<Item = (FieldKey, &Field)>

Source§

fn get(&self, key: &FieldKey) -> Option<&Field>

Source§

fn span(&self) -> Span

Implementors§