Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shl between instance and patch #32

Merged
merged 15 commits into from
Aug 12, 2024
23 changes: 23 additions & 0 deletions struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ impl Patch {
#[cfg(not(feature = "status"))]
let patch_status_impl = quote!();

let add_impl = quote! {
impl core::ops::Add<#name #generics> for #struct_name #generics #where_clause {
type Output = #struct_name;
yanganto marked this conversation as resolved.
Show resolved Hide resolved

fn add(mut self, rhs: #name #generics) -> Self {
self.apply(rhs);
self
}
}

impl core::ops::Add<#struct_name #generics> for #name #generics #where_clause {
type Output = #struct_name;
yanganto marked this conversation as resolved.
Show resolved Hide resolved

fn add(mut self, rhs: #struct_name #generics) -> #struct_name #where_clause {
let mut rhs = rhs;
rhs.apply(self);
rhs
}
}
};

yanganto marked this conversation as resolved.
Show resolved Hide resolved
let patch_impl = quote! {
impl #generics struct_patch::traits::Patch< #name #generics > for #struct_name #generics #where_clause {
fn apply(&mut self, patch: #name #generics) {
Expand Down Expand Up @@ -163,6 +184,8 @@ impl Patch {
#patch_status_impl

#patch_impl

#add_impl
})
}

Expand Down
31 changes: 27 additions & 4 deletions struct-patch/examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use struct_patch::Patch;
#[derive(Default, Patch)]
#[patch(attribute(derive(Debug, Default)))]
struct Item {
field_bool: bool,
field_complete: bool,
field_int: usize,
field_string: String,
}
Expand All @@ -12,7 +12,7 @@ struct Item {
//
// #[derive(Debug, Default)] // pass by patch(attribute(...))
// struct ItemPatch {
// field_bool: Option<bool>,
// field_complete: Option<bool>,
// field_int: Option<usize>,
// field_string: Option<String>,
// }
Expand All @@ -26,12 +26,35 @@ fn main() {

assert_eq!(
format!("{patch:?}"),
"ItemPatch { field_bool: None, field_int: Some(7), field_string: None }"
"ItemPatch { field_complete: None, field_int: Some(7), field_string: None }"
);

item.apply(patch);

assert_eq!(item.field_bool, false);
assert_eq!(item.field_complete, false);
assert_eq!(item.field_int, 7);
assert_eq!(item.field_string, "");

let another_patch = ItemPatch {
field_complete: None,
field_int: None,
field_string: Some("from another patch".into()),
};
let new_item = item + another_patch;

assert_eq!(new_item.field_complete, false);
assert_eq!(new_item.field_int, 7);
assert_eq!(new_item.field_string, "from another patch");

let the_other_patch = ItemPatch {
field_complete: Some(true),
field_int: None,
field_string: None,
};
let final_item = the_other_patch + new_item;
assert_eq!(final_item.field_complete, true);
assert_eq!(final_item.field_int, 7);
assert_eq!(final_item.field_string, "from another patch");

println!("instance example run passed")
}
2 changes: 1 addition & 1 deletion struct-patch/examples/patch-attr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use struct_patch::Patch;
use serde_with::skip_serializing_none;
use struct_patch::Patch;

#[derive(Default, Patch)]
#[patch(attribute(derive(serde::Serialize, Debug, Default)))]
Expand Down
Loading