benhall-7 / diff-struct

Rust trait for diffing and applying diffs between data structures
MIT License
54 stars 14 forks source link

Use Diff With generics #19

Closed AdamJSoftware closed 1 year ago

AdamJSoftware commented 1 year ago

I am trying to use diff with generics but I am encountering issues whenever I try to add Serializing and/or debug. Specifically, that

 the trait `Serialize` is not implemented for `<T as Diff>::Repr

Here is an example, image

How would I get around this?

benhall-7 commented 1 year ago

So here the issue is that when you take the diff of Script, the version field may or not be the same type as T. Instead it will have the type <T as Diff>::Repr, meaning whatever the type of struct the diff is. For example, if T were a HashMap or Vec, the diff of T (<T as Diff>::Repr) would be a totally different struct like HashMapDiff or VecDiff. You have to make sure that the T implements Diff, and that T::Repr implements Debug and Serialize. It would look close to this:

#[derive(Diff)]
struct ScriptSource;

#[derive(Diff)]
#[diff(attr(#[derive(Debug, Serialize)]))]
struct Script<T: Diff>
where
    <T as Diff>::Repr: Debug + Serialize,
{
    functions: Vec<String>,
    source: ScriptSource,
    version: T,
}

Does that clear it up?

AdamJSoftware commented 1 year ago

Yes it does!