Crates.io | const_field_count |
lib.rs | const_field_count |
version | 0.1.0 |
source | src |
created_at | 2024-07-15 09:33:42.999081 |
updated_at | 2024-07-15 09:33:42.999081 |
description | A Rust procedural macro to derive a constant function for counting the number of fields in a struct. |
homepage | |
repository | https://github.com/Cupnfish/const_field_count |
max_upload_size | |
id | 1303637 |
size | 5,527 |
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.
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);
}
FieldCount
macro can only be derived for structs. Attempting to use it on enums or unions will result in a compile-time panic.This project is licensed under the MIT License. See the LICENSE file for details.