2016-05-07 14:33:59 -05:00
|
|
|
// These just test that serde_codegen is able to produce code that compiles
|
2016-05-15 17:54:20 -05:00
|
|
|
// successfully when there are a variety of generics and non-(de)serializable
|
|
|
|
// types involved.
|
2016-05-07 14:33:59 -05:00
|
|
|
|
|
|
|
extern crate serde;
|
|
|
|
use self::serde::ser::{Serialize, Serializer};
|
|
|
|
use self::serde::de::{Deserialize, Deserializer};
|
|
|
|
|
2016-07-22 01:22:38 -05:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2016-05-07 14:33:59 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
#[test]
|
|
|
|
fn test_gen() {
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct With<T> {
|
|
|
|
t: T,
|
2016-05-15 17:54:20 -05:00
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
2016-07-22 10:49:51 -05:00
|
|
|
x: X,
|
|
|
|
}
|
|
|
|
assert::<With<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct WithRef<'a, T: 'a> {
|
|
|
|
#[serde(skip_deserializing)]
|
|
|
|
t: Option<&'a T>,
|
2016-05-15 17:54:20 -05:00
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
2016-07-22 10:49:51 -05:00
|
|
|
x: X,
|
|
|
|
}
|
|
|
|
assert::<WithRef<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct PhantomX {
|
|
|
|
x: PhantomData<X>,
|
|
|
|
}
|
|
|
|
assert::<PhantomX>();
|
|
|
|
|
2016-07-22 11:05:36 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct PhantomT<T> {
|
|
|
|
t: PhantomData<T>,
|
|
|
|
}
|
|
|
|
assert::<PhantomT<X>>();
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Bounds<T: Serialize + Deserialize> {
|
2016-05-07 14:33:59 -05:00
|
|
|
t: T,
|
2016-07-22 10:49:51 -05:00
|
|
|
option: Option<T>,
|
|
|
|
boxed: Box<T>,
|
|
|
|
option_boxed: Option<Box<T>>,
|
|
|
|
}
|
|
|
|
assert::<Bounds<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct NoBounds<T> {
|
|
|
|
t: T,
|
|
|
|
option: Option<T>,
|
|
|
|
boxed: Box<T>,
|
|
|
|
option_boxed: Option<Box<T>>,
|
|
|
|
}
|
|
|
|
assert::<NoBounds<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum EnumWith<T> {
|
|
|
|
Unit,
|
|
|
|
Newtype(
|
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
|
|
|
X),
|
|
|
|
Tuple(
|
|
|
|
T,
|
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
|
|
|
X),
|
|
|
|
Struct {
|
|
|
|
t: T,
|
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
|
|
|
x: X },
|
|
|
|
}
|
|
|
|
assert::<EnumWith<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
|
|
|
t: T,
|
|
|
|
rrrt: &'a &'b &'c T,
|
|
|
|
}
|
|
|
|
assert_ser::<MultipleRef<i32>>();
|
2016-05-07 14:33:59 -05:00
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Newtype(
|
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
|
|
|
X
|
|
|
|
);
|
|
|
|
assert::<Newtype>();
|
2016-05-19 01:46:06 -05:00
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Tuple<T>(
|
|
|
|
T,
|
|
|
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
|
|
|
X,
|
|
|
|
);
|
|
|
|
assert::<Tuple<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum TreeNode<D> {
|
|
|
|
Split {
|
|
|
|
left: Box<TreeNode<D>>,
|
|
|
|
right: Box<TreeNode<D>>,
|
|
|
|
},
|
|
|
|
Leaf {
|
|
|
|
data: D,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert::<TreeNode<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct ListNode<D> {
|
2016-05-21 00:03:20 -05:00
|
|
|
data: D,
|
2016-07-22 10:49:51 -05:00
|
|
|
next: Box<ListNode<D>>,
|
|
|
|
}
|
|
|
|
assert::<ListNode<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct RecursiveA {
|
|
|
|
b: Box<RecursiveB>,
|
|
|
|
}
|
|
|
|
assert::<RecursiveA>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum RecursiveB {
|
|
|
|
A(RecursiveA),
|
|
|
|
}
|
|
|
|
assert::<RecursiveB>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct RecursiveGenericA<T> {
|
|
|
|
t: T,
|
|
|
|
b: Box<RecursiveGenericB<T>>,
|
|
|
|
}
|
|
|
|
assert::<RecursiveGenericA<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum RecursiveGenericB<T> {
|
|
|
|
T(T),
|
|
|
|
A(RecursiveGenericA<T>),
|
|
|
|
}
|
|
|
|
assert::<RecursiveGenericB<i32>>();
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct OptionStatic<'a> {
|
|
|
|
a: Option<&'a str>,
|
|
|
|
b: Option<&'static str>,
|
|
|
|
}
|
|
|
|
assert_ser::<OptionStatic>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(bound="D: SerializeWith + DeserializeWith")]
|
|
|
|
struct WithTraits1<D, E> {
|
|
|
|
#[serde(serialize_with="SerializeWith::serialize_with",
|
|
|
|
deserialize_with="DeserializeWith::deserialize_with")]
|
|
|
|
d: D,
|
|
|
|
#[serde(serialize_with="SerializeWith::serialize_with",
|
|
|
|
deserialize_with="DeserializeWith::deserialize_with",
|
|
|
|
bound="E: SerializeWith + DeserializeWith")]
|
|
|
|
e: E,
|
|
|
|
}
|
|
|
|
assert::<WithTraits1<X, X>>();
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(bound(serialize="D: SerializeWith",
|
|
|
|
deserialize="D: DeserializeWith"))]
|
|
|
|
struct WithTraits2<D, E> {
|
|
|
|
#[serde(serialize_with="SerializeWith::serialize_with",
|
|
|
|
deserialize_with="DeserializeWith::deserialize_with")]
|
|
|
|
d: D,
|
|
|
|
#[serde(serialize_with="SerializeWith::serialize_with",
|
|
|
|
bound(serialize="E: SerializeWith"))]
|
|
|
|
#[serde(deserialize_with="DeserializeWith::deserialize_with",
|
|
|
|
bound(deserialize="E: DeserializeWith"))]
|
|
|
|
e: E,
|
|
|
|
}
|
|
|
|
assert::<WithTraits2<X, X>>();
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2016-07-22 00:51:28 -05:00
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
fn assert<T: Serialize + Deserialize>() {}
|
|
|
|
fn assert_ser<T: Serialize>() {}
|
2016-07-22 00:51:28 -05:00
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
trait SerializeWith {
|
|
|
|
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> Result<(), S::Error>;
|
2016-07-22 00:51:28 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
trait DeserializeWith: Sized {
|
|
|
|
fn deserialize_with<D: Deserializer>(_: &mut D) -> Result<Self, D::Error>;
|
2016-07-22 00:51:28 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
// Implements neither Serialize nor Deserialize
|
|
|
|
struct X;
|
2016-07-22 00:51:28 -05:00
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
fn ser_x<S: Serializer>(_: &X, _: &mut S) -> Result<(), S::Error> {
|
|
|
|
unimplemented!()
|
2016-06-04 18:12:01 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
fn de_x<D: Deserializer>(_: &mut D) -> Result<X, D::Error> {
|
|
|
|
unimplemented!()
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
impl SerializeWith for X {
|
|
|
|
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> Result<(), S::Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:49:51 -05:00
|
|
|
impl DeserializeWith for X {
|
|
|
|
fn deserialize_with<D: Deserializer>(_: &mut D) -> Result<Self, D::Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|