Implementing Patchable
Here's a look at the Patchable
trait.
#![allow(unused_variables)] fn main() { pub trait Patchable<P> { fn apply_patch(&mut self, patch: P); }
P
is the delta encoding that was generated by the Diffable.create_delta_towards
method.
Different types will have different patches that need to be applied in different ways.
Let's talk a look at an i128
, one of the more trivial Patchable
types.
#![allow(unused_variables)] fn main() { implement Patchable<Option<i128>> for i128 { fn apply_patch(&mut self, patch: Option<i128>) { if let Some(patch) = patch { *self = patch; } } } }
Here's a slightly more complex example patch function.
#![allow(unused_variables)] fn main() { enum CustomPatchExample { Replace(i128), Add(i16) } // This is not how the real implementation for i128 looks. implement Patchable<CustomPatchExample> for i128 { fn apply_patch(&mut self, patch: CustomPatchExample) { match patch { CustomPatchExample::Replace(new) => { *self = new; } CustomPatchExample::Add(add) => { self += add as i128; } } } } }