Trait struct_patch::traits::Patch
source · pub trait Patch<P> {
// Required methods
fn apply(&mut self, patch: P);
fn into_patch(self) -> P;
fn into_patch_by_diff(self, previous_struct: Self) -> P;
fn new_empty_patch() -> P;
}
Expand description
A struct that a patch can be applied to
Deriving Patch
will generate a patch struct and an accompanying trait impl so that it can be applied to the original struct.
#[derive(Patch)]
struct Item {
field_bool: bool,
field_int: usize,
field_string: String,
}
// Generated struct
// struct ItemPatch {
// field_bool: Option<bool>,
// field_int: Option<usize>,
// field_string: Option<String>,
// }
Container attributes
#[patch_derive(...)]
Use this attribute to derive traits on the generated patch struct
#[derive(Patch)]
#[patch_derive(Debug, Default, Deserialize, Serialize)]
struct Item;
// Generated struct
// #[derive(Debug, Default, Deserialize, Serialize)]
// struct ItemPatch {}
#[patch_name = "..."]
Use this attribute to change the name of the generated patch struct
#[derive(Patch)]
#[patch_name = "ItemOverlay"]
struct Item { }
// Generated struct
// struct ItemOverlay {}
Field attributes
#[patch_skip]
If you want certain fields to be unpatchable, you can let the derive macro skip certain fields when creating the patch struct
#[derive(Patch)]
struct Item {
#[patch_skip]
id: String,
data: String,
}
// Generated struct
// struct ItemPatch {
// data: Option<String>,
// }
Required Methods§
sourcefn into_patch(self) -> P
fn into_patch(self) -> P
Returns a patch that when applied turns any struct of the same type into Self
sourcefn into_patch_by_diff(self, previous_struct: Self) -> P
fn into_patch_by_diff(self, previous_struct: Self) -> P
Returns a patch that when applied turns previous_struct
into Self
sourcefn new_empty_patch() -> P
fn new_empty_patch() -> P
Get an empty patch instance