Skip to content

Commit 8799852

Browse files
committed
move more code into internals
fix mdf fix lints
1 parent 5e4c3ae commit 8799852

File tree

5 files changed

+267
-203
lines changed

5 files changed

+267
-203
lines changed

src/datetime/rustc_serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);
5858

5959
#[allow(deprecated)]
6060
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
61-
/// Pull the inner DateTime<Tz> out
61+
/// Pull the inner `DateTime<Tz>` out
6262
#[allow(deprecated)]
6363
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
6464
obj.0

src/month.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Month {
153153
}
154154

155155
impl num_traits::FromPrimitive for Month {
156-
/// Returns an Option<Month> from a i64, assuming a 1-index, January = 1.
156+
/// Returns an `Option<Month>` from a i64, assuming a 1-index, January = 1.
157157
///
158158
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
159159
/// ---------------------------| -------------------- | --------------------- | ... | -----

src/naive/date.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Days {
183183
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
184184
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
185185
pub struct NaiveDate {
186-
ymdf: DateImpl, // (year << 13) | of
186+
ymdf: DateImpl,
187187
}
188188

189189
/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
@@ -223,18 +223,13 @@ fn test_date_bounds() {
223223

224224
impl NaiveDate {
225225
/// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
226-
fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
227-
if (MIN_YEAR..=MAX_YEAR).contains(&year) && of.valid() {
228-
let Of(of) = of;
229-
Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
230-
} else {
231-
None
232-
}
226+
fn from_parts(year: i32, of: Of) -> Option<NaiveDate> {
227+
Some(NaiveDate { ymdf: DateImpl::from_parts(year, of)? })
233228
}
234229

235230
/// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
236231
fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
237-
NaiveDate::from_of(year, mdf.to_of())
232+
NaiveDate::from_parts(year, mdf.to_of()?)
238233
}
239234

240235
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -301,7 +296,7 @@ impl NaiveDate {
301296
/// ```
302297
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
303298
let flags = YearFlags::from_year(year);
304-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
299+
NaiveDate::from_parts(year, Of::new(ordinal, flags)?)
305300
}
306301

307302
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -368,7 +363,7 @@ impl NaiveDate {
368363
if weekord <= delta {
369364
// ordinal < 1, previous year
370365
let prevflags = YearFlags::from_year(year - 1);
371-
NaiveDate::from_of(
366+
NaiveDate::from_parts(
372367
year - 1,
373368
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
374369
)
@@ -377,11 +372,11 @@ impl NaiveDate {
377372
let ndays = flags.ndays();
378373
if ordinal <= ndays {
379374
// this year
380-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
375+
NaiveDate::from_parts(year, Of::new(ordinal, flags)?)
381376
} else {
382377
// ordinal > ndays, next year
383378
let nextflags = YearFlags::from_year(year + 1);
384-
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags)?)
379+
NaiveDate::from_parts(year + 1, Of::new(ordinal - ndays, nextflags)?)
385380
}
386381
}
387382
} else {
@@ -424,7 +419,7 @@ impl NaiveDate {
424419
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
425420
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
426421
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
427-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
422+
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
428423
}
429424

430425
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -870,28 +865,23 @@ impl NaiveDate {
870865
/// Returns the packed ordinal-flags.
871866
#[inline]
872867
fn of(&self) -> Of {
873-
Of((self.ymdf & 0b1_1111_1111_1111) as u32)
868+
self.ymdf.of()
874869
}
875870

876871
/// Makes a new `NaiveDate` with the packed month-day-flags changed.
877872
///
878873
/// Returns `None` when the resulting `NaiveDate` would be invalid.
879874
#[inline]
880875
fn with_mdf(&self, mdf: Mdf) -> Option<NaiveDate> {
881-
self.with_of(mdf.to_of())
876+
self.with_of(mdf.to_of()?)
882877
}
883878

884879
/// Makes a new `NaiveDate` with the packed ordinal-flags changed.
885880
///
886881
/// Returns `None` when the resulting `NaiveDate` would be invalid.
887882
#[inline]
888883
fn with_of(&self, of: Of) -> Option<NaiveDate> {
889-
if of.valid() {
890-
let Of(of) = of;
891-
Some(NaiveDate { ymdf: (self.ymdf & !0b1_1111_1111_1111) | of as DateImpl })
892-
} else {
893-
None
894-
}
884+
Some(NaiveDate { ymdf: DateImpl::from_parts(self.year(), of)? })
895885
}
896886

897887
/// Makes a new `NaiveDate` for the next calendar date.
@@ -918,7 +908,7 @@ impl NaiveDate {
918908
/// ```
919909
#[inline]
920910
pub fn succ_opt(&self) -> Option<NaiveDate> {
921-
self.with_of(self.of().succ()).or_else(|| NaiveDate::from_ymd_opt(self.year() + 1, 1, 1))
911+
Some(NaiveDate { ymdf: self.ymdf.succ_opt()? })
922912
}
923913

924914
/// Makes a new `NaiveDate` for the previous calendar date.
@@ -945,7 +935,7 @@ impl NaiveDate {
945935
/// ```
946936
#[inline]
947937
pub fn pred_opt(&self) -> Option<NaiveDate> {
948-
self.with_of(self.of().pred()).or_else(|| NaiveDate::from_ymd_opt(self.year() - 1, 12, 31))
938+
Some(NaiveDate { ymdf: self.ymdf.pred_opt()? })
949939
}
950940

951941
/// Adds the `days` part of given `Duration` to the current date.
@@ -976,7 +966,7 @@ impl NaiveDate {
976966

977967
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
978968
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
979-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
969+
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
980970
}
981971

982972
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1007,7 +997,7 @@ impl NaiveDate {
1007997

1008998
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
1009999
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1010-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1000+
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
10111001
}
10121002

10131003
/// Subtracts another `NaiveDate` from the current date.
@@ -1191,9 +1181,9 @@ impl NaiveDate {
11911181
}
11921182

11931183
/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
1194-
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
1184+
pub const MIN: NaiveDate = NaiveDate { ymdf: DateImpl::MIN };
11951185
/// The maximum possible `NaiveDate` (December 31, 262143 CE).
1196-
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ };
1186+
pub const MAX: NaiveDate = NaiveDate { ymdf: DateImpl::MAX };
11971187
}
11981188

11991189
impl Datelike for NaiveDate {
@@ -1209,7 +1199,7 @@ impl Datelike for NaiveDate {
12091199
/// ```
12101200
#[inline]
12111201
fn year(&self) -> i32 {
1212-
self.ymdf >> 13
1202+
self.ymdf.year()
12131203
}
12141204

12151205
/// Returns the month number starting from 1.
@@ -1376,7 +1366,7 @@ impl Datelike for NaiveDate {
13761366

13771367
#[inline]
13781368
fn iso_week(&self) -> IsoWeek {
1379-
isoweek::iso_week_from_yof(self.year(), self.of())
1369+
isoweek::iso_week_from_yof(self.year(), self.of()).unwrap()
13801370
}
13811371

13821372
/// Makes a new `NaiveDate` with the year number changed.

0 commit comments

Comments
 (0)