From e287287b222b1539b34aa7acc0c79c5329e75993 Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Fri, 1 Dec 2023 11:08:21 -0800 Subject: [PATCH] Expose rkyv features as features for chrono users. rkyv by default serializes usize as u32. This isn't ideal on most modern platforms and unfortunately is configured through a feature flag. If we just set `default-features = false` in the rkyv Cargo dependency, the crate fails to compile because all the size features are mutually exclusive. On the other hand if we want to e.g., change the serialization of usize to 64-bit and we also want to use chrono this currently fails to compile because chrono always enables rkyv/size_32. This re-exports the relevant rkyv features so users can choose which serialization to enable. The approach is similar to what the ordered-float crate does: https://github.com/reem/rust-ordered-float/blob/8111b345372632893af0b8aa12152f3dc7278aba/Cargo.toml#L37 Signed-off-by: Gerd Zellweger --- Cargo.toml | 7 ++++++- src/datetime/mod.rs | 7 +++++-- src/lib.rs | 2 +- src/month.rs | 9 ++++++--- src/naive/date.rs | 9 ++++++--- src/naive/datetime/mod.rs | 9 ++++++--- src/naive/isoweek.rs | 9 ++++++--- src/naive/time/mod.rs | 9 ++++++--- src/offset/fixed.rs | 12 +++++++++--- src/offset/local/mod.rs | 12 +++++++++--- src/offset/utc.rs | 12 +++++++++--- src/time_delta.rs | 9 ++++++--- src/weekday.rs | 12 +++++++++--- 13 files changed, 84 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2830bd4302..bfefb040ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,11 +26,16 @@ clock = ["std", "winapi", "iana-time-zone", "android-tzdata"] wasmbind = ["wasm-bindgen", "js-sys"] unstable-locales = ["pure-rust-locales"] __internal_bench = [] +rkyv = ["rkyv_32"] +rkyv_16 = ["dep:rkyv", "rkyv?/size_16"] +rkyv_32 = ["dep:rkyv", "rkyv?/size_32"] +rkyv_64 = ["dep:rkyv", "rkyv?/size_64"] +rkyv_ck = ["rkyv?/validation"] [dependencies] serde = { version = "1.0.99", default-features = false, optional = true } pure-rust-locales = { version = "0.7", optional = true } -rkyv = { version = "0.7", optional = true } +rkyv = { version = "0.7", optional = true, default-features = false } arbitrary = { version = "1.0.0", features = ["derive"], optional = true } [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 6efd15ef8c..122b8c333f 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -28,7 +28,7 @@ use crate::offset::Local; use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; use crate::{Datelike, Months, TimeDelta, Timelike, Weekday}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; /// documented at re-export site @@ -73,7 +73,10 @@ pub enum SecondsFormat { /// the general-purpose constructors are all via the methods on the /// [`TimeZone`](./offset/trait.TimeZone.html) implementations. #[derive(Clone)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] pub struct DateTime { datetime: NaiveDateTime, offset: Tz::Offset, diff --git a/src/lib.rs b/src/lib.rs index 0d092fcec1..cb700e5a7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -544,7 +544,7 @@ pub mod serde { /// Zero-copy serialization/deserialization with rkyv. /// /// This module re-exports the `Archived*` versions of chrono's types. -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] pub mod rkyv { pub use crate::datetime::ArchivedDateTime; pub use crate::month::ArchivedMonth; diff --git a/src/month.rs b/src/month.rs index acb96a2da1..185195c6a6 100644 --- a/src/month.rs +++ b/src/month.rs @@ -1,6 +1,6 @@ use core::fmt; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; use crate::OutOfRange; @@ -29,9 +29,12 @@ use crate::OutOfRange; /// Can be Serialized/Deserialized with serde // Actual implementation is zero-indexed, API intended as 1-indexed for more intuitive behavior. #[derive(PartialEq, Eq, Copy, Clone, Debug, Hash, PartialOrd, Ord)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] diff --git a/src/naive/date.rs b/src/naive/date.rs index faccc06024..8d6654e553 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -9,7 +9,7 @@ use core::iter::FusedIterator; use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign}; use core::{fmt, str}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; /// L10n locales. @@ -190,9 +190,12 @@ impl Days { /// /// [proleptic Gregorian date]: crate::NaiveDate#calendar-date #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] pub struct NaiveDate { diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 3816be9c42..443dce7d6a 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -10,7 +10,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::time::Duration; use core::{fmt, str}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] @@ -72,9 +72,12 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime::MAX; /// assert_eq!(dt.num_seconds_from_midnight(), 33011); /// ``` #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] diff --git a/src/naive/isoweek.rs b/src/naive/isoweek.rs index 606699659b..872b700975 100644 --- a/src/naive/isoweek.rs +++ b/src/naive/isoweek.rs @@ -7,7 +7,7 @@ use core::fmt; use super::internals::{DateImpl, Of, YearFlags}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; /// ISO 8601 week. @@ -17,9 +17,12 @@ use rkyv::{Archive, Deserialize, Serialize}; /// One can retrieve this type from the existing [`Datelike`](../trait.Datelike.html) types /// via the [`Datelike::iso_week`](../trait.Datelike.html#tymethod.iso_week) method. #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] pub struct IsoWeek { diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index f2a0693ffd..3e5a13e65a 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -9,7 +9,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::time::Duration; use core::{fmt, str}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] @@ -199,9 +199,12 @@ mod tests; /// 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))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] pub struct NaiveTime { diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 1c3dc5e366..38b41d350f 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -6,7 +6,7 @@ use core::fmt; use core::str::FromStr; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; use super::{LocalResult, Offset, TimeZone}; @@ -20,8 +20,14 @@ use crate::{NaiveDateTime, ParseError}; /// `DateTime` instances. See the [`east_opt`](#method.east_opt) and /// [`west_opt`](#method.west_opt) methods for examples. #[derive(PartialEq, Eq, Hash, Copy, Clone)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] -#[cfg_attr(feature = "rkyv", archive_attr(derive(Clone, Copy, PartialEq, Eq, Hash, Debug)))] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + archive_attr(derive(Clone, Copy, PartialEq, Eq, Hash, Debug)) +)] pub struct FixedOffset { local_minus_utc: i32, } diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index bc48b322e2..5e83dba4ba 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -3,7 +3,7 @@ //! The local (system) time zone. -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; use super::fixed::FixedOffset; @@ -101,8 +101,14 @@ mod tz_info; /// assert!(dt1 >= dt2); /// ``` #[derive(Copy, Clone, Debug)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] -#[cfg_attr(feature = "rkyv", archive_attr(derive(Clone, Copy, Debug)))] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + archive_attr(derive(Clone, Copy, Debug)) +)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Local; diff --git a/src/offset/utc.rs b/src/offset/utc.rs index e81949ec96..f43d290b70 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -14,7 +14,7 @@ use core::fmt; ))] use std::time::{SystemTime, UNIX_EPOCH}; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; use super::{FixedOffset, LocalResult, Offset, TimeZone}; @@ -40,8 +40,14 @@ use crate::DateTime; /// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt); /// ``` #[derive(Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] -#[cfg_attr(feature = "rkyv", archive_attr(derive(Clone, Copy, PartialEq, Eq, Debug, Hash)))] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + archive_attr(derive(Clone, Copy, PartialEq, Eq, Debug, Hash)) +)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Utc; diff --git a/src/time_delta.rs b/src/time_delta.rs index 84d049275a..efa9366bd2 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -16,7 +16,7 @@ use core::{fmt, i64}; #[cfg(feature = "std")] use std::error::Error; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; /// The number of nanoseconds in a microsecond. @@ -51,9 +51,12 @@ macro_rules! try_opt { /// /// This also allows for the negative duration; see individual methods for details. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr( - feature = "rkyv", + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)) )] pub struct TimeDelta { diff --git a/src/weekday.rs b/src/weekday.rs index a9c51069c4..ec45e816a4 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -1,6 +1,6 @@ use core::fmt; -#[cfg(feature = "rkyv")] +#[cfg(any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"))] use rkyv::{Archive, Deserialize, Serialize}; use crate::OutOfRange; @@ -30,8 +30,14 @@ use crate::OutOfRange; /// assert_eq!(sunday.pred(), Weekday::Sat); /// ``` #[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)] -#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] -#[cfg_attr(feature = "rkyv", archive_attr(derive(Clone, Copy, PartialEq, Eq, Debug, Hash)))] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + derive(Archive, Deserialize, Serialize) +)] +#[cfg_attr( + any(feature = "rkyv_16", feature = "rkyv_32", feature = "rkyv_64"), + archive_attr(derive(Clone, Copy, PartialEq, Eq, Debug, Hash)) +)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub enum Weekday { /// Monday.