@@ -242,19 +242,35 @@ impl NaiveDate {
242242 pub ( crate ) fn weeks_from ( & self , day : Weekday ) -> i32 {
243243 ( self . ordinal ( ) as i32 - self . weekday ( ) . num_days_from ( day) as i32 + 6 ) / 7
244244 }
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.
252261 }
253262 }
254263
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+ }
258274 }
259275
260276 /// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -325,7 +341,7 @@ impl NaiveDate {
325341 #[ must_use]
326342 pub fn from_yo_opt ( year : i32 , ordinal : u32 ) -> Option < NaiveDate > {
327343 let flags = YearFlags :: from_year ( year) ;
328- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
344+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
329345 }
330346
331347 /// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -394,20 +410,21 @@ impl NaiveDate {
394410 if weekord <= delta {
395411 // ordinal < 1, previous year
396412 let prevflags = YearFlags :: from_year ( year - 1 ) ;
397- NaiveDate :: from_of (
413+ NaiveDate :: from_ordinal_and_flags (
398414 year - 1 ,
399- Of :: new ( weekord + prevflags. ndays ( ) - delta, prevflags) ?,
415+ weekord + prevflags. ndays ( ) - delta,
416+ prevflags,
400417 )
401418 } else {
402419 let ordinal = weekord - delta;
403420 let ndays = flags. ndays ( ) ;
404421 if ordinal <= ndays {
405422 // this year
406- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
423+ NaiveDate :: from_ordinal_and_flags ( year, ordinal, flags)
407424 } else {
408425 // ordinal > ndays, next year
409426 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)
411428 }
412429 }
413430 } else {
@@ -452,7 +469,7 @@ impl NaiveDate {
452469 let ( year_div_400, cycle) = div_mod_floor ( days, 146_097 ) ;
453470 let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
454471 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)
456473 }
457474
458475 /// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1038,7 +1055,7 @@ impl NaiveDate {
10381055
10391056 let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
10401057 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)
10421059 }
10431060
10441061 /// Subtracts the `days` part of given `Duration` from the current date.
@@ -1070,7 +1087,7 @@ impl NaiveDate {
10701087
10711088 let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
10721089 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)
10741091 }
10751092
10761093 /// Subtracts another `NaiveDate` from the current date.
0 commit comments