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 85a0fac commit f4012de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 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,8 +15,9 @@

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

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

#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
Expand All @@ -25,18 +26,18 @@ use rkyv::{Archive, Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
#[allow(unreachable_pub)] // must be pub for rkyv
pub struct DateImpl(i32);
pub 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

0 comments on commit f4012de

Please sign in to comment.