3 个不稳定版本
| 新 0.2.1 | 2024年8月20日 |
|---|---|
| 0.2.0 | 2024年8月20日 |
| 0.1.0 | 2022年9月23日 |
#1546 在 Rust 模式
每月下载量 66
5KB
README
这是一个编写 rust derive 宏的模板。
该 trait 为命名结构设计。
- 如果结构的字段有属性
#[exclude],则this.field保持不变。 - 如果结构的字段不是
Option:this.field = that.field.clone() - 如果结构的字段有属性
#[force]:this.field = that.field.clone() - 如果结构的字段是
Option且没有属性#[force]- 如果
that.field.is_some():this.field = that.field.clone() - 如果
that.field.is_none():this.field保持不变。
- 如果
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);
依赖项
~285–740KB
~18K SLoC