structible
A proc-macro for generating map-backed structs with type-safe accessors.
use structible;
let mut person = new;
assert_eq!;
assert_eq!;
person.set_email;
assert_eq!;
*person.age_mut += 1;
assert_eq!;
Quick Reference
Struct Attributes
| Attribute | Example | Description |
|---|---|---|
backing |
#[structible(backing = BTreeMap)] |
Map type (default: HashMap) |
constructor |
#[structible(constructor = create)] |
Constructor name (default: new) |
Field Attributes
| Attribute | Example | Description |
|---|---|---|
get |
#[structible(get = full_name)] |
Custom getter name |
get_mut |
#[structible(get_mut = name_ref)] |
Custom mutable getter name |
set |
#[structible(set = rename)] |
Custom setter name |
remove |
#[structible(remove = clear)] |
Custom remover name (optional fields) |
key |
#[structible(key = String)] |
Unknown/extension fields catch-all |
Generated Methods
For each field, the macro generates:
| Field Type | Method | Signature |
|---|---|---|
| Required | Getter | fn name(&self) -> &T |
| Required | Mutable getter | fn name_mut(&mut self) -> &mut T |
| Required | Setter | fn set_name(&mut self, value: T) |
| Optional | Getter | fn name(&self) -> Option<&T> |
| Optional | Mutable getter | fn name_mut(&mut self) -> Option<&mut T> |
| Optional | Setter | fn set_name(&mut self, value: Option<T>) |
| Optional | Remover | fn remove_name(&mut self) -> Option<T> |
The constructor accepts all required fields: fn new(name: String, age: u32) -> Self
BTreeMap Backing
Use BTreeMap for ordered iteration:
Unknown/Extension Fields
Catch-all for dynamic fields beyond the statically-known ones:
let mut person = new;
person.add_extra;
assert_eq!;
for in person.extra_iter
Generated methods: add_{field}, {field}, {field}_mut, remove_{field}, {field}_iter
Ownership Extraction
Extract owned values with into_fields() or individual take_* methods:
let person = new;
let PersonFields = person.into_fields;
// Or extract individual fields:
let mut person = new;
let name = person.take_name; // Consumes the field
Custom BackingMap
Implement BackingMap<K, V> for custom map types:
For unknown fields support, also implement IterableMap<K, V>.
Automatic Derives
Generated structs derive: Debug, Clone, PartialEq
Default is only implemented when all fields are optional.
Limitations
- Named struct fields only (no tuple/unit structs)
- At most one unknown/extension field per struct
- Field types must implement
CloneandPartialEq