|
@@ -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>,
|
|
|
{
|