// These just test that serde_codegen is able to produce code that compiles // successfully when there are a variety of generics and non-(de)serializable // types involved. extern crate serde; use self::serde::ser::{Serialize, Serializer}; use self::serde::de::{Deserialize, Deserializer}; use std::marker::PhantomData; ////////////////////////////////////////////////////////////////////////// #[test] fn test_gen() { #[derive(Serialize, Deserialize)] struct With { t: T, #[serde(serialize_with="ser_x", deserialize_with="de_x")] x: X, } assert::>(); #[derive(Serialize, Deserialize)] struct WithRef<'a, T: 'a> { #[serde(skip_deserializing)] t: Option<&'a T>, #[serde(serialize_with="ser_x", deserialize_with="de_x")] x: X, } assert::>(); #[derive(Serialize, Deserialize)] struct PhantomX { x: PhantomData, } assert::(); #[derive(Serialize, Deserialize)] struct PhantomT { t: PhantomData, } assert::>(); #[derive(Serialize, Deserialize)] struct Bounds { t: T, option: Option, boxed: Box, option_boxed: Option>, } assert::>(); #[derive(Serialize, Deserialize)] struct NoBounds { t: T, option: Option, boxed: Box, option_boxed: Option>, } assert::>(); #[derive(Serialize, Deserialize)] enum EnumWith { 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::>(); #[derive(Serialize)] struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a { t: T, rrrt: &'a &'b &'c T, } assert_ser::>(); #[derive(Serialize, Deserialize)] struct Newtype( #[serde(serialize_with="ser_x", deserialize_with="de_x")] X ); assert::(); #[derive(Serialize, Deserialize)] struct Tuple( T, #[serde(serialize_with="ser_x", deserialize_with="de_x")] X, ); assert::>(); #[derive(Serialize, Deserialize)] enum TreeNode { Split { left: Box>, right: Box>, }, Leaf { data: D, }, } assert::>(); #[derive(Serialize, Deserialize)] struct ListNode { data: D, next: Box>, } assert::>(); #[derive(Serialize, Deserialize)] struct RecursiveA { b: Box, } assert::(); #[derive(Serialize, Deserialize)] enum RecursiveB { A(RecursiveA), } assert::(); #[derive(Serialize, Deserialize)] struct RecursiveGenericA { t: T, b: Box>, } assert::>(); #[derive(Serialize, Deserialize)] enum RecursiveGenericB { T(T), A(RecursiveGenericA), } assert::>(); #[derive(Serialize)] struct OptionStatic<'a> { a: Option<&'a str>, b: Option<&'static str>, } assert_ser::(); #[derive(Serialize, Deserialize)] #[serde(bound="D: SerializeWith + DeserializeWith")] struct WithTraits1 { #[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::>(); #[derive(Serialize, Deserialize)] #[serde(bound(serialize="D: SerializeWith", deserialize="D: DeserializeWith"))] struct WithTraits2 { #[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::>(); } ////////////////////////////////////////////////////////////////////////// fn assert() {} fn assert_ser() {} trait SerializeWith { fn serialize_with(_: &Self, _: &mut S) -> Result<(), S::Error>; } trait DeserializeWith: Sized { fn deserialize_with(_: &mut D) -> Result; } // Implements neither Serialize nor Deserialize struct X; fn ser_x(_: &X, _: &mut S) -> Result<(), S::Error> { unimplemented!() } fn de_x(_: &mut D) -> Result { unimplemented!() } impl SerializeWith for X { fn serialize_with(_: &Self, _: &mut S) -> Result<(), S::Error> { unimplemented!() } } impl DeserializeWith for X { fn deserialize_with(_: &mut D) -> Result { unimplemented!() } }