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,
and use is_empty to check the patch instance has something to patch or not.
use struct_patch::traits::Patch;
let mut item = Item::default();
let mut patch = Item::default_patch();
assert(patch.is_empty());
patch.field_int = Some(7);
assert!(!patch.is_empty());
item.apply(patch); // only `field_int` updatedIf 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.