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