Set fields on structs by string.
rust
pub trait SetField<T> {
fn set_field(&mut self, field: &str, value: T) -> bool;
}
```rust use set_field::SetField;
struct Foo {
a: i32,
b: Option
The SetField macro expands Foo into this:
rust
struct Foo {
a: i32,
b: Option<bool>,
c: i32,
}
impl SetField<i32> for Foo {
fn set_field(&mut self, field: &str, value: i32) -> bool {
match field {
"a" => {
self.a = value;
true
}
"c" => {
self.c = value;
true
}
_ => false,
}
}
}
impl SetField<Option<bool>> for Foo {
fn set_field(&mut self, field: &str, value: Option<bool>) -> bool {
match field {
"b" => {
self.b = value;
true
}
_ => false,
}
}
}