| Crates.io | enum-struct |
| lib.rs | enum-struct |
| version | 0.1.1 |
| created_at | 2025-09-21 07:47:25.186712+00 |
| updated_at | 2025-09-21 07:49:10.438969+00 |
| description | Add shared fields to each variant of the enum |
| homepage | |
| repository | https://github.com/A4-Tacks/enum-struct-rs |
| max_upload_size | |
| id | 1848577 |
| size | 13,431 |
Add shared fields to each variant of the enumeration
For example:
struct Foo {
pub id: u64,
pub data: FooData,
}
enum FooData {
Named(String),
Complex { name: String, age: u32 },
Empty,
}
Change to using enum_struct::fields:
#[enum_struct::fields { id: u64 }]
enum Foo {
Named(String),
Complex { name: String, age: u32 },
Empty,
}
Expand into:
enum Foo {
Named(u64, String),
Complex { id: u64, name: String, age: u32 },
Empty { id: u64 },
}
impl Foo {
fn id(&self) -> &u64 { /*...*/ }
fn id_mut(&mut self) -> &mut u64 { /*...*/ }
fn into_id(self) -> u64 { /*...*/ }
}
#[enum_struct::fields {
id: u64,
}]
#[derive(Debug, PartialEq)]
enum Foo {
Named(String),
Complex { name: String, age: u32 },
Empty,
}
let named = Foo::Named(2, "jack".into());
let complex = Foo::Complex { id: 3, name: "john".into(), age: 22 };
let empty = Foo::Empty { id: 4 };
assert_eq!(named.id(), &2);
assert_eq!(complex.id(), &3);
assert_eq!(empty.id(), &4);
let mut named = named;
*named.id_mut() = 8;
assert_eq!(named.id(), &8);
assert_eq!(named, Foo::Named(8, "jack".into()));