https://github.com/rushiimachine/field-count
Derive the field count for a struct/enum variant.
https://github.com/rushiimachine/field-count
Last synced: about 2 months ago
JSON representation
Derive the field count for a struct/enum variant.
- Host: GitHub
- URL: https://github.com/rushiimachine/field-count
- Owner: rushiiMachine
- License: mit
- Created: 2023-11-27T00:07:23.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-27T00:15:08.000Z (over 1 year ago)
- Last Synced: 2025-03-19T22:11:32.234Z (about 2 months ago)
- Language: Rust
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# field_count
Derive the field count for a struct / enum variants, generating implementations for the `FieldCount`
and `EnumFieldCount` traits, respectively.This is fork and continuation of the original [field_count](https://github.com/discosultan/field-count) project that is
unmaintained.## Getting Started
```toml
# Cargo.toml[dependencies]
field_count = { git = "https://github.com/rushiiMachine/field-count" }
``````rust
use field_count::FieldCount;// Two impls are generated, one implementing the FieldCount trait,
// and the other a freestanding impl that has a const version of `field_count`
#[derive(FieldCount)]
struct MyStruct {
first_field: i32,
second_field: String,
third_field: u16,
}fn main() {
assert_eq!(MyStruct::field_count(), 3);
}
``````rust
use field_count::EnumFieldCount;#[derive(EnumFieldCount)]
enum MyGenericEnum {
Basic,
Gen(T),
}fn main() {
let b: MyGenericEnum = MyGenericEnum::Basic::;
let g = MyGenericEnum::Gen(vec![1, 2, 3]);assert_eq!(b.field_count(), 0);
assert_eq!(g.field_count(), 1);
}
```## Credits
This crate was inspired by [the following StackOverflow answer](https://stackoverflow.com/a/54177920/1466456)
by [Lukas Kalbertodt](https://github.com/LukasKalbertodt).