Skip to content

Commit

Permalink
rkyv-08 feature implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Awpteamoose committed Oct 10, 2024
1 parent 05a6ce6 commit e96444f
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 2 deletions.
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
10 changes: 9 additions & 1 deletion src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ 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 +55,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 +1778,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
20 changes: 20 additions & 0 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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 +38,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 +451,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);
}
}
12 changes: 12 additions & 0 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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 +101,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
12 changes: 12 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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 +74,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
12 changes: 12 additions & 0 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,15 @@ 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);
}
26 changes: 25 additions & 1 deletion src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ 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 +26,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 +174,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 +242,16 @@ 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);
}
}
12 changes: 12 additions & 0 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ 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 +219,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);
}
20 changes: 20 additions & 0 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ 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 +29,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 +245,12 @@ 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);
}
}
25 changes: 25 additions & 0 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use std::cmp::Ordering;
#[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::fixed::FixedOffset;
use super::{MappedLocalTime, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
Expand Down Expand Up @@ -117,6 +120,15 @@ mod tz_info;
archive(compare(PartialEq)),
archive_attr(derive(Clone, Copy, 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, Debug),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Local;
Expand Down Expand Up @@ -538,4 +550,17 @@ mod tests {
// wrapping object
assert_eq!(rkyv::from_bytes::<Local>(&bytes).unwrap(), super::ArchivedLocal);
}

#[test]
#[cfg(feature = "rkyv-08-bytecheck")]
fn test_rkyv_bytecheck() {
let local = Local;
// Local is a ZST and serializes to 0 bytes
let bytes = rkyv_08::to_bytes::<rkyv_08::rancor::Error>(&local).unwrap();
assert_eq!(bytes.len(), 0);

// but is deserialized to an archived variant without a
// wrapping object
assert_eq!(rkyv_08::from_bytes::<Local, rkyv_08::rancor::Error>(&bytes).unwrap(), super::ArchivedLocal);
}
}
12 changes: 12 additions & 0 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
#[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::{FixedOffset, MappedLocalTime, Offset, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
#[cfg(feature = "now")]
Expand Down Expand Up @@ -47,6 +50,15 @@ use crate::{Date, DateTime};
archive(compare(PartialEq)),
archive_attr(derive(Clone, Copy, PartialEq, Eq, 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),
derive(Clone, Copy, PartialEq, Eq, Debug, Hash),
),
)]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(arbitrary::Arbitrary))]
pub struct Utc;
Expand Down
Loading

0 comments on commit e96444f

Please sign in to comment.