#field #const #macro-derive #numbers #constant #count #struct

macro const_field_count

A Rust procedural macro to derive a constant function for counting the number of fields in a struct

1 unstable release

0.1.0 Jul 15, 2024

#731 in Procedural macros

32 downloads per month

MIT license

4KB

Const Field Count

Const Field Count is a Rust procedural macro that allows you to derive a constant function to count the number of fields in a struct. This can be useful for compile-time checks and optimizations.

Usage

To use the FieldCount derive macro, add the following to your Cargo.toml:

[dependencies]
const_field_count = "0.1.0"

Then, in your Rust code, you can derive the FieldCount trait for your structs:

use const_field_count::FieldCount;

#[derive(FieldCount)]
struct TokenInfo {
    _name: String,
    _price: i32,
}

fn main() {
    assert_eq!(TokenInfo::field_count(), 2);
}

Limitations

  • The FieldCount macro can only be derived for structs. Attempting to use it on enums or unions will result in a compile-time panic.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~205–640KB
~15K SLoC