@@ -242,19 +242,35 @@ impl NaiveDate {
242
242
pub ( crate ) fn weeks_from ( & self , day : Weekday ) -> i32 {
243
243
( self . ordinal ( ) as i32 - self . weekday ( ) . num_days_from ( day) as i32 + 6 ) / 7
244
244
}
245
- /// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
246
- fn from_of ( year : i32 , of : Of ) -> Option < NaiveDate > {
247
- if ( MIN_YEAR ..=MAX_YEAR ) . contains ( & year) {
248
- let of = of. inner ( ) ;
249
- Some ( NaiveDate { ymdf : ( year << 13 ) | ( of as DateImpl ) } )
250
- } else {
251
- None
245
+
246
+ /// Makes a new `NaiveDate` from year, ordinal and flags.
247
+ /// Does not check whether the flags are correct for the provided year.
248
+ const fn from_ordinal_and_flags (
249
+ year : i32 ,
250
+ ordinal : u32 ,
251
+ flags : YearFlags ,
252
+ ) -> Option < NaiveDate > {
253
+ if year < MIN_YEAR || year > MAX_YEAR {
254
+ return None ; // Out-of-range
255
+ }
256
+ // Enable debug check once the MSRV >= 1.57 (panicking in const feature)
257
+ // debug_assert!(YearFlags::from_year(year).0 == flags.0);
258
+ match Of :: new ( ordinal, flags) {
259
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
260
+ None => None , // Invalid: Ordinal outside of the nr of days in a year with those flags.
252
261
}
253
262
}
254
263
255
- /// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
256
- fn from_mdf ( year : i32 , mdf : Mdf ) -> Option < NaiveDate > {
257
- NaiveDate :: from_of ( year, mdf. to_of ( ) ?)
264
+ /// Makes a new `NaiveDate` from year and packed month-day-flags.
265
+ /// Does not check whether the flags are correct for the provided year.
266
+ const fn from_mdf ( year : i32 , mdf : Mdf ) -> Option < NaiveDate > {
267
+ if year < MIN_YEAR || year > MAX_YEAR {
268
+ return None ; // Out-of-range
269
+ }
270
+ match mdf. to_of ( ) {
271
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
272
+ None => None , // Non-existing date
273
+ }
258
274
}
259
275
260
276
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -325,7 +341,7 @@ impl NaiveDate {
325
341
#[ must_use]
326
342
pub fn from_yo_opt ( year : i32 , ordinal : u32 ) -> Option < NaiveDate > {
327
343
let flags = YearFlags :: from_year ( year) ;
328
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
344
+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
329
345
}
330
346
331
347
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -394,20 +410,21 @@ impl NaiveDate {
394
410
if weekord <= delta {
395
411
// ordinal < 1, previous year
396
412
let prevflags = YearFlags :: from_year ( year - 1 ) ;
397
- NaiveDate :: from_of (
413
+ NaiveDate :: from_ordinal_and_flags (
398
414
year - 1 ,
399
- Of :: new ( weekord + prevflags. ndays ( ) - delta, prevflags) ?,
415
+ weekord + prevflags. ndays ( ) - delta,
416
+ prevflags,
400
417
)
401
418
} else {
402
419
let ordinal = weekord - delta;
403
420
let ndays = flags. ndays ( ) ;
404
421
if ordinal <= ndays {
405
422
// this year
406
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
423
+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
407
424
} else {
408
425
// ordinal > ndays, next year
409
426
let nextflags = YearFlags :: from_year ( year + 1 ) ;
410
- NaiveDate :: from_of ( year + 1 , Of :: new ( ordinal - ndays, nextflags) ? )
427
+ NaiveDate :: from_ordinal_and_flags ( year + 1 , ordinal - ndays, nextflags)
411
428
}
412
429
}
413
430
} else {
@@ -452,7 +469,7 @@ impl NaiveDate {
452
469
let ( year_div_400, cycle) = div_mod_floor ( days, 146_097 ) ;
453
470
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
454
471
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
455
- NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , Of :: new ( ordinal, flags) ? )
472
+ NaiveDate :: from_ordinal_and_flags ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
456
473
}
457
474
458
475
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1038,7 +1055,7 @@ impl NaiveDate {
1038
1055
1039
1056
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1040
1057
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
1041
- NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , Of :: new ( ordinal, flags) ? )
1058
+ NaiveDate :: from_ordinal_and_flags ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
1042
1059
}
1043
1060
1044
1061
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1070,7 +1087,7 @@ impl NaiveDate {
1070
1087
1071
1088
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1072
1089
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
1073
- NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , Of :: new ( ordinal, flags) ? )
1090
+ NaiveDate :: from_ordinal_and_flags ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
1074
1091
}
1075
1092
1076
1093
/// Subtracts another `NaiveDate` from the current date.
0 commit comments