Provides StructFieldNames
derive macro.
```rust
struct SomeStruct {
fieldone: i32,
fieldtwo: Vec
generates
rust
struct SomeStructFieldStaticStr {
fieldone: &'static str,
fieldtwo: &'static str,
}
impl SomeStruct {
const FIELDNAMES: SomeStructFieldStaticStr = SomeStructFieldStaticStr {
fieldone: "fieldone",
fieldtwo: "fieldtwo",
};
}
which can be used like
rust
let fieldonename: &'static str = SomeStruct::FIELDNAMES.fieldone;
println!("{}", fieldone_name);
```
.
This is useful mostly for typo-proofing.
Credits to the field_types crate. A lot of code here is copied from there.
Use the StructFieldNames
derive macro like this:
```rust
struct SomeStruct {
fieldone: i32,
fieldtwo: Vec
then access the field name as `&'static str` like this:
rust
let fieldonename: &'static str = SomeStruct::FIELDNAMES.fieldone;
```
Use #[struct_field_names(skip)]
to skip fields.
With
```rust
struct Struct {
fieldone: bool,
#[structfieldnames(skip)]
fieldtwo: usize,
}
``
,
SomeStruct::FIELDNAMES.fieldtwo` won't exist.
Visibility of the field names struct follows your struct.
With ```rust
pub struct PublicStruct { pub publicfield: i32, privatefield: i32 }
struct PrivateStruct {
pub publicfield: i32,
privatefield: i32
}
``
, only
PublicStruct::FIELDNAMES.publicfield` would be available to the outside world.