Crates.io | derive-merge-struct |
lib.rs | derive-merge-struct |
version | 0.2.1 |
source | src |
created_at | 2022-09-23 10:54:55.205571 |
updated_at | 2024-08-20 07:20:10.693587 |
description | A derive macro to partially update a named struct |
homepage | |
repository | https://github.com/reticenceji/merge_struct |
max_upload_size | |
id | 672395 |
size | 4,430 |
It's a template to write rust derive macro.
The trait is designed for named struct.
#[exclude]
, this.field
remain the same.Option
: this.field = that.field.clone()
#[force]
: this.field = that.field.clone()
Option
and doesn't have attribute #[force]
:
If that.field.is_some()
: this.field = that.field.clone()
If that.field.is_none()
: this.field
remain the same.
pub trait MergeProto {
fn merge_proto(&mut self, another: &Self);
}
#[derive(MergeProto)]
struct TestStruct {
a: Option<i32>,
b: Option<String>,
c: Option<u32>,
#[force]
d: Option<i32>,
#[exclude]
e: Option<i32>
}
let mut this = TestStruct {
a: Some(1),
b: None,
c: Some(1),
d: Some(1),
e: None
};
let that = TestStruct {
a: Some(2),
b: Some("hello".to_string()),
c: None,
d: None,
e: Some(1)
};
this.merge_proto(&that);
assert_eq!(this.a, Some(2));
assert_eq!(this.b, Some("hello".to_string()));
assert_eq!(this.c, Some(1));
assert_eq!(this.d, None);
assert_eq!(this.e, None);