Skip to content

Commit

Permalink
Use NonZeroI32 to store a NaiveDate
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 28, 2023
1 parent 67dbf23 commit 92dc230
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,20 @@ mod tests {
fn test_type_sizes() {
use core::mem::size_of;
assert_eq!(size_of::<NaiveDate>(), 4);
assert_eq!(size_of::<Option<NaiveDate>>(), 8);
assert_eq!(size_of::<Option<NaiveDate>>(), 4);
assert_eq!(size_of::<NaiveTime>(), 8);
assert_eq!(size_of::<Option<NaiveTime>>(), 12);
assert_eq!(size_of::<NaiveDateTime>(), 12);
assert_eq!(size_of::<Option<NaiveDateTime>>(), 16);
assert_eq!(size_of::<Option<NaiveDateTime>>(), 12);

assert_eq!(size_of::<DateTime<Utc>>(), 12);
assert_eq!(size_of::<DateTime<FixedOffset>>(), 16);
assert_eq!(size_of::<DateTime<Local>>(), 16);
assert_eq!(size_of::<Option<DateTime<FixedOffset>>>(), 20);
assert_eq!(size_of::<Option<DateTime<FixedOffset>>>(), 16);

assert_eq!(size_of::<Date<Utc>>(), 4);
assert_eq!(size_of::<Option<Date<Utc>>>(), 8);
assert_eq!(size_of::<Option<Date<Utc>>>(), 4);
assert_eq!(size_of::<Date<FixedOffset>>(), 8);
assert_eq!(size_of::<Option<Date<FixedOffset>>>(), 12);
assert_eq!(size_of::<Option<Date<FixedOffset>>>(), 8);
}
}
9 changes: 5 additions & 4 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@

#![cfg_attr(feature = "__internal_bench", allow(missing_docs))]

use crate::Weekday;
use crate::{expect, Weekday};
use core::fmt;
use core::num::NonZeroI32;

/// The internal date representation: `year << 13 | Of`
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(super) struct DateImpl(i32);
pub(super) struct DateImpl(NonZeroI32);

pub(super) const MAX_YEAR: DateImpl = DateImpl::new(i32::MAX >> 13);
pub(super) const MIN_YEAR: DateImpl = DateImpl::new(i32::MIN >> 13);

impl DateImpl {
pub(super) const fn new(val: i32) -> Self {
DateImpl(val)
DateImpl(expect!(NonZeroI32::new(val), "invalid internal value"))
}

pub(super) const fn get(&self) -> i32 {
self.0
self.0.get()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ mod tests {
let minweek = NaiveDate::MIN.iso_week();
let maxweek = NaiveDate::MAX.iso_week();

assert_eq!(minweek.year(), internals::MIN_YEAR);
assert_eq!(minweek.year(), internals::MIN_YEAR.get());
assert_eq!(minweek.week(), 1);
assert_eq!(minweek.week0(), 0);
#[cfg(any(feature = "alloc", feature = "std"))]
assert_eq!(format!("{:?}", minweek), NaiveDate::MIN.format("%G-W%V").to_string());

assert_eq!(maxweek.year(), internals::MAX_YEAR + 1);
assert_eq!(maxweek.year(), internals::MAX_YEAR.get() + 1);
assert_eq!(maxweek.week(), 1);
assert_eq!(maxweek.week0(), 0);
#[cfg(any(feature = "alloc", feature = "std"))]
Expand Down

0 comments on commit 92dc230

Please sign in to comment.