From ac0ed387d5e594a8842284770b343cead4ac9aa0 Mon Sep 17 00:00:00 2001 From: Weiyuan Wu Date: Fri, 20 Aug 2021 06:13:37 +0000 Subject: [PATCH] add optional rkyv support --- CHANGELOG.md | 31 ++++++++++--------- Cargo.toml | 71 ++++++++++++++++++++++--------------------- src/datetime.rs | 3 ++ src/lib.rs | 3 ++ src/naive/date.rs | 3 ++ src/naive/datetime.rs | 3 ++ src/naive/time.rs | 3 ++ src/offset/fixed.rs | 3 ++ src/offset/utc.rs | 3 ++ 9 files changed, 73 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d92797e587..40939c5020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Versions with only mechanical changes will be omitted from the following list. * Implement `DurationRound` for `NaiveDateTime` * Add `DateTime::from_local()` to construct from given local date and time (#572) * Correct build for wasm32-unknown-emscripten target (#568) +* Add optional rkyv support. ## 0.4.19 @@ -291,21 +292,21 @@ release due to the compatibility concern raised. The full list of changes is as follows: - Before | After - ---------------------------------------- | ---------------------------- - `chrono::date::Date` | `chrono::Date` - `chrono::date::MIN` | `chrono::MIN_DATE` - `chrono::date::MAX` | `chrono::MAX_DATE` - `chrono::datetime::DateTime` | `chrono::DateTime` - `chrono::naive::time::NaiveTime` | `chrono::naive::NaiveTime` - `chrono::naive::date::NaiveDate` | `chrono::naive::NaiveDate` - `chrono::naive::date::MIN` | `chrono::naive::MIN_DATE` - `chrono::naive::date::MAX` | `chrono::naive::MAX_DATE` - `chrono::naive::datetime::NaiveDateTime` | `chrono::naive::NaiveDateTime` - `chrono::offset::utc::UTC` | `chrono::offset::Utc` - `chrono::offset::fixed::FixedOffset` | `chrono::offset::FixedOffset` - `chrono::offset::local::Local` | `chrono::offset::Local` - `chrono::format::parsed::Parsed` | `chrono::format::Parsed` + | Before | After | + | ---------------------------------------- | ------------------------------ | + | `chrono::date::Date` | `chrono::Date` | + | `chrono::date::MIN` | `chrono::MIN_DATE` | + | `chrono::date::MAX` | `chrono::MAX_DATE` | + | `chrono::datetime::DateTime` | `chrono::DateTime` | + | `chrono::naive::time::NaiveTime` | `chrono::naive::NaiveTime` | + | `chrono::naive::date::NaiveDate` | `chrono::naive::NaiveDate` | + | `chrono::naive::date::MIN` | `chrono::naive::MIN_DATE` | + | `chrono::naive::date::MAX` | `chrono::naive::MAX_DATE` | + | `chrono::naive::datetime::NaiveDateTime` | `chrono::naive::NaiveDateTime` | + | `chrono::offset::utc::UTC` | `chrono::offset::Utc` | + | `chrono::offset::fixed::FixedOffset` | `chrono::offset::FixedOffset` | + | `chrono::offset::local::Local` | `chrono::offset::Local` | + | `chrono::format::parsed::Parsed` | `chrono::format::Parsed` | With an exception of `Utc`, this change does not affect any direct usage of `chrono::*` or `chrono::prelude::*` types. diff --git a/Cargo.toml b/Cargo.toml index 6b4d48afa8..646c87fd97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,62 +1,63 @@ [package] -name = "chrono" -version = "0.4.19" authors = [ - "Kang Seonghoon ", - "Brandon W Maister ", + "Kang Seonghoon ", + "Brandon W Maister ", ] +name = "chrono" +version = "0.4.19" +categories = ["date-and-time"] description = "Date and time library for Rust" -homepage = "https://github.com/chronotope/chrono" documentation = "https://docs.rs/chrono/" -repository = "https://github.com/chronotope/chrono" +edition = "2015" +exclude = ["/ci/*", "/.travis.yml", "/appveyor.yml", "/Makefile"] +homepage = "https://github.com/chronotope/chrono" keywords = ["date", "time", "calendar"] -categories = ["date-and-time"] -readme = "README.md" license = "MIT/Apache-2.0" -exclude = ["/ci/*", "/.travis.yml", "/appveyor.yml", "/Makefile"] - +readme = "README.md" +repository = "https://github.com/chronotope/chrono" [badges] -travis-ci = { repository = "chronotope/chrono" } -appveyor = { repository = "chronotope/chrono" } +appveyor = {repository = "chronotope/chrono"} +travis-ci = {repository = "chronotope/chrono"} [lib] name = "chrono" [features] -default = ["clock", "std", "oldtime"] +__doctest = [] +__internal_bench = [] alloc = [] -std = [] clock = ["libc", "std", "winapi"] +default = ["clock", "std", "oldtime"] oldtime = ["time"] -wasmbind = ["wasm-bindgen", "js-sys"] +std = [] unstable-locales = ["pure-rust-locales", "alloc"] -__internal_bench = [] -__doctest = [] +wasmbind = ["wasm-bindgen", "js-sys"] [dependencies] -libc = { version = "0.2.69", optional = true } -time = { version = "0.1.43", optional = true } -num-integer = { version = "0.1.36", default-features = false } -num-traits = { version = "0.2", default-features = false } -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 } +libc = {version = "0.2.69", optional = true} +num-integer = {version = "0.1.36", default-features = false} +num-traits = {version = "0.2", default-features = false} +pure-rust-locales = {version = "0.5.2", optional = true} +rkyv = {version = "0.7", optional = true} +rustc-serialize = {version = "0.3.20", optional = true} +serde = {version = "1.0.99", default-features = false, optional = true} +time = {version = "0.1.43", optional = true} [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] -wasm-bindgen = { version = "0.2", optional = true } -js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API +js-sys = {version = "0.3", optional = true}# contains FFI bindings for the JS Date API +wasm-bindgen = {version = "0.2", optional = true} [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi"], optional = true } +winapi = {version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi"], optional = true} [dev-dependencies] -serde_json = { version = "1" } -serde_derive = { version = "1", default-features = false } -bincode = { version = "0.8.0" } -num-iter = { version = "0.1.35", default-features = false } -criterion = { version = "0.3" } -doc-comment = { version = "0.3" } +bincode = {version = "0.8.0"} +criterion = {version = "0.3"} +doc-comment = {version = "0.3"} +num-iter = {version = "0.1.35", default-features = false} +serde_derive = {version = "1", default-features = false} +serde_json = {version = "1"} [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies] wasm-bindgen-test = "0.3" @@ -68,11 +69,11 @@ features = ["serde"] features = ["serde"] [[bench]] +harness = false name = "chrono" required-features = ["__internal_bench"] -harness = false [[bench]] -name = "serde" harness = false +name = "serde" required-features = ["serde"] diff --git a/src/datetime.rs b/src/datetime.rs index 417fee0e0c..659566cc91 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -27,6 +27,8 @@ use naive::{self, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime}; #[cfg(feature = "clock")] use offset::Local; use offset::{FixedOffset, Offset, TimeZone, Utc}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use Date; use {Datelike, Timelike, Weekday}; @@ -67,6 +69,7 @@ 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))] pub struct DateTime { datetime: NaiveDateTime, offset: Tz::Offset, diff --git a/src/lib.rs b/src/lib.rs index e18a1da38e..1065a069c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -491,6 +491,9 @@ extern crate test; #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] extern crate wasm_bindgen; +#[cfg(feature = "rkyv")] +extern crate rkyv; + #[cfg(feature = "__doctest")] #[cfg_attr(feature = "__doctest", cfg(doctest))] doctest!("../README.md"); diff --git a/src/naive/date.rs b/src/naive/date.rs index 34cdc2c54d..710bc46fca 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -16,6 +16,8 @@ use format::DelayedFormat; use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use format::{Item, Numeric, Pad}; use naive::{IsoWeek, NaiveDateTime, NaiveTime}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use {Datelike, Weekday}; use super::internals::{self, DateImpl, Mdf, Of, YearFlags}; @@ -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.rs b/src/naive/datetime.rs index 213de89a16..59e74130cd 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -18,6 +18,8 @@ use format::{Fixed, Item, Numeric, Pad}; use naive::date::{MAX_DATE, MIN_DATE}; use naive::time::{MAX_TIME, MIN_TIME}; use naive::{IsoWeek, NaiveDate, NaiveTime}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use {Datelike, Timelike, Weekday}; /// The tight upper bound guarantees that a duration with `|Duration| >= 2^MAX_SECS_BITS` @@ -59,6 +61,7 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime { date: MAX_DATE, time: MA /// assert_eq!(dt.num_seconds_from_midnight(), 33011); /// ``` #[derive(PartialEq, Eq, 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.rs b/src/naive/time.rs index 5f3966b9b1..d049b60164 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -14,6 +14,8 @@ use div::div_mod_floor; use format::DelayedFormat; use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use format::{Fixed, Item, Numeric, Pad}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use Timelike; pub const MIN_TIME: NaiveTime = NaiveTime { secs: 0, frac: 0 }; @@ -179,6 +181,7 @@ pub const MAX_TIME: NaiveTime = NaiveTime { secs: 23 * 3600 + 59 * 60 + 59, frac /// 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, 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 441d0f63c1..e61544ac1c 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -10,6 +10,8 @@ use oldtime::Duration as OldDuration; use super::{LocalResult, Offset, TimeZone}; use div::div_mod_floor; use naive::{NaiveDate, NaiveDateTime, NaiveTime}; +#[cfg(feature = "rkyv")] +use rkyv::{Archive, Deserialize, Serialize}; use DateTime; use Timelike; @@ -20,6 +22,7 @@ use 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/utc.rs b/src/offset/utc.rs index aff86073d2..11bfd86fe1 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -7,6 +7,8 @@ use core::fmt; use super::{FixedOffset, LocalResult, Offset, TimeZone}; use naive::{NaiveDate, NaiveDateTime}; +#[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 {Date, DateTime}; /// 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")]