Skip to main content

FieldNames

Trait FieldNames 

Source
pub trait FieldNames<const N: usize> {
    type FieldName;

    // Required method
    fn field_names() -> [Self::FieldName; N];
}
Expand description

Implemented by structs that have a FieldName enum generated by the [FieldName] derive macro.

N is the number of non-skipped fields + nested fields, resolved at compile time. Deriving FieldName on a struct Foo with N non-skipped fields generates:

impl FieldNames<N> for Foo {
    type FieldName = FooFieldName;
    fn field_names() -> [FooFieldName; N] { ... }
}

Call field_names to get an ordered array of all field-name variants without needing a struct instance. Ordered by field declaration order.

§Example

use struct_to_enum::{FieldName, FieldNames};

#[derive(FieldName)]
struct Point {
    x: f32,
    y: f32,
}

let names: [PointFieldName; 2] = Point::field_names();
assert_eq!(names, [PointFieldName::X, PointFieldName::Y]);

Required Associated Types§

Required Methods§

Source

fn field_names() -> [Self::FieldName; N]

Returns all field-name enum variants in field declaration order.

NOTE: This method will be made const once const_trait_impl is stabilised. Tracking issue: https://github.com/rust-lang/rust/issues/67792

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.

Implementors§