@@ -241,19 +241,30 @@ 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_of ( year : i32 , ordinal : u32 , flags : YearFlags ) -> Option < NaiveDate > {
248
+ if year < MIN_YEAR || year > MAX_YEAR {
249
+ return None ; // Out-of-range
250
+ }
251
+ debug_assert ! ( YearFlags :: from_year( year) . 0 == flags. 0 ) ;
252
+ match Of :: new ( ordinal, flags) {
253
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
254
+ None => None , // Invalid: Ordinal outside of the nr of days in a year with those flags.
251
255
}
252
256
}
253
257
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 ( ) ?)
258
+ /// Makes a new `NaiveDate` from year and packed month-day-flags.
259
+ /// Does not check whether the flags are correct for the provided year.
260
+ const fn from_mdf ( year : i32 , mdf : Mdf ) -> Option < NaiveDate > {
261
+ if year < MIN_YEAR || year > MAX_YEAR {
262
+ return None ; // Out-of-range
263
+ }
264
+ match mdf. to_of ( ) {
265
+ Some ( of) => Some ( NaiveDate { ymdf : ( year << 13 ) | ( of. inner ( ) as DateImpl ) } ) ,
266
+ None => None , // Non-existing date
267
+ }
257
268
}
258
269
259
270
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -324,7 +335,7 @@ impl NaiveDate {
324
335
#[ must_use]
325
336
pub fn from_yo_opt ( year : i32 , ordinal : u32 ) -> Option < NaiveDate > {
326
337
let flags = YearFlags :: from_year ( year) ;
327
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
338
+ NaiveDate :: from_of ( year, ordinal, flags)
328
339
}
329
340
330
341
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -393,20 +404,17 @@ impl NaiveDate {
393
404
if weekord <= delta {
394
405
// ordinal < 1, previous year
395
406
let prevflags = YearFlags :: from_year ( year - 1 ) ;
396
- NaiveDate :: from_of (
397
- year - 1 ,
398
- Of :: new ( weekord + prevflags. ndays ( ) - delta, prevflags) ?,
399
- )
407
+ NaiveDate :: from_of ( year - 1 , weekord + prevflags. ndays ( ) - delta, prevflags)
400
408
} else {
401
409
let ordinal = weekord - delta;
402
410
let ndays = flags. ndays ( ) ;
403
411
if ordinal <= ndays {
404
412
// this year
405
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
413
+ NaiveDate :: from_of ( year, ordinal, flags)
406
414
} else {
407
415
// ordinal > ndays, next year
408
416
let nextflags = YearFlags :: from_year ( year + 1 ) ;
409
- NaiveDate :: from_of ( year + 1 , Of :: new ( ordinal - ndays, nextflags) ? )
417
+ NaiveDate :: from_of ( year + 1 , ordinal - ndays, nextflags)
410
418
}
411
419
}
412
420
} else {
@@ -451,7 +459,7 @@ impl NaiveDate {
451
459
let ( year_div_400, cycle) = div_mod_floor ( days, 146_097 ) ;
452
460
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
453
461
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) ? )
462
+ NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
455
463
}
456
464
457
465
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1037,7 +1045,7 @@ impl NaiveDate {
1037
1045
1038
1046
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1039
1047
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) ? )
1048
+ NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
1041
1049
}
1042
1050
1043
1051
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1069,7 +1077,7 @@ impl NaiveDate {
1069
1077
1070
1078
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1071
1079
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) ? )
1080
+ NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
1073
1081
}
1074
1082
1075
1083
/// Subtracts another `NaiveDate` from the current date.
0 commit comments