#struct-fields #field-name #struct #name #macro

no-std field

Statically-verified struct field names as strings

1 unstable release

0.1.0 Jul 6, 2023

#28 in #field-name

MIT/Apache

8KB

field

Statically verified struct field names as strings.

See the documentation for more details.

Installation

Add the following to your Cargo manifest (Cargo.toml file):

[dependencies]
field = "0.1.0"

Or, just run cargo add field.

Dependencies

field has zero dependencies, and is #[no_std]-compatible by default.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.


lib.rs:

Statically-verified struct field names as strings.

The field! macro evaluates to a field's name after verifying that the field exists on the specified struct. The type must be in scope, and the field must be visible (accessible) at the point of invocation of the macro.

Usage

Invoke the macro as field!(field @ Struct):

use field::*;

struct User {
    name: String,
    age: u32,
}

let name = field!(name @ User);
assert_eq!(name, "name");

let age = field!(age @ User);
assert_eq!(age, "age");

A field that is not on the specified struct or a type that is not in scope will cause a compilation error:

// This fails because there is no field named "address" on "User"
let address = field!(address @ User);

// This fails because their is no struct named "NonExistent"
let foo = field!(whatever @ NonExistent);

Generics

field! also works with generic types, as long as concrete type parameters are provided:

use field::*;

struct Pair<T> {
    first: T,
    second: T,
}

let first = field!(first @ Pair<()>);
assert_eq!(first, "first");

// Any type can be used for the type parameter(s)
let second = field!(second @ Pair<i32>);
assert_eq!(second, "second");

Paths

That's right, field! also works with path syntax:

use field::*;

mod wrap {
    pub struct Wrap<T> {
        pub inner: T, // Must be pub so that it is visible at the point of invocation
    }
}

let inner = field!(inner @ wrap::Wrap<()>);
assert_eq!(inner, "inner");

Visibility

The specified field and struct must be visible (aka accessible) at the point of invocation of the macro:

use field::*;

mod int {
    pub struct Int {
        pub int: i32,
    }
}

let int = field!(int @ int::Int);
assert_eq!(int, "int");

A type or field that is not visible will result in a compilation error:

use field::*;

mod example {
    struct Secret {
        pub secret: String,
    }

    pub struct Empty {
        empty: ()
    }
}

// This fails because the "Secret" struct is not visible to the outer module
let secret = field!(secret @ example::Secret);

// This fails because the "empty" field is not visible to the outer module
let empty = field!(empty @ example::Empty);

Dependencies

This crate is completely dependency-free. #[no_std] is also supported by default.

No runtime deps