Remove Of::succ

This commit is contained in:
Paul Dicker 2023-08-10 16:51:31 +02:00 committed by Paul Dicker
parent 2e00a1ca0b
commit 1835cfcde2
2 changed files with 15 additions and 14 deletions

View File

@ -1098,9 +1098,10 @@ impl NaiveDate {
#[inline] #[inline]
#[must_use] #[must_use]
pub const fn succ_opt(&self) -> Option<NaiveDate> { pub const fn succ_opt(&self) -> Option<NaiveDate> {
match self.of().succ() { let new_ol = (self.yof & OL_MASK) + (1 << 4);
Some(of) => Some(self.with_of(of)), match new_ol <= MAX_OL {
None => NaiveDate::from_ymd_opt(self.year() + 1, 1, 1), true => Some(NaiveDate { yof: self.yof & !OL_MASK | new_ol }),
false => NaiveDate::from_yo_opt(self.year() + 1, 1),
} }
} }
@ -2309,6 +2310,17 @@ pub(super) const MAX_YEAR: i32 = (i32::MAX >> 13) - 1;
/// `NaiveDate::MIN` pushes it beyond the valid, representable range. /// `NaiveDate::MIN` pushes it beyond the valid, representable range.
pub(super) const MIN_YEAR: i32 = (i32::MIN >> 13) + 1; pub(super) const MIN_YEAR: i32 = (i32::MIN >> 13) + 1;
const ORDINAL_MASK: i32 = 0b1_1111_1111_0000;
const LEAP_YEAR_MASK: i32 = 0b1000;
// OL: ordinal and leap year flag.
// With only these parts of the date an ordinal 366 in a common year would be encoded as
// `((366 << 1) | 1) << 3`, and in a leap year as `((366 << 1) | 0) << 3`, which is less.
// This allows for efficiently checking the ordinal exists depending on whether this is a leap year.
const OL_MASK: i32 = ORDINAL_MASK | LEAP_YEAR_MASK;
const MAX_OL: i32 = 366 << 4;
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
fn test_encodable_json<F, E>(to_string: F) fn test_encodable_json<F, E>(to_string: F)
where where

View File

@ -346,13 +346,6 @@ impl Of {
Mdf::from_of(*self) Mdf::from_of(*self)
} }
/// Returns an `Of` with the next day, or `None` if this is the last day of the year.
#[inline]
pub(super) const fn succ(&self) -> Option<Of> {
let of = Of(self.0 + (1 << 4));
of.validate()
}
/// Returns an `Of` with the previous day, or `None` if this is the first day of the year. /// Returns an `Of` with the previous day, or `None` if this is the first day of the year.
#[inline] #[inline]
pub(super) const fn pred(&self) -> Option<Of> { pub(super) const fn pred(&self) -> Option<Of> {
@ -867,10 +860,6 @@ mod tests {
assert!(Of::from_mdf(Mdf::new(2, 29, leap_year).unwrap()).is_some()); assert!(Of::from_mdf(Mdf::new(2, 29, leap_year).unwrap()).is_some());
assert!(Of::from_mdf(Mdf::new(2, 28, regular_year).unwrap()).is_some()); assert!(Of::from_mdf(Mdf::new(2, 28, regular_year).unwrap()).is_some());
assert!(Of::new(365, regular_year).unwrap().succ().is_none());
assert!(Of::new(365, leap_year).unwrap().succ().is_some());
assert!(Of::new(366, leap_year).unwrap().succ().is_none());
assert!(Of::new(1, regular_year).unwrap().pred().is_none()); assert!(Of::new(1, regular_year).unwrap().pred().is_none());
assert!(Of::new(1, leap_year).unwrap().pred().is_none()); assert!(Of::new(1, leap_year).unwrap().pred().is_none());
} }