Skip to content

Commit

Permalink
add optional rkyv support
Browse files Browse the repository at this point in the history
  • Loading branch information
dovahcrow committed Jan 20, 2022
1 parent f6bd567 commit ac0ed38
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 50 deletions.
31 changes: 16 additions & 15 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
71 changes: 36 additions & 35 deletions Cargo.toml
@@ -1,62 +1,63 @@
[package]
name = "chrono"
version = "0.4.19"
authors = [
"Kang Seonghoon <public+rust@mearie.org>",
"Brandon W Maister <quodlibetor@gmail.com>",
"Kang Seonghoon <public+rust@mearie.org>",
"Brandon W Maister <quodlibetor@gmail.com>",
]
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"
Expand All @@ -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"]
3 changes: 3 additions & 0 deletions src/datetime.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Tz: TimeZone> {
datetime: NaiveDateTime,
offset: Tz::Offset,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions src/naive/date.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/naive/datetime.rs
Expand Up @@ -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`
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/naive/time.rs
Expand Up @@ -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 };
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/offset/fixed.rs
Expand Up @@ -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;

Expand All @@ -20,6 +22,7 @@ use Timelike;
/// `DateTime<FixedOffset>` 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,
}
Expand Down
3 changes: 3 additions & 0 deletions src/offset/utc.rs
Expand Up @@ -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"))
Expand All @@ -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")]
Expand Down

0 comments on commit ac0ed38

Please sign in to comment.