From 80c3582531284426bef157403a6af5428d1e2566 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Fri, 21 Apr 2017 21:12:26 +1000 Subject: [PATCH 1/3] upgrade to serde 1.0 --- Cargo.toml | 6 +++--- src/datetime.rs | 28 ++++++++++++++-------------- src/naive/date.rs | 10 +++++----- src/naive/datetime.rs | 10 +++++----- src/naive/time.rs | 10 +++++----- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90d1d31..6196612 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,8 @@ name = "chrono" time = "^0.1.36" num = { version = "0.1", default-features = false } rustc-serialize = { version = "0.3", optional = true } -serde = { version = "0.9", optional = true } +serde = { version = "1", optional = true } [dev-dependencies] -serde_json = { version = ">=0.9.0" } -bincode = { version = "1.0.0-alpha6", features = ["serde"], default-features = false } +serde_json = { version = "1" } +bincode = { version = "0.7.0", features = ["serde"], default-features = false, git = "https://github.com/TyOverby/bincode.git" } diff --git a/src/datetime.rs b/src/datetime.rs index 9052b81..0a1fd14 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -416,11 +416,11 @@ fn test_encodable_json(to_string_utc: FUTC, to_string_fixed: FF #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(utc_from_str: FUTC, - fixed_from_str: FFixed, - local_from_str: FLocal) - where FUTC: Fn(&str) -> Result, E>, - FFixed: Fn(&str) -> Result, E>, - FLocal: Fn(&str) -> Result, E>, + fixed_from_str: FFixed, + local_from_str: FLocal) + where FUTC: for<'de> Fn(&'de str) -> Result, E>, + FFixed: for<'de> Fn(&'de str) -> Result, E>, + FLocal: for<'de> Fn(&'de str) -> Result, E>, E: ::std::fmt::Debug { // should check against the offset as well (the normal DateTime comparison will ignore them) @@ -527,7 +527,7 @@ mod serde { struct DateTimeVisitor; - impl de::Visitor for DateTimeVisitor { + impl<'de> de::Visitor<'de> for DateTimeVisitor { type Value = DateTime; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result @@ -542,25 +542,25 @@ mod serde { } } - impl de::Deserialize for DateTime { + impl<'de> de::Deserialize<'de> for DateTime { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(DateTimeVisitor) } } - impl de::Deserialize for DateTime { + impl<'de> de::Deserialize<'de> for DateTime { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC)) } } - impl de::Deserialize for DateTime { + impl<'de> de::Deserialize<'de> for DateTime { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local)) } @@ -576,8 +576,8 @@ mod serde { #[test] fn test_serde_deserialize() { - super::test_decodable_json(self::serde_json::from_str, self::serde_json::from_str, - self::serde_json::from_str); + super::test_decodable_json(|input| self::serde_json::from_str(&input), |input| self::serde_json::from_str(&input), + |input| self::serde_json::from_str(&input)); } #[test] diff --git a/src/naive/date.rs b/src/naive/date.rs index 90ee26c..30061c8 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1495,7 +1495,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: Fn(&str) -> Result, E: ::std::fmt::Debug + where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug { use std::{i32, i64}; @@ -1576,7 +1576,7 @@ mod serde { struct NaiveDateVisitor; - impl de::Visitor for NaiveDateVisitor { + impl<'de> de::Visitor<'de> for NaiveDateVisitor { type Value = NaiveDate; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result @@ -1591,9 +1591,9 @@ mod serde { } } - impl de::Deserialize for NaiveDate { + impl<'de> de::Deserialize<'de> for NaiveDate { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(NaiveDateVisitor) } @@ -1609,7 +1609,7 @@ mod serde { #[test] fn test_serde_deserialize() { - super::test_decodable_json(self::serde_json::from_str); + super::test_decodable_json(|input| self::serde_json::from_str(&input)); } #[test] diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 33ba752..f029ffb 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1371,7 +1371,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: Fn(&str) -> Result, E: ::std::fmt::Debug + where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug { use naive::date; @@ -1474,7 +1474,7 @@ mod serde { struct NaiveDateTimeVisitor; - impl de::Visitor for NaiveDateTimeVisitor { + impl<'de> de::Visitor<'de> for NaiveDateTimeVisitor { type Value = NaiveDateTime; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result @@ -1489,9 +1489,9 @@ mod serde { } } - impl de::Deserialize for NaiveDateTime { + impl<'de> de::Deserialize<'de> for NaiveDateTime { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(NaiveDateTimeVisitor) } @@ -1507,7 +1507,7 @@ mod serde { #[test] fn test_serde_deserialize() { - super::test_decodable_json(self::serde_json::from_str); + super::test_decodable_json(|input| self::serde_json::from_str(&input)); } #[test] diff --git a/src/naive/time.rs b/src/naive/time.rs index a463eaf..89e56bc 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1261,7 +1261,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: Fn(&str) -> Result, E: ::std::fmt::Debug + where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug { assert_eq!(from_str(r#""00:00:00""#).ok(), Some(NaiveTime::from_hms(0, 0, 0))); @@ -1352,7 +1352,7 @@ mod serde { struct NaiveTimeVisitor; - impl de::Visitor for NaiveTimeVisitor { + impl<'de> de::Visitor<'de> for NaiveTimeVisitor { type Value = NaiveTime; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result @@ -1367,9 +1367,9 @@ mod serde { } } - impl de::Deserialize for NaiveTime { + impl<'de> de::Deserialize<'de> for NaiveTime { fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_str(NaiveTimeVisitor) } @@ -1385,7 +1385,7 @@ mod serde { #[test] fn test_serde_deserialize() { - super::test_decodable_json(self::serde_json::from_str); + super::test_decodable_json(|input| self::serde_json::from_str(&input)); } #[test] From 5504f6c854edaee806cb28a752996ef292caa785 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Fri, 21 Apr 2017 21:31:50 +1000 Subject: [PATCH 2/3] optimise serialize --- src/datetime.rs | 12 +++++++++++- src/naive/date.rs | 12 +++++++++++- src/naive/datetime.rs | 12 +++++++++++- src/naive/time.rs | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 0a1fd14..e3c3670 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -520,8 +520,18 @@ mod serde { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { + struct FormatWrapped<'a, D: 'a> { + inner: &'a D + } + + impl<'a, D: fmt::Debug> fmt::Display for FormatWrapped<'a, D> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } + } + // Debug formatting is correct RFC3339, and it allows Zulu. - serializer.serialize_str(&format!("{:?}", self)) + serializer.collect_str(&FormatWrapped { inner: &self }) } } diff --git a/src/naive/date.rs b/src/naive/date.rs index 30061c8..79cb3f2 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1570,7 +1570,17 @@ mod serde { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { - serializer.serialize_str(&format!("{:?}", self)) + struct FormatWrapped<'a, D: 'a> { + inner: &'a D + } + + impl<'a, D: fmt::Debug> fmt::Display for FormatWrapped<'a, D> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } + } + + serializer.collect_str(&FormatWrapped { inner: &self }) } } diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index f029ffb..f68210e 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1468,7 +1468,17 @@ mod serde { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { - serializer.serialize_str(&format!("{:?}", self)) + struct FormatWrapped<'a, D: 'a> { + inner: &'a D + } + + impl<'a, D: fmt::Debug> fmt::Display for FormatWrapped<'a, D> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } + } + + serializer.collect_str(&FormatWrapped { inner: &self }) } } diff --git a/src/naive/time.rs b/src/naive/time.rs index 89e56bc..7f0e7d3 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1346,7 +1346,7 @@ mod serde { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { - serializer.serialize_str(&format!("{:?}", self)) + serializer.collect_str(&self) } } From 1d1bfd93da72587a69262dbefa478bde1261c62a Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Mon, 24 Apr 2017 07:17:37 +1000 Subject: [PATCH 3/3] remove unneeded hrtb --- 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 e3c3670..6f6f71d 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -418,9 +418,9 @@ fn test_encodable_json(to_string_utc: FUTC, to_string_fixed: FF fn test_decodable_json(utc_from_str: FUTC, fixed_from_str: FFixed, local_from_str: FLocal) - where FUTC: for<'de> Fn(&'de str) -> Result, E>, - FFixed: for<'de> Fn(&'de str) -> Result, E>, - FLocal: for<'de> Fn(&'de str) -> Result, E>, + where FUTC: Fn(&str) -> Result, E>, + FFixed: Fn(&str) -> Result, E>, + FLocal: Fn(&str) -> Result, E>, E: ::std::fmt::Debug { // should check against the offset as well (the normal DateTime comparison will ignore them) diff --git a/src/naive/date.rs b/src/naive/date.rs index 79cb3f2..b302c37 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1495,7 +1495,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug + where F: Fn(&str) -> Result, E: ::std::fmt::Debug { use std::{i32, i64}; diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index f68210e..0271218 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1371,7 +1371,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug + where F: Fn(&str) -> Result, E: ::std::fmt::Debug { use naive::date; diff --git a/src/naive/time.rs b/src/naive/time.rs index 7f0e7d3..9157e9a 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1261,7 +1261,7 @@ fn test_encodable_json(to_string: F) #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(from_str: F) - where F: for<'de> Fn(&'de str) -> Result, E: ::std::fmt::Debug + where F: Fn(&str) -> Result, E: ::std::fmt::Debug { assert_eq!(from_str(r#""00:00:00""#).ok(), Some(NaiveTime::from_hms(0, 0, 0)));