From e5a592d152aa01e6768c45580581145a6a8c80ec Mon Sep 17 00:00:00 2001 From: Weiyuan Wu Date: Fri, 20 Aug 2021 06:13:37 +0000 Subject: [PATCH 1/2] add optional rkyv support --- CHANGELOG.md | 1 + Cargo.toml | 1 + src/date.rs | 4 +++- src/naive/date.rs | 3 +++ src/naive/datetime/mod.rs | 3 +++ src/naive/time/mod.rs | 3 +++ src/offset/fixed.rs | 3 +++ src/offset/local.rs | 3 +++ src/offset/utc.rs | 3 +++ 9 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd8facd028..e596cf406e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Versions with only mechanical changes will be omitted from the following list. * Correct build for wasm32-unknown-emscripten target (#568) * Change `Local::now()` and `Utc::now()` documentation from "current date" to "current date and time" (#647) * Fix `duration_round` panic on rounding by `Duration::zero()` (#658) +* Add optional rkyv support. ## 0.4.19 diff --git a/Cargo.toml b/Cargo.toml index 03ad3eb263..0a7c37cd86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ rustc-serialize = { version = "0.3.20", optional = true } serde = { version = "1.0.99", default-features = false, optional = true } pure-rust-locales = { version = "0.5.2", optional = true } criterion = { version = "0.3", optional = true } +rkyv = {version = "0.7", optional = true} [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] wasm-bindgen = { version = "0.2", optional = true } diff --git a/src/date.rs b/src/date.rs index a3cb88a0bc..1904378389 100644 --- a/src/date.rs +++ b/src/date.rs @@ -18,7 +18,8 @@ use crate::naive::{self, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::{TimeZone, Utc}; use crate::DateTime; use crate::{Datelike, Weekday}; - +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; /// ISO 8601 calendar date with time zone. /// /// You almost certainly want to be using a [`NaiveDate`] instead of this type. @@ -51,6 +52,7 @@ use crate::{Datelike, Weekday}; /// so the local date and UTC date should be equal for most cases /// even though the raw calculation between `NaiveDate` and `Duration` may not. #[derive(Clone)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct Date { date: NaiveDate, offset: Tz::Offset, diff --git a/src/naive/date.rs b/src/naive/date.rs index d0b6b4c33a..70011478b4 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -17,6 +17,8 @@ use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Item, Numeric, Pad}; use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime}; use crate::{Datelike, Weekday}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use super::internals::{self, DateImpl, Mdf, Of, YearFlags}; use super::isoweek; @@ -96,6 +98,7 @@ const MAX_BITS: usize = 44; /// /// This is currently the internal format of Chrono's date types. #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct NaiveDate { ymdf: DateImpl, // (year << 13) | of } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 6d37af442e..9744453bc9 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -20,6 +20,8 @@ use crate::naive::time::{MAX_TIME, MIN_TIME}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::oldtime::Duration as OldDuration; use crate::{Datelike, Timelike, Weekday}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "rustc-serialize")] pub(super) mod rustc_serialize; @@ -70,6 +72,7 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime { date: MAX_DATE, time: MA /// assert_eq!(dt.num_seconds_from_midnight(), 33011); /// ``` #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct NaiveDateTime { date: NaiveDate, time: NaiveTime, diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 8046214d23..ea64dc0810 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -15,6 +15,8 @@ use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; use crate::oldtime::Duration as OldDuration; use crate::Timelike; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "rustc-serialize")] mod rustc_serialize; @@ -189,6 +191,7 @@ pub(super) const MAX_TIME: NaiveTime = /// Since Chrono alone cannot determine any existence of leap seconds, /// **there is absolutely no guarantee that the leap second read has actually happened**. #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct NaiveTime { secs: u32, frac: u32, diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 3b31402a4b..9bca837039 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -12,6 +12,8 @@ use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; use crate::oldtime::Duration as OldDuration; use crate::DateTime; use crate::Timelike; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// @@ -20,6 +22,7 @@ use crate::Timelike; /// `DateTime` instances. See the [`east`](#method.east) and /// [`west`](#method.west) methods for examples. #[derive(PartialEq, Eq, Hash, Copy, Clone)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct FixedOffset { local_minus_utc: i32, } diff --git a/src/offset/local.rs b/src/offset/local.rs index 3eef187bb8..205e2257f1 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -14,6 +14,8 @@ use crate::naive::{NaiveDate, NaiveDateTime}; use crate::{Date, DateTime}; #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] use crate::{Datelike, Timelike}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; /// Converts a `time::Tm` struct into the timezone-aware `DateTime`. /// This assumes that `time` is working correctly, i.e. any error is fatal. @@ -88,6 +90,7 @@ fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> sys::Timespec { /// let dt: DateTime = Local.timestamp(0, 0); /// ``` #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct Local; impl Local { diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 42e76a9c1d..54c1c022e8 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -9,6 +9,8 @@ use super::{FixedOffset, LocalResult, Offset, TimeZone}; use crate::naive::{NaiveDate, NaiveDateTime}; #[cfg(feature = "clock")] use crate::{Date, DateTime}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(all( feature = "clock", not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")) @@ -33,6 +35,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; /// assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 1, 1), dt); /// ``` #[derive(Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] pub struct Utc; #[cfg(feature = "clock")] From f7d6fde940dbfbfcf1b88afa5d100326378bfeac Mon Sep 17 00:00:00 2001 From: Weiyuan Wu Date: Wed, 23 Mar 2022 20:44:36 +0000 Subject: [PATCH 2/2] tidy up imports --- src/date.rs | 8 +++++--- src/naive/date.rs | 7 ++++--- src/naive/datetime/mod.rs | 4 ++-- src/naive/time/mod.rs | 5 +++-- src/offset/fixed.rs | 5 +++-- src/offset/local.rs | 8 ++++---- src/offset/utc.rs | 15 ++++++++------- 7 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/date.rs b/src/date.rs index 1904378389..0758cc844f 100644 --- a/src/date.rs +++ b/src/date.rs @@ -3,23 +3,25 @@ //! ISO 8601 calendar date with time zone. -use crate::oldtime::Duration as OldDuration; #[cfg(any(feature = "alloc", feature = "std", test))] use core::borrow::Borrow; use core::cmp::Ordering; use core::ops::{Add, Sub}; use core::{fmt, hash}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; + #[cfg(feature = "unstable-locales")] use crate::format::Locale; #[cfg(any(feature = "alloc", feature = "std", test))] use crate::format::{DelayedFormat, Item, StrftimeItems}; use crate::naive::{self, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::{TimeZone, Utc}; +use crate::oldtime::Duration as OldDuration; use crate::DateTime; use crate::{Datelike, Weekday}; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; + /// ISO 8601 calendar date with time zone. /// /// You almost certainly want to be using a [`NaiveDate`] instead of this type. diff --git a/src/naive/date.rs b/src/naive/date.rs index 70011478b4..50ba1129a8 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -3,22 +3,23 @@ //! ISO 8601 calendar date without timezone. -use crate::oldtime::Duration as OldDuration; #[cfg(any(feature = "alloc", feature = "std", test))] use core::borrow::Borrow; use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::{fmt, str}; + use num_integer::div_mod_floor; use num_traits::ToPrimitive; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(any(feature = "alloc", feature = "std", test))] use crate::format::DelayedFormat; use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Item, Numeric, Pad}; use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime}; +use crate::oldtime::Duration as OldDuration; use crate::{Datelike, Weekday}; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; use super::internals::{self, DateImpl, Mdf, Of, YearFlags}; use super::isoweek; diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 9744453bc9..cd1853c338 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -10,6 +10,8 @@ use core::{fmt, str}; use num_integer::div_mod_floor; use num_traits::ToPrimitive; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(any(feature = "alloc", feature = "std", test))] use crate::format::DelayedFormat; @@ -20,8 +22,6 @@ use crate::naive::time::{MAX_TIME, MIN_TIME}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::oldtime::Duration as OldDuration; use crate::{Datelike, Timelike, Weekday}; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "rustc-serialize")] pub(super) mod rustc_serialize; diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index ea64dc0810..29b0929a06 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -7,7 +7,10 @@ use core::borrow::Borrow; use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::{fmt, str}; + use num_integer::div_mod_floor; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; #[cfg(any(feature = "alloc", feature = "std", test))] use crate::format::DelayedFormat; @@ -15,8 +18,6 @@ use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; use crate::oldtime::Duration as OldDuration; use crate::Timelike; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "rustc-serialize")] mod rustc_serialize; diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 9bca837039..df1a990bac 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -5,15 +5,16 @@ use core::fmt; use core::ops::{Add, Sub}; + use num_integer::div_mod_floor; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use super::{LocalResult, Offset, TimeZone}; use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; use crate::oldtime::Duration as OldDuration; use crate::DateTime; use crate::Timelike; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// diff --git a/src/offset/local.rs b/src/offset/local.rs index 205e2257f1..f1da534212 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -3,19 +3,19 @@ //! The local (system) time zone. -#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] -use crate::sys::{self, Timespec}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use super::fixed::FixedOffset; use super::{LocalResult, TimeZone}; #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] use crate::naive::NaiveTime; use crate::naive::{NaiveDate, NaiveDateTime}; +#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] +use crate::sys::{self, Timespec}; use crate::{Date, DateTime}; #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] use crate::{Datelike, Timelike}; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; /// Converts a `time::Tm` struct into the timezone-aware `DateTime`. /// This assumes that `time` is working correctly, i.e. any error is fatal. diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 54c1c022e8..730c03e95f 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -4,19 +4,20 @@ //! The UTC (Coordinated Universal Time) time zone. use core::fmt; - -use super::{FixedOffset, LocalResult, Offset, TimeZone}; -use crate::naive::{NaiveDate, NaiveDateTime}; -#[cfg(feature = "clock")] -use crate::{Date, DateTime}; -#[cfg(feature = "rkyv")] -use rkyv::{Archive, Deserialize, Serialize}; #[cfg(all( feature = "clock", not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")) ))] use std::time::{SystemTime, UNIX_EPOCH}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; + +use super::{FixedOffset, LocalResult, Offset, TimeZone}; +use crate::naive::{NaiveDate, NaiveDateTime}; +#[cfg(feature = "clock")] +use crate::{Date, DateTime}; + /// The UTC time zone. This is the most efficient time zone when you don't need the local time. /// It is also used as an offset (which is also a dummy type). ///