@@ -252,19 +252,35 @@ impl NaiveDate {
252
252
pub ( crate ) fn weeks_from ( & self , day : Weekday ) -> i32 {
253
253
( self . ordinal ( ) as i32 - self . weekday ( ) . num_days_from ( day) as i32 + 6 ) / 7
254
254
}
255
- /// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
256
- fn from_of ( year : i32 , of : Of ) -> Option < NaiveDate > {
257
- if ( MIN_YEAR ..=MAX_YEAR ) . contains ( & year) {
258
- let of = of. inner ( ) ;
259
- Some ( NaiveDate { ymdf : ( year << 13 ) | ( of as DateImpl ) } )
260
- } else {
261
- None
255
+
256
+ /// Makes a new `NaiveDate` from year, ordinal and flags.
257
+ /// Does not check whether the flags are correct for the provided year.
258
+ const fn from_ordinal_and_flags (
259
+ year : i32 ,
260
+ ordinal : u32 ,
261
+ flags : YearFlags ,
262
+ ) -> Option < NaiveDate > {
263
+ if year < MIN_YEAR || year > MAX_YEAR {
264
+ return None ; // Out-of-range
265
+ }
266
+ // Enable debug check once the MSRV >= 1.57 (panicking in const feature)
267
+ // debug_assert!(YearFlags::from_year(year).0 == flags.0);
268
+ match Of :: new ( ordinal, flags) {
269
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
270
+ None => None , // Invalid: Ordinal outside of the nr of days in a year with those flags.
262
271
}
263
272
}
264
273
265
- /// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
266
- fn from_mdf ( year : i32 , mdf : Mdf ) -> Option < NaiveDate > {
267
- NaiveDate :: from_of ( year, mdf. to_of ( ) ?)
274
+ /// Makes a new `NaiveDate` from year and packed month-day-flags.
275
+ /// Does not check whether the flags are correct for the provided year.
276
+ const fn from_mdf ( year : i32 , mdf : Mdf ) -> Option < NaiveDate > {
277
+ if year < MIN_YEAR || year > MAX_YEAR {
278
+ return None ; // Out-of-range
279
+ }
280
+ match mdf. to_of ( ) {
281
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
282
+ None => None , // Non-existing date
283
+ }
268
284
}
269
285
270
286
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -335,7 +351,7 @@ impl NaiveDate {
335
351
#[ must_use]
336
352
pub fn from_yo_opt ( year : i32 , ordinal : u32 ) -> Option < NaiveDate > {
337
353
let flags = YearFlags :: from_year ( year) ;
338
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
354
+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
339
355
}
340
356
341
357
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -404,20 +420,21 @@ impl NaiveDate {
404
420
if weekord <= delta {
405
421
// ordinal < 1, previous year
406
422
let prevflags = YearFlags :: from_year ( year - 1 ) ;
407
- NaiveDate :: from_of (
423
+ NaiveDate :: from_ordinal_and_flags (
408
424
year - 1 ,
409
- Of :: new ( weekord + prevflags. ndays ( ) - delta, prevflags) ?,
425
+ weekord + prevflags. ndays ( ) - delta,
426
+ prevflags,
410
427
)
411
428
} else {
412
429
let ordinal = weekord - delta;
413
430
let ndays = flags. ndays ( ) ;
414
431
if ordinal <= ndays {
415
432
// this year
416
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
433
+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
417
434
} else {
418
435
// ordinal > ndays, next year
419
436
let nextflags = YearFlags :: from_year ( year + 1 ) ;
420
- NaiveDate :: from_of ( year + 1 , Of :: new ( ordinal - ndays, nextflags) ? )
437
+ NaiveDate :: from_ordinal_and_flags ( year + 1 , ordinal - ndays, nextflags)
421
438
}
422
439
}
423
440
} else {
@@ -462,7 +479,7 @@ impl NaiveDate {
462
479
let ( year_div_400, cycle) = div_mod_floor ( days, 146_097 ) ;
463
480
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
464
481
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
465
- NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , Of :: new ( ordinal, flags) ? )
482
+ NaiveDate :: from_ordinal_and_flags ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
466
483
}
467
484
468
485
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -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 the `days` part of given `Duration` from the current date.
@@ -1102,7 +1119,7 @@ impl NaiveDate {
1102
1119
1103
1120
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1104
1121
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
1105
- NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , Of :: new ( ordinal, flags) ? )
1122
+ NaiveDate :: from_ordinal_and_flags ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
1106
1123
}
1107
1124
1108
1125
/// Subtracts another `NaiveDate` from the current date.
0 commit comments