Skip to content

Commit

Permalink
Use Shl as default feature and remove add
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 12, 2024
1 parent 905615f commit 58a4816
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 98 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,10 @@ jobs:
nix develop -c cargo run --no-default-features --example json
nix develop -c cargo run --no-default-features --example rename-patch-struct
nix develop -c cargo run --no-default-features --example patch-attr
nix develop -c cargo run --no-default-features --example op
nix develop -c cargo test --no-default-features
- name: Test with default features
run: |
nix develop -c cargo run --example status
nix develop -c cargo test
- name: Test add feature
run: |
nix develop -c cargo run --features=add --example instance
nix develop -c cargo run --features=add --example diff
nix develop -c cargo run --features=add --example json
nix develop -c cargo run --features=add --example rename-patch-struct
nix develop -c cargo run --features=add --example patch-attr
nix develop -c cargo test --features=add
72 changes: 47 additions & 25 deletions struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,71 @@ impl Patch {
#[cfg(not(feature = "status"))]
let patch_status_impl = quote!();

#[cfg(feature = "add")]
let add_impl = quote! {
impl #generics core::ops::Add<#name #generics> for #struct_name #generics #where_clause {
let op_impl = quote! {
impl #generics core::ops::Shl<#name #generics> for #struct_name #generics #where_clause {
type Output = Self;

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

impl #generics core::ops::Add<#struct_name #generics> for #name #generics #where_clause {
type Output = #struct_name #generics;

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

impl #generics core::ops::Add<Self> for #name #generics #where_clause {
impl #generics core::ops::Shl<#name #generics> for #name #generics #where_clause {
type Output = Self;

fn add(mut self, rhs: Self) -> Self {
fn shl(mut self, rhs: #name #generics) -> Self {
Self {
#(
#renamed_field_names: match (self.#renamed_field_names, rhs.#renamed_field_names) {
(Some(a), Some(b)) => Some(a + b),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
},
#renamed_field_names: rhs.#renamed_field_names.or(self.#renamed_field_names),
)*
#(
#original_field_names: rhs.#original_field_names.or(self.#original_field_names),
)*
}
}
}

// TODO
// We need unstable feature to make sure the type of field for add feature
// https://doc.rust-lang.org/std/any/fn.type_name_of_val.html
// impl #generics core::ops::Add<Self> for #name #generics #where_clause {
// type Output = Self;

// fn add(mut self, rhs: Self) -> Self {
// Self {
// #(
// #renamed_field_names: match (self.#renamed_field_names, rhs.#renamed_field_names) {
// (Some(a), Some(b)) => {
// if std::any::type_name_of_val(&a) == "usize" {
// Some(a + b)
// } else {
// Some(b)
// }
// },
// (Some(a), None) => Some(a),
// (None, Some(b)) => Some(b),
// (None, None) => None,
// },
// )*
// #(
// #original_field_names: match (self.#original_field_names, rhs.#original_field_names) {
// (Some(a), Some(b)) => {
// if std::any::type_name_of_val(&a) == "usize" {
// Some(a + b)
// } else {
// Some(b)
// }
// },
// (Some(a), None) => Some(a),
// (None, Some(b)) => Some(b),
// (None, None) => None,
// },
// )*
// }
// }
// }
};
#[cfg(not(feature = "add"))]
let add_impl = quote!();

let patch_impl = quote! {
impl #generics struct_patch::traits::Patch< #name #generics > for #struct_name #generics #where_clause {
Expand Down Expand Up @@ -208,7 +230,7 @@ impl Patch {

#patch_impl

#add_impl
#op_impl
})
}

Expand Down
37 changes: 0 additions & 37 deletions struct-patch/examples/add.rs

This file was deleted.

43 changes: 20 additions & 23 deletions struct-patch/examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,24 @@ fn main() {
assert_eq!(item.field_int, 7);
assert_eq!(item.field_string, "");

#[cfg(feature = "add")]
{
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");
}
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 = new_item << the_other_patch;
assert_eq!(final_item.field_complete, true);
assert_eq!(final_item.field_int, 7);
assert_eq!(final_item.field_string, "from another patch");
}
6 changes: 2 additions & 4 deletions struct-patch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ mod tests {
);
}

#[cfg(feature = "add")]
#[test]
fn test_add() {
fn test_shl() {
#[derive(Patch, Debug, PartialEq)]
struct Item {
field: u32,
Expand All @@ -300,15 +299,14 @@ mod tests {
};

assert_eq!(
item + patch,
item << patch,
Item {
field: 1,
other: String::from("bye")
}
);
}

#[cfg(feature = "add")]
#[test]
fn test_add_combined() {
#[derive(Patch, Debug, PartialEq)]
Expand Down

0 comments on commit 58a4816

Please sign in to comment.