浏览代码

More diff work

Kevin Lee 1 年之前
父节点
当前提交
d03e6117d2
共有 5 个文件被更改,包括 136 次插入55 次删除
  1. 73 30
      src/ezcad/array_of.rs
  2. 27 16
      src/ezcad/field_of.rs
  3. 1 1
      src/ezcad/objects/hatch.rs
  4. 1 1
      src/ezcad/objects/mod.rs
  5. 34 7
      src/ezcad/types.rs

+ 73 - 30
src/ezcad/array_of.rs

@@ -1,5 +1,5 @@
 use binrw::{binrw, BinRead, BinWrite};
-use diff::Diff;
+use diff::{Diff, VecDiff};
 use num::Num;
 use std::{
     fmt::{Debug, Display},
@@ -9,18 +9,14 @@ use std::{
 
 /// Wrapper to prefix array of primitives (u8/u32/etc)
 #[binrw]
-#[derive(Clone, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
+#[derive(Clone, Default, PartialEq)]
 pub struct ArrayOfPrimitive<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + Num + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: Num + 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -38,10 +34,9 @@ where
 impl<T, S> Debug for ArrayOfPrimitive<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + Num + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: Debug + Num + 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -50,20 +45,44 @@ where
     }
 }
 
+// Custom Diff implementation to only diff internal value
+impl<T, S> Diff for ArrayOfPrimitive<T, S>
+where
+    T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    T: Diff + PartialEq + Num + 'static,
+    S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    S: TryFrom<usize> + Copy + Clone + Debug + Display + 'static,
+    <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
+    usize: TryFrom<S>,
+{
+    type Repr = VecDiff<T>;
+
+    fn diff(&self, other: &Self) -> Self::Repr {
+        self.value.diff(&other.value)
+    }
+
+    fn apply(&mut self, diff: &Self::Repr) {
+        self.value.apply(diff)
+    }
+
+    fn identity() -> Self {
+        Self {
+            value: vec![],
+            _marker: PhantomData,
+        }
+    }
+}
+
 /// Wrapper to prefix array of single non-primitive type
 #[binrw]
-#[derive(Clone, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
+#[derive(Clone, PartialEq)]
 pub struct ArrayOf<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -78,13 +97,40 @@ where
     _marker: PhantomData<S>,
 }
 
+// Custom Diff implementation to only diff internal value
+impl<T, S> Diff for ArrayOf<T, S>
+where
+    T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    T: Diff + PartialEq + 'static,
+    S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    S: TryFrom<usize> + Copy + Clone + Debug + Display + 'static,
+    <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
+    usize: TryFrom<S>,
+{
+    type Repr = VecDiff<T>;
+
+    fn diff(&self, other: &Self) -> Self::Repr {
+        self.value.diff(&other.value)
+    }
+
+    fn apply(&mut self, diff: &Self::Repr) {
+        self.value.apply(diff)
+    }
+
+    fn identity() -> Self {
+        Self {
+            value: vec![],
+            _marker: PhantomData,
+        }
+    }
+}
+
 impl<T, S> Debug for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: Debug + 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + Display + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -96,10 +142,9 @@ where
 impl<T, S> Deref for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -113,10 +158,9 @@ where
 impl<T, S> DerefMut for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {
@@ -128,10 +172,9 @@ where
 impl<T, S> From<Vec<T>> for ArrayOf<T, S>
 where
     T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    T: Debug + Diff + PartialEq + 'static,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: 'static,
     S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
-    S: TryFrom<usize> + Copy + Clone + Debug + Display + Diff + PartialEq + 'static,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
     <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
     usize: TryFrom<S>,
 {

+ 27 - 16
src/ezcad/field_of.rs

@@ -8,16 +8,11 @@ use std::{
 
 /// Wrapper to prefix arbitrary types with size of type as u32
 #[binrw]
-#[derive(Clone, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
+#[derive(Clone, Default, PartialEq)]
 pub struct FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff,
-    <T as Diff>::Repr: Debug + PartialEq,
 {
     // Temporary variable that must match size of value
     #[br(temp, assert(size == size_of::<T>().try_into().unwrap()))]
@@ -27,12 +22,34 @@ where
     pub value: T,
 }
 
+// Custom Diff implementation to only diff internal value
+impl<T> Diff for FieldOf<T>
+where
+    T: for<'a> BinRead<Args<'a> = ()>,
+    T: for<'a> BinWrite<Args<'a> = ()>,
+    T: Diff + Default,
+{
+    type Repr = <T as Diff>::Repr;
+
+    fn diff(&self, other: &Self) -> Self::Repr {
+        self.value.diff(&other.value)
+    }
+
+    fn apply(&mut self, diff: &Self::Repr) {
+        self.value.apply(diff)
+    }
+
+    fn identity() -> Self {
+        Self {
+            value: T::default(),
+        }
+    }
+}
+
 impl<T> Deref for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff,
-    <T as Diff>::Repr: Debug + PartialEq,
 {
     type Target = T;
 
@@ -45,8 +62,6 @@ impl<T> DerefMut for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff,
-    <T as Diff>::Repr: Debug + PartialEq,
 {
     fn deref_mut(&mut self) -> &mut Self::Target {
         &mut self.value
@@ -57,8 +72,7 @@ impl<T> Debug for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff + Debug,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: Debug,
 {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "{:?}", self.value)
@@ -69,8 +83,7 @@ impl<T> Display for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff + Display,
-    <T as Diff>::Repr: Debug + PartialEq,
+    T: Display,
 {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "{}", self.value)
@@ -81,8 +94,6 @@ impl<T> From<T> for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,
-    T: Diff,
-    <T as Diff>::Repr: Debug + PartialEq,
 {
     fn from(value: T) -> Self {
         Self { value }

+ 1 - 1
src/ezcad/objects/hatch.rs

@@ -16,7 +16,7 @@ use crate::{
 use super::{line::Lines, Object, ObjectCore};
 
 #[bitfield(bits = 32)]
-#[derive(BinRead, BinWrite, Copy, Clone, Debug, Diff, PartialEq)]
+#[derive(BinRead, BinWrite, Copy, Clone, Debug, Default, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
 ))]

+ 1 - 1
src/ezcad/objects/mod.rs

@@ -25,7 +25,7 @@ pub mod polygon;
 pub mod rectangle;
 
 #[bitfield(bits = 16)]
-#[derive(BinRead, BinWrite, Copy, Clone, Debug, Diff, PartialEq)]
+#[derive(BinRead, BinWrite, Copy, Clone, Debug, Default, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
 ))]

+ 34 - 7
src/ezcad/types.rs

@@ -1,7 +1,7 @@
 use std::fmt::{Debug, Display};
 
 use binrw::{binrw, BinRead, BinWrite};
-use diff::Diff;
+use diff::{Diff, VecDiff};
 
 use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
 
@@ -13,10 +13,7 @@ pub type U16 = FieldOf<u16>;
 pub type F64 = FieldOf<f64>;
 
 #[binrw]
-#[derive(Clone, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
+#[derive(Clone, Default, PartialEq)]
 pub struct WString {
     // Temporary variable that holds number of elements
     #[br(temp)]
@@ -40,6 +37,23 @@ impl From<String> for WString {
     }
 }
 
+// Custom Diff implementation to only diff internal value
+impl Diff for WString {
+    type Repr = VecDiff<u16>;
+
+    fn diff(&self, other: &Self) -> Self::Repr {
+        self.value.diff(&other.value)
+    }
+
+    fn apply(&mut self, diff: &Self::Repr) {
+        self.value.apply(diff)
+    }
+
+    fn identity() -> Self {
+        Self { value: vec![] }
+    }
+}
+
 impl Debug for WString {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "\"{}\"", String::from(self))
@@ -52,7 +66,7 @@ impl Display for WString {
     }
 }
 
-#[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite)]
+#[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
 ))]
@@ -74,7 +88,7 @@ impl From<(u8, u8, u8)> for Rgba {
     }
 }
 
-#[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite)]
+#[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
 ))]
@@ -95,6 +109,13 @@ pub enum WobbleType {
     Ellipse = 2,
     Vert8 = 3,
     Hori8 = 4,
+    Unknown = 5,
+}
+
+impl Default for WobbleType {
+    fn default() -> Self {
+        WobbleType::Unknown
+    }
 }
 
 #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
@@ -104,6 +125,7 @@ pub enum WobbleType {
 #[brw(repr(u16))]
 #[repr(u16)]
 pub enum ObjectType {
+    Unknown = 0,
     Curve = 1,
     Point = 2,
     Rectangle = 3,
@@ -112,3 +134,8 @@ pub enum ObjectType {
     Polygon = 6,
     Hatch = 32,
 }
+impl Default for ObjectType {
+    fn default() -> Self {
+        ObjectType::Unknown
+    }
+}