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

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

#2178 in Procedural macros

Download history 7/week @ 2025-10-16 80/week @ 2025-10-23 86/week @ 2025-10-30 27/week @ 2025-11-06 43/week @ 2025-11-13 57/week @ 2025-11-20 27/week @ 2025-11-27 11/week @ 2025-12-04 13/week @ 2025-12-11 20/week @ 2025-12-18 66/week @ 2025-12-25 45/week @ 2026-01-01 69/week @ 2026-01-08 122/week @ 2026-01-15 152/week @ 2026-01-22 101/week @ 2026-01-29

447 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

~130–510KB
~12K SLoC