Create custom serializable changes to apply later
Deltastruct is an attribute macro that generates definitions for
serializable changes (called deltas) over structs recursively.
For techinical reasons, the attribute macro must be applied to a mod
definition containing a struct
with the same name and an impl
for
it's change definitions. Function names are converted to camel case to
appease the compiler.
The generated deltas are always the type name followed by Delta
. ie: Foo
-> FooDelta
, i32
-> i32Delta
.
# use deltastruct::*;
#[deltastruct]
mod Vector3 {
#[derive(Clone, Copy)]
pub struct Vector3 {
pub x : f64,
pub y : f64,
pub z : f64,
}
impl Vector3 {
fn set(&mut self, other : Vector3) {
self.x = other.x;
self.y = other.y;
self.z = other.z;
}
fn multiply_scalar(&mut self, n : f64) {
self.x *= n;
self.y *= n;
self.z *= n;
}
fn normalize(&mut self) {
let magnitude = (
self.x.powf(2f64)
+ self.y.powf(2f64)
+ self.z.powf(2f64)
).sqrt();
self.x /= magnitude;
self.y /= magnitude;
self.z /= magnitude;
}
}
}
fn main() {
let mut v = Vector3 {
x: 3f64,
y: 6f64,
z: 6f64
};
let v_delta = Vector3Delta::MultiplyScalar(4f64);
v.apply(&v_delta);
v.apply(&v_delta);
assert_eq!(v.x, 48f64);
let v_delta = Vector3Delta::Normalize();
v.apply(&v_delta);
assert_eq!(v.z, 2f64/3f64);
let v_delta = Vector3Delta::X(f64Delta::Set(8f64));
v.apply(&v_delta);
assert_eq!(v.x, 8f64);
}