5 releases

0.2.3 Mar 12, 2025
0.2.2 Mar 12, 2025
0.2.1 Aug 20, 2024
0.2.0 Aug 20, 2024
0.1.0 Sep 23, 2022

#635 in Rust patterns

Download history 493/week @ 2025-05-17 524/week @ 2025-05-24 282/week @ 2025-05-31 1032/week @ 2025-06-07 472/week @ 2025-06-14 51/week @ 2025-06-21 411/week @ 2025-06-28 486/week @ 2025-07-05 868/week @ 2025-07-12 849/week @ 2025-07-19 571/week @ 2025-07-26 194/week @ 2025-08-02 156/week @ 2025-08-09 594/week @ 2025-08-16 227/week @ 2025-08-23 73/week @ 2025-08-30

1,050 downloads per month

MIT license

6KB
51 lines

README

The trait is designed for named struct.

  • If the field of struct has attribute #[exclude], this.field remain the same.
  • ElIf the field of struct is not Option: this.field = that.field.clone()
  • ElIf the field of struct has attribute #[force]: this.field = that.field.clone()
  • ElIf the field of struct is 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);

Dependencies

~185–610KB
~14K SLoC