const_field_count

Crates.ioconst_field_count
lib.rsconst_field_count
version0.1.0
sourcesrc
created_at2024-07-15 09:33:42.999081
updated_at2024-07-15 09:33:42.999081
descriptionA Rust procedural macro to derive a constant function for counting the number of fields in a struct.
homepage
repositoryhttps://github.com/Cupnfish/const_field_count
max_upload_size
id1303637
size5,527
邻二氮杂菲 (Cupnfish)

documentation

README

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.

Commit count: 0

cargo fmt