Derive Macro struct_patch::Patch
source · #[derive(Patch)]
{
// Attributes available to this derive:
#[patch_derive]
}
Expand description
Patch
help you patch Rust instance, and easy to partial update.
#[derive(Patch)]
struct Item {
field_bool: bool,
field_int: usize,
field_string: String,
}
Patch derive will generate ItemPatch and implement Patch trait for struct.
#[derive(Default)]
struct ItemPatch {
field_bool: Option<bool>,
field_int: Option<usize>,
field_string: Option<String>,
}
Such that you can use apply
function to patch the existing fields from ItemPatch
to Item
use struct_patch::traits::Patch;
let mut item = Item::default();
let mut patch = Item::default_patch();
patch.field_int = Some(7);
item.apply(patch); // only `field_int` updated
If you want to add more derives on patch struct, you can use patch_derive
as following.
#[derive(Patch)]
#[patch_derive(Debug, Default, Deserialize, Serialize)]
struct Item { }
Patch derive will generate ItemPatch and implement Patch trait for struct.
#[derive(Debug, Default, Deserialize, Serialize)]
struct ItemPatch {}
Such that the patch struct can easily generated from json or other serializer. Please check the example.