Skip to content

Commit

Permalink
Make DateImpl a newtype instead of a type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 28, 2023
1 parent 64514ad commit 67dbf23
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
20 changes: 12 additions & 8 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use crate::{Datelike, Duration, Weekday};
use super::internals::{self, DateImpl, Mdf, Of, YearFlags};
use super::isoweek;

const MAX_YEAR: i32 = internals::MAX_YEAR;
const MIN_YEAR: i32 = internals::MIN_YEAR;
const MAX_YEAR: i32 = internals::MAX_YEAR.get();
const MIN_YEAR: i32 = internals::MIN_YEAR.get();

/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first
/// day of the week.
Expand Down Expand Up @@ -229,7 +229,7 @@ impl NaiveDate {
}
debug_assert!(YearFlags::from_year(year).0 == flags.0);
match Of::new(ordinal, flags) {
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
Some(of) => Some(NaiveDate { ymdf: DateImpl::new((year << 13) | of.inner() as i32) }),
None => None, // Invalid: Ordinal outside of the nr of days in a year with those flags.
}
}
Expand All @@ -241,7 +241,7 @@ impl NaiveDate {
return None; // Out-of-range
}
match mdf.to_of() {
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
Some(of) => Some(NaiveDate { ymdf: DateImpl::new((year << 13) | of.inner() as i32) }),
None => None, // Non-existing date
}
}
Expand Down Expand Up @@ -1027,7 +1027,9 @@ impl NaiveDate {
/// Does not check if the year flags match the year.
#[inline]
const fn with_of(&self, of: Of) -> NaiveDate {
NaiveDate { ymdf: (self.ymdf & !0b1_1111_1111_1111) | of.inner() as DateImpl }
NaiveDate {
ymdf: DateImpl::new((self.ymdf.get() & !0b1_1111_1111_1111) | of.inner() as i32),
}
}

/// Makes a new `NaiveDate` for the next calendar date.
Expand Down Expand Up @@ -1405,9 +1407,11 @@ impl NaiveDate {
}

/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
pub const MIN: NaiveDate =
NaiveDate { ymdf: DateImpl::new((MIN_YEAR << 13) | (1 << 4) | 0o07) /*FE*/ };
/// The maximum possible `NaiveDate` (December 31, 262143 CE).
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ };
pub const MAX: NaiveDate =
NaiveDate { ymdf: DateImpl::new((MAX_YEAR << 13) | (365 << 4) | 0o17) /*F*/ };
}

impl Datelike for NaiveDate {
Expand All @@ -1423,7 +1427,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn year(&self) -> i32 {
self.ymdf >> 13
self.ymdf.get() >> 13
}

/// Returns the month number starting from 1.
Expand Down
19 changes: 15 additions & 4 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ use crate::Weekday;
use core::fmt;

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

pub(super) const MAX_YEAR: DateImpl = i32::MAX >> 13;
pub(super) const MIN_YEAR: DateImpl = i32::MIN >> 13;
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)
}

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

/// The year flags (aka the dominical letter).
///
Expand Down Expand Up @@ -280,7 +291,7 @@ impl Of {

pub(super) const fn from_date_impl(date_impl: DateImpl) -> Of {
// We assume the value in the `DateImpl` is valid.
Of((date_impl & 0b1_1111_1111_1111) as u32)
Of((date_impl.get() & 0b1_1111_1111_1111) as u32)
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(super) fn iso_week_from_yof(year: i32, of: Of) -> IsoWeek {
}
};
let flags = YearFlags::from_year(year);
IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(flags.0) }
IsoWeek { ywf: DateImpl::new((year << 10) | (week << 4) as i32 | flags.0 as i32) }
}

impl IsoWeek {
Expand All @@ -73,7 +73,7 @@ impl IsoWeek {
/// ```
#[inline]
pub const fn year(&self) -> i32 {
self.ywf >> 10
self.ywf.get() >> 10
}

/// Returns the ISO week number starting from 1.
Expand All @@ -90,7 +90,7 @@ impl IsoWeek {
/// ```
#[inline]
pub const fn week(&self) -> u32 {
((self.ywf >> 4) & 0x3f) as u32
((self.ywf.get() >> 4) & 0x3f) as u32
}

/// Returns the ISO week number starting from 0.
Expand All @@ -107,7 +107,7 @@ impl IsoWeek {
/// ```
#[inline]
pub const fn week0(&self) -> u32 {
((self.ywf >> 4) & 0x3f) as u32 - 1
((self.ywf.get() >> 4) & 0x3f) as u32 - 1
}
}

Expand Down

0 comments on commit 67dbf23

Please sign in to comment.