Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rkyv-08 feature #1619

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ rkyv-16 = ["dep:rkyv", "rkyv?/size_16"]
rkyv-32 = ["dep:rkyv", "rkyv?/size_32"]
rkyv-64 = ["dep:rkyv", "rkyv?/size_64"]
rkyv-validation = ["rkyv?/validation"]
rkyv-08 = ["dep:rkyv-08", "rkyv-08/pointer_width_32"]
rkyv-08-16 = ["dep:rkyv-08", "rkyv-08/pointer_width_16"]
rkyv-08-32 = ["dep:rkyv-08", "rkyv-08/pointer_width_32"]
rkyv-08-64 = ["dep:rkyv-08", "rkyv-08/pointer_width_64"]
rkyv-08-bytecheck = ["rkyv-08/bytecheck"]
# Features for internal use only:
__internal_bench = []

Expand All @@ -42,6 +47,7 @@ num-traits = { version = "0.2", default-features = false }
serde = { version = "1.0.99", default-features = false, optional = true }
pure-rust-locales = { version = "0.8", optional = true }
rkyv = { version = "0.7.43", optional = true, default-features = false }
rkyv-08 = { package = "rkyv", version = "0.8.8", 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]
Expand Down
15 changes: 14 additions & 1 deletion src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ use crate::{Datelike, Months, TimeDelta, Timelike, Weekday};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

/// documented at re-export site
#[cfg(feature = "serde")]
pub(super) mod serde;
Expand All @@ -52,6 +60,11 @@ mod tests;
derive(Archive, Deserialize, Serialize),
archive(compare(PartialEq, PartialOrd))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(crate = rkyv_08, compare(PartialEq, PartialOrd))
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct DateTime<Tz: TimeZone> {
datetime: NaiveDateTime,
Expand Down Expand Up @@ -1770,7 +1783,7 @@ impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
// * https://github.com/rust-lang/rust/issues/26925
// * https://github.com/rkyv/rkyv/issues/333
// * https://github.com/dtolnay/syn/issues/370
#[cfg(feature = "rkyv-validation")]
#[cfg(any(feature = "rkyv-validation", feature = "rkyv-08-bytecheck"))]
impl<Tz: TimeZone> fmt::Debug for ArchivedDateTime<Tz>
where
Tz: Archive,
Expand Down
25 changes: 25 additions & 0 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ use core::fmt;
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

use crate::OutOfRange;

/// The month of the year.
Expand Down Expand Up @@ -35,6 +43,15 @@ use crate::OutOfRange;
archive(compare(PartialEq, PartialOrd)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq, PartialOrd),
derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(arbitrary::Arbitrary))]
pub enum Month {
Expand Down Expand Up @@ -439,4 +456,12 @@ mod tests {
let bytes = rkyv::to_bytes::<_, 1>(&month).unwrap();
assert_eq!(rkyv::from_bytes::<Month>(&bytes).unwrap(), month);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_08_bytecheck() {
let month = Month::January;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&month).unwrap();
assert_eq!(rkyv_08::from_bytes::<Month, rkyv_08::rancor::Error>(&bytes).unwrap(), month);
}
}
17 changes: 17 additions & 0 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ use core::{fmt, str};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

/// L10n locales.
#[cfg(all(feature = "unstable-locales", feature = "alloc"))]
use pure_rust_locales::Locale;
Expand Down Expand Up @@ -98,6 +106,15 @@ mod tests;
archive(compare(PartialEq, PartialOrd)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq, PartialOrd),
derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct NaiveDate {
yof: NonZeroI32, // (year << 13) | of
Expand Down
12 changes: 12 additions & 0 deletions src/naive/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,18 @@ fn test_rkyv_validation() {
assert_eq!(rkyv::from_bytes::<NaiveDate>(&bytes).unwrap(), date_max);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_validation() {
let date_min = NaiveDate::MIN;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&date_min).unwrap();
assert_eq!(rkyv_08::from_bytes::<NaiveDate, rkyv_08::rancor::Error>(&bytes).unwrap(), date_min);

let date_max = NaiveDate::MAX;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&date_max).unwrap();
assert_eq!(rkyv_08::from_bytes::<NaiveDate, rkyv_08::rancor::Error>(&bytes).unwrap(), date_max);
}

// MAX_YEAR-12-31 minus 0000-01-01
// = (MAX_YEAR-12-31 minus 0000-12-31) + (0000-12-31 - 0000-01-01)
// = MAX_YEAR * 365 + (# of leap years from 0001 to MAX_YEAR) + 365
Expand Down
17 changes: 17 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ use core::{fmt, str};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

#[cfg(feature = "alloc")]
use crate::format::DelayedFormat;
use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems};
Expand Down Expand Up @@ -71,6 +79,15 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime::MAX;
archive(compare(PartialEq, PartialOrd)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq, PartialOrd),
derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(arbitrary::Arbitrary))]
pub struct NaiveDateTime {
Expand Down
18 changes: 18 additions & 0 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,21 @@ fn test_rkyv_validation() {
let bytes = rkyv::to_bytes::<_, 12>(&dt_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveDateTime>(&bytes).unwrap(), dt_max);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_validation() {
let dt_min = NaiveDateTime::MIN;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&dt_min).unwrap();
assert_eq!(
rkyv_08::from_bytes::<NaiveDateTime, rkyv_08::rancor::Error>(&bytes).unwrap(),
dt_min
);

let dt_max = NaiveDateTime::MAX;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&dt_max).unwrap();
assert_eq!(
rkyv_08::from_bytes::<NaiveDateTime, rkyv_08::rancor::Error>(&bytes).unwrap(),
dt_max
);
}
37 changes: 36 additions & 1 deletion src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ use super::internals::YearFlags;
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

/// ISO 8601 week.
///
/// This type, combined with [`Weekday`](../enum.Weekday.html),
Expand All @@ -23,6 +31,15 @@ use rkyv::{Archive, Deserialize, Serialize};
archive(compare(PartialEq, PartialOrd)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq, PartialOrd),
derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct IsoWeek {
// Note that this allows for larger year range than `NaiveDate`.
Expand Down Expand Up @@ -162,7 +179,7 @@ impl fmt::Debug for IsoWeek {

#[cfg(test)]
mod tests {
#[cfg(feature = "rkyv-validation")]
#[cfg(any(feature = "rkyv-validation", feature = "rkyv-08-bytecheck"))]
use super::IsoWeek;
use crate::naive::date::{self, NaiveDate};
use crate::Datelike;
Expand Down Expand Up @@ -230,4 +247,22 @@ mod tests {
let bytes = rkyv::to_bytes::<_, 4>(&maxweek).unwrap();
assert_eq!(rkyv::from_bytes::<IsoWeek>(&bytes).unwrap(), maxweek);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_bytecheck() {
let minweek = NaiveDate::MIN.iso_week();
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&minweek).unwrap();
assert_eq!(
rkyv_08::from_bytes::<IsoWeek, rkyv_08::rancor::Error>(&bytes).unwrap(),
minweek
);

let maxweek = NaiveDate::MAX.iso_week();
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&maxweek).unwrap();
assert_eq!(
rkyv_08::from_bytes::<IsoWeek, rkyv_08::rancor::Error>(&bytes).unwrap(),
maxweek
);
}
}
17 changes: 17 additions & 0 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ use core::{fmt, str};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

#[cfg(feature = "alloc")]
use crate::format::DelayedFormat;
use crate::format::{
Expand Down Expand Up @@ -216,6 +224,15 @@ mod tests;
archive(compare(PartialEq, PartialOrd)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq, PartialOrd),
derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct NaiveTime {
secs: u32,
Expand Down
12 changes: 12 additions & 0 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,15 @@ fn test_rkyv_validation() {
let bytes = rkyv::to_bytes::<_, 8>(&t_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveTime>(&bytes).unwrap(), t_max);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_bytecheck() {
let t_min = NaiveTime::MIN;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&t_min).unwrap();
assert_eq!(rkyv_08::from_bytes::<NaiveTime, rkyv_08::rancor::Error>(&bytes).unwrap(), t_min);

let t_max = NaiveTime::MAX;
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&t_max).unwrap();
assert_eq!(rkyv_08::from_bytes::<NaiveTime, rkyv_08::rancor::Error>(&bytes).unwrap(), t_max);
}
28 changes: 28 additions & 0 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ use core::str::FromStr;
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(
feature = "rkyv-08",
feature = "rkyv-08-16",
feature = "rkyv-08-32",
feature = "rkyv-08-64"
))]
use rkyv_08::{Archive, Deserialize, Serialize};

use super::{MappedLocalTime, Offset, TimeZone};
use crate::format::{scan, ParseError, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime};
Expand All @@ -26,6 +34,15 @@ use crate::naive::{NaiveDate, NaiveDateTime};
archive(compare(PartialEq)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, Hash, Debug))
)]
#[cfg_attr(
any(feature = "rkyv-08", feature = "rkyv-08-16", feature = "rkyv-08-32", feature = "rkyv-08-64"),
derive(Archive, Deserialize, Serialize),
rkyv(
crate = rkyv_08,
compare(PartialEq),
derive(Clone, Copy, PartialEq, Eq, Hash, Debug),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct FixedOffset {
local_minus_utc: i32,
Expand Down Expand Up @@ -233,4 +250,15 @@ mod tests {
let bytes = rkyv::to_bytes::<_, 4>(&offset).unwrap();
assert_eq!(rkyv::from_bytes::<FixedOffset>(&bytes).unwrap(), offset);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_bytecheck() {
let offset = FixedOffset::from_str("-0500").unwrap();
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&offset).unwrap();
assert_eq!(
rkyv_08::from_bytes::<FixedOffset, rkyv_08::rancor::Error>(&bytes).unwrap(),
offset
);
}
}
Loading
Loading