Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SerdeDiff for a few infallible container types #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
impl SerdeDiff for Cow
AaronFriel committed Jul 5, 2021

Verified

This commit was signed with the committer’s verified signature.
yeshan333 yeshan333
commit 49489ab14a42cc31ebf2a4b3c968a14d3e1564a9
27 changes: 27 additions & 0 deletions src/implementation.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use crate::{
use serde::{de, ser::SerializeSeq, Deserialize, Serialize};

use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
hash::Hash,
};
@@ -409,3 +410,29 @@ impl<T: SerdeDiff + Serialize + for<'a> Deserialize<'a>> SerdeDiff for Option<T>

type Unit = ();
opaque_serde_diff!(Unit);

impl<'xyz, B: ?Sized + 'xyz> SerdeDiff for Cow<'xyz, B>
where
B: Clone,
B: SerdeDiff,
<B as ToOwned>::Owned: SerdeDiff,
{
fn diff<'a, S: serde::ser::SerializeSeq>(
&self,
ctx: &mut DiffContext<'a, S>,
other: &Self,
) -> Result<bool, S::Error> {
(&self as &B).diff(ctx, other)
}

fn apply<'de, A>(
&mut self,
seq: &mut A,
ctx: &mut ApplyContext,
) -> Result<bool, <A as serde::de::SeqAccess<'de>>::Error>
where
A: serde::de::SeqAccess<'de>,
{
self.to_mut().apply(seq, ctx)
}
}
48 changes: 46 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate as serde_diff;
use crate::{Apply, Diff, SerdeDiff};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::Debug;

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
@@ -135,7 +136,7 @@ fn test_tuple() {
);
}

#[derive(SerdeDiff, Serialize, Deserialize, Clone, PartialEq, Debug)]
#[derive(SerdeDiff, Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
#[serde(from = "MySimpleStruct", into = "MySimpleStruct")]
#[serde_diff(target = "MySimpleStruct")]
struct MyComplexStruct {
@@ -145,12 +146,17 @@ struct MyComplexStruct {
b: u32,
}

#[derive(SerdeDiff, Serialize, Deserialize, Default, PartialEq, Debug)]
#[derive(SerdeDiff, Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
#[serde(rename = "MyComplexStruct", default)]
struct MySimpleStruct {
a: u32,
}

#[derive(SerdeDiff, Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
struct MyCowStruct<'x> {
a: Cow<'x, MySimpleStruct>,
}

impl From<MySimpleStruct> for MyComplexStruct {
fn from(my_simple_struct: MySimpleStruct) -> Self {
MyComplexStruct {
@@ -200,3 +206,41 @@ fn test_targeted() {
Some(MyComplexStruct { a: 2, b: 0 }),
);
}

#[test]
fn test_cow() {
roundtrip(
MyCowStruct {
a: Cow::Owned(MySimpleStruct { a: 0 }),
},
MyCowStruct {
a: Cow::Owned(MySimpleStruct { a: 10 }),
},
);
let a = MySimpleStruct { a: 0 };
let b = MySimpleStruct { a: 1 };
roundtrip(
MyCowStruct {
a: Cow::Borrowed(&a),
},
MyCowStruct {
a: Cow::Owned(MySimpleStruct { a: 10 }),
},
);
roundtrip(
MyCowStruct {
a: Cow::Owned(MySimpleStruct { a: 0 }),
},
MyCowStruct {
a: Cow::Borrowed(&b),
},
);
roundtrip(
MyCowStruct {
a: Cow::Borrowed(&a),
},
MyCowStruct {
a: Cow::Borrowed(&b),
},
);
}