From c3b9a26bb080fae9e61bc14ee2b899239d40210f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 16 Aug 2016 02:54:11 -0400 Subject: [PATCH 1/2] Support deserializing from bincode Bincode is a minimal format that expects the Deserialize implementation to tell it what type of data it should expect to see. --- src/datetime.rs | 6 +++--- src/naive/date.rs | 2 +- src/naive/datetime.rs | 2 +- src/naive/time.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index a0326cc..a0bf400 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -496,7 +496,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(DateTimeVisitor) + deserializer.deserialize_str(DateTimeVisitor) } } @@ -504,7 +504,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC)) + deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC)) } } @@ -512,7 +512,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(DateTimeVisitor).map(|dt| dt.with_timezone(&Local)) + deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local)) } } diff --git a/src/naive/date.rs b/src/naive/date.rs index 61bff19..770279a 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1581,7 +1581,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(NaiveDateVisitor) + deserializer.deserialize_str(NaiveDateVisitor) } } diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 1249611..e583d8e 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1143,7 +1143,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(NaiveDateTimeVisitor) + deserializer.deserialize_str(NaiveDateTimeVisitor) } } diff --git a/src/naive/time.rs b/src/naive/time.rs index 4c26533..12d12b0 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1268,7 +1268,7 @@ mod serde { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { - deserializer.deserialize(NaiveTimeVisitor) + deserializer.deserialize_str(NaiveTimeVisitor) } } From c3cd76af16a2c5c1aae87026fb98959a4d9b9f63 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 16 Aug 2016 18:08:08 -0400 Subject: [PATCH 2/2] Add bincode serde tests --- Cargo.toml | 1 + src/datetime.rs | 15 +++++++++++++++ src/naive/date.rs | 14 ++++++++++++++ src/naive/datetime.rs | 15 +++++++++++++++ src/naive/time.rs | 14 ++++++++++++++ 5 files changed, 59 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 3feecf2..364e575 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ serde = { version = "<0.9", optional = true } [dev-dependencies] serde_json = { version = ">=0.7.0" } +bincode = { version = "0.6", features = ["serde"], default-features = false } diff --git a/src/datetime.rs b/src/datetime.rs index a0bf400..c0a5a19 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -517,6 +517,7 @@ mod serde { } #[cfg(test)] extern crate serde_json; + #[cfg(test)] extern crate bincode; #[test] fn test_serde_serialize() { @@ -537,6 +538,20 @@ mod serde { assert!(from_str(r#""2014-07-32T12:34:06Z""#).is_err()); } + + #[test] + fn test_serde_bincode() { + // Bincode is relevant to test separately from JSON because + // it is not self-describing. + use self::bincode::SizeLimit; + use self::bincode::serde::{serialize, deserialize}; + + let dt = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6); + let encoded = serialize(&dt, SizeLimit::Infinite).unwrap(); + let decoded: DateTime = deserialize(&encoded).unwrap(); + assert_eq!(dt, decoded); + assert_eq!(dt.offset(), decoded.offset()); + } } #[cfg(test)] diff --git a/src/naive/date.rs b/src/naive/date.rs index 770279a..6ec3cab 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1586,6 +1586,7 @@ mod serde { } #[cfg(test)] extern crate serde_json; + #[cfg(test)] extern crate bincode; #[test] fn test_serde_serialize() { @@ -1637,6 +1638,19 @@ mod serde { assert!(from_str(r#"{"ymdf":20}"#).is_err()); // :( assert!(from_str(r#"null"#).is_err()); } + + #[test] + fn test_serde_bincode() { + // Bincode is relevant to test separately from JSON because + // it is not self-describing. + use self::bincode::SizeLimit; + use self::bincode::serde::{serialize, deserialize}; + + let d = NaiveDate::from_ymd(2014, 7, 24); + let encoded = serialize(&d, SizeLimit::Infinite).unwrap(); + let decoded: NaiveDate = deserialize(&encoded).unwrap(); + assert_eq!(d, decoded); + } } #[cfg(test)] diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index e583d8e..9900a85 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1148,6 +1148,7 @@ mod serde { } #[cfg(test)] extern crate serde_json; + #[cfg(test)] extern crate bincode; #[test] fn test_serde_serialize() { @@ -1230,6 +1231,20 @@ mod serde { assert!(from_str(r#"{"date":{"ymdf":20},"time":{"secs":0,"frac":0}}"#).is_err()); // :( assert!(from_str(r#"null"#).is_err()); } + + #[test] + fn test_serde_bincode() { + // Bincode is relevant to test separately from JSON because + // it is not self-describing. + use naive::date::NaiveDate; + use self::bincode::SizeLimit; + use self::bincode::serde::{serialize, deserialize}; + + let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90); + let encoded = serialize(&dt, SizeLimit::Infinite).unwrap(); + let decoded: NaiveDateTime = deserialize(&encoded).unwrap(); + assert_eq!(dt, decoded); + } } #[cfg(test)] diff --git a/src/naive/time.rs b/src/naive/time.rs index 12d12b0..6fff14f 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1273,6 +1273,7 @@ mod serde { } #[cfg(test)] extern crate serde_json; + #[cfg(test)] extern crate bincode; #[test] fn test_serde_serialize() { @@ -1340,6 +1341,19 @@ mod serde { assert!(from_str(r#"{"secs":0,"frac":0}"#).is_err()); // :( assert!(from_str(r#"null"#).is_err()); } + + #[test] + fn test_serde_bincode() { + // Bincode is relevant to test separately from JSON because + // it is not self-describing. + use self::bincode::SizeLimit; + use self::bincode::serde::{serialize, deserialize}; + + let t = NaiveTime::from_hms_nano(3, 5, 7, 98765432); + let encoded = serialize(&t, SizeLimit::Infinite).unwrap(); + let decoded: NaiveTime = deserialize(&encoded).unwrap(); + assert_eq!(t, decoded); + } } #[cfg(test)]