All Projects → TedDriggs → field_names

TedDriggs / field_names

Licence: MIT license
proc-macro for accessing struct field names at runtime

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to field names

lombok-rs
Lombok port for Rust
Stars: ✭ 31 (+19.23%)
Mutual labels:  proc-macro
reacty yew
Generate Yew components from React components via Typescript type definitions
Stars: ✭ 46 (+76.92%)
Mutual labels:  proc-macro
real-async-trait-rs
A proc macro for real async traits, using nightly-only existential types and generic associated types to work around the need for type erasure
Stars: ✭ 38 (+46.15%)
Mutual labels:  proc-macro
proc-quote
A procedural macro implementation of `quote!`.
Stars: ✭ 22 (-15.38%)
Mutual labels:  proc-macro
maybe-async-rs
A procedure macro to unify SYNC and ASYNC implementation for downstream application/crates
Stars: ✭ 22 (-15.38%)
Mutual labels:  proc-macro
futures-async-stream
Async stream for Rust and the futures crate.
Stars: ✭ 141 (+442.31%)
Mutual labels:  proc-macro
superstruct
Rust library for versioned data types
Stars: ✭ 27 (+3.85%)
Mutual labels:  proc-macro
easy-ext
An attribute macro for easily writing extension trait pattern.
Stars: ✭ 17 (-34.62%)
Mutual labels:  proc-macro
litrs
Parsing and inspecting Rust literals (particularly useful for proc macros)
Stars: ✭ 25 (-3.85%)
Mutual labels:  proc-macro
rust-fsm
Finite state machine framework for Rust with readable specifications
Stars: ✭ 67 (+157.69%)
Mutual labels:  proc-macro

field_names

Build Status Latest Version

field_names is a Rust crate to expose a field or variant names from source code as strings at runtime.

Example

Consider a simple struct such as this one.

#[derive(FieldNames)]
struct Example {
    hello: String,
    world: String,
    #[field_names(skip)]
    ignore_me: bool,
}

field_names will emit the following:

#[automatically_derived]
impl Example {
    const FIELDS: [&'static str; 2] = [
        "hello",
        "world",
    ];
}

Enums are the same:

#[derive(VariantNames)]
enum Example {
    Hello(String),
    #[variant_names(skip)]
    Secret(String),
    World,
}

field_names will emit the following:

#[automatically_derived]
impl Example {
    const VARIANTS: [&'static str; 2] = [
        "Hello",
        "World",
    ];
}

Uses

This crate was originally created for a case where a set of rules were being read at runtime which referenced fields of structs elsewhere in the code base. The referenced struct exposed a method which had a match statement to go from strings to its fields, but there was not a way to ensure the arms of that match statement stayed in sync with the struct definition. With this crate, a unit test could be created to ensure that every field on the struct - except those deliberately omitted - was handled by the method.

This crate can also be used to enforce relationships among structs and enums at unit-test time that cannot be expressed at compile-time. See tests/keep_in_sync for an example and explanation of that scenario.

FAQs

Why aren't FieldNames and VariantNames traits?

Using field_names is an implementation convenience; it shouldn't force you to change your crate's public API.

How do I make FIELDS or VARIANTS public?

You can add your own inherent method, e.g. fields() -> &[&'static str], or define a trait that matches your use-case and reference FIELDS in the trait implementation.

Can I get field names for an enum variant?

This currently isn't supported, using newtype variants and separate structs per variant is currently the recommended approach. You can use the from_variants crate to auto-generate conversions from those structs into the enum.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].