-
Notifications
You must be signed in to change notification settings - Fork 5
How can I prevent an Option field from being overwritten? #81
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Hi @Jackhr-arch, You may misunderstood here. #[derive(struct_patch::Patch, Debug, PartialEq)]
struct Foo {
maybe_name: Option<String>,
}
let mut foo = Foo { maybe_name: None };
let mut patch = Foo::new_empty_patch();
patch.maybe_name = Some(Some("Should be kept".to_owned()));
foo.apply(patch);
assert_eq!(foo, Foo {
maybe_name: Some("Should be kept".to_owned())
});
let mut patch = Foo::new_empty_patch();
// Current design does not fit your using case.
// In your example the field is some, so we need to patch the content of the field.
// patch.maybe_name = Some(Some("Being overwritten".to_owned()));
// If the field `None`, there is nothing to patch
patch.maybe_name = None;
foo.apply(patch);
assert_eq!(foo, Foo {
maybe_name: Some("Should be kept".to_owned())
}); The thing you want is only patch |
No more in my case, but I think In fact, I encounter some other problems. #[derive(struct_patch::Patch, Debug, PartialEq)]
struct Foo {
vec: Vec<u8>,
}
let mut foo = Foo { vec: vec![1, 2] };
let mut patch = Foo::new_empty_patch();
patch.vec = Some(vec![3, 4]);
foo.apply(patch);
// Not append
assert_eq!(foo, Foo { vec: vec![3, 4] }) Currently I solve this by using In fact, if I use
while the other fields keep being overwritten with These makes things easier, what you think? |
Hmm, it is good to keep patch object the same, because the field of instance will be patched based on the field of And we can provide another struct for your using case, I want to call it #[derive(struct_patch::Filler, Debug, PartialEq)]
struct Foo {
maybe_name: Option<String>,
}
let mut foo = Foo { maybe_name: None };
let mut filler = Foo::new_empty_filler();
filler.maybe_name = Some("Should be kept".to_owned());
foo.apply(filler);
assert_eq!(foo, Foo {
maybe_name: Some("Should be kept".to_owned())
});
let mut filler = Foo::new_empty_filler();
// The filler and not apply if the instance is filled
filler.maybe_name = Some("Being overwritten".to_owned());
foo.apply(filler);
assert_eq!(foo, Foo {
maybe_name: Some("Should be kept".to_owned())
}); The using case for #[derive(struct_patch::Filler, Debug, PartialEq)]
struct Foo {
#[filler(full=4)]
vec: Vec<u8>,
}
let mut foo = Foo { vec: vec![1, 2] };
let mut filler = Foo::new_empty_filler();
filler.vec = vec![3, 4];
foo.apply(filler);
assert_eq!(foo, Foo { vec: vec![1, 2, 3, 4] })
let mut filler = Foo::new_empty_filler();
filler.vec = vec![5, 6];
foo.apply(filler);
// It is full, so the filler apply without effects.
assert_eq!(foo, Foo { vec: vec![1, 2, 3, 4] }) Following is the example to define #[derive(struct_patch::Filler, Debug, PartialEq, Default)]
struct Color {
R: u8,
G: u8,
B: u8,
#[filler(empty=0)]
// or #[filler(default_is_empty)]
alpha: u8,
}
let mut color = Color::default();
let mut filler = Coler::new_empty_filler();
// Note: there is no R, G, or B in ColorFiler, because R, G, B are not `Option` or `Vec` field
// ColorFiler {
// alpha: Option<u8>,
// }
filler.alpha = Some(50);
color.apply(filler);
assert_eq!(color, Color { R: 0, G: 0, B: 0, alpha: 50 }) If you think this is a good idea, we can work on this and release the |
Yeah I think that's a good idea. I suggest a
And the behavior of applying I've also got some ideas about this.
Maybe you can just work on current trait |
We are clear with the basic In my view, the The concept of We can discuss each topic in issues, and it will be easier to move on. Thanks. |
Here is the example:
I hope there is something like
The text was updated successfully, but these errors were encountered: