@@ -242,19 +242,29 @@ 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) && of. valid ( ) {
248
- let Of ( of) = of;
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_of ( year : i32 , ordinal : u32 , flags : YearFlags ) -> Option < NaiveDate > {
249
+ if year < MIN_YEAR || year > MAX_YEAR {
250
+ return None ; // Out-of-range
251
+ }
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.
252
255
}
253
256
}
254
257
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 ( ) )
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
+ }
258
268
}
259
269
260
270
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -325,7 +335,7 @@ impl NaiveDate {
325
335
#[ must_use]
326
336
pub fn from_yo_opt ( year : i32 , ordinal : u32 ) -> Option < NaiveDate > {
327
337
let flags = YearFlags :: from_year ( year) ;
328
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
338
+ NaiveDate :: from_of ( year, ordinal, flags)
329
339
}
330
340
331
341
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -394,20 +404,17 @@ impl NaiveDate {
394
404
if weekord <= delta {
395
405
// ordinal < 1, previous year
396
406
let prevflags = YearFlags :: from_year ( year - 1 ) ;
397
- NaiveDate :: from_of (
398
- year - 1 ,
399
- Of :: new ( weekord + prevflags. ndays ( ) - delta, prevflags) ?,
400
- )
407
+ NaiveDate :: from_of ( year - 1 , weekord + prevflags. ndays ( ) - delta, prevflags)
401
408
} else {
402
409
let ordinal = weekord - delta;
403
410
let ndays = flags. ndays ( ) ;
404
411
if ordinal <= ndays {
405
412
// this year
406
- NaiveDate :: from_of ( year, Of :: new ( ordinal, flags) ? )
413
+ NaiveDate :: from_of ( year, ordinal, flags)
407
414
} else {
408
415
// ordinal > ndays, next year
409
416
let nextflags = YearFlags :: from_year ( year + 1 ) ;
410
- NaiveDate :: from_of ( year + 1 , Of :: new ( ordinal - ndays, nextflags) ? )
417
+ NaiveDate :: from_of ( year + 1 , ordinal - ndays, nextflags)
411
418
}
412
419
}
413
420
} else {
@@ -452,7 +459,7 @@ impl NaiveDate {
452
459
let ( year_div_400, cycle) = div_mod_floor ( days, 146_097 ) ;
453
460
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
454
461
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) ? )
462
+ NaiveDate :: from_of ( year_div_400 * 400 + year_mod_400 as i32 , ordinal, flags)
456
463
}
457
464
458
465
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -925,28 +932,24 @@ impl NaiveDate {
925
932
/// Returns the packed ordinal-flags.
926
933
#[ inline]
927
934
const fn of ( & self ) -> Of {
928
- Of ( ( self . ymdf & 0b1_1111_1111_1111 ) as u32 )
935
+ Of :: from_date_impl ( self . ymdf )
929
936
}
930
937
931
938
/// Makes a new `NaiveDate` with the packed month-day-flags changed.
932
939
///
933
940
/// Returns `None` when the resulting `NaiveDate` would be invalid.
934
941
#[ inline]
935
942
fn with_mdf ( & self , mdf : Mdf ) -> Option < NaiveDate > {
936
- self . with_of ( mdf. to_of ( ) )
943
+ Some ( self . with_of ( mdf. to_of ( ) ? ) )
937
944
}
938
945
939
946
/// Makes a new `NaiveDate` with the packed ordinal-flags changed.
940
947
///
941
948
/// Returns `None` when the resulting `NaiveDate` would be invalid.
949
+ /// Does not check if the year flags match the year.
942
950
#[ inline]
943
- fn with_of ( & self , of : Of ) -> Option < NaiveDate > {
944
- if of. valid ( ) {
945
- let Of ( of) = of;
946
- Some ( NaiveDate { ymdf : ( self . ymdf & !0b1_1111_1111_1111 ) | of as DateImpl } )
947
- } else {
948
- None
949
- }
951
+ const fn with_of ( & self , of : Of ) -> NaiveDate {
952
+ NaiveDate { ymdf : ( self . ymdf & !0b1_1111_1111_1111 ) | of. inner ( ) as DateImpl }
950
953
}
951
954
952
955
/// Makes a new `NaiveDate` for the next calendar date.
@@ -975,7 +978,10 @@ impl NaiveDate {
975
978
#[ inline]
976
979
#[ must_use]
977
980
pub fn succ_opt ( & self ) -> Option < NaiveDate > {
978
- self . with_of ( self . of ( ) . succ ( ) ) . or_else ( || NaiveDate :: from_ymd_opt ( self . year ( ) + 1 , 1 , 1 ) )
981
+ match self . of ( ) . succ ( ) {
982
+ Some ( of) => Some ( self . with_of ( of) ) ,
983
+ None => NaiveDate :: from_ymd_opt ( self . year ( ) + 1 , 1 , 1 ) ,
984
+ }
979
985
}
980
986
981
987
/// Makes a new `NaiveDate` for the previous calendar date.
@@ -1004,7 +1010,10 @@ impl NaiveDate {
1004
1010
#[ inline]
1005
1011
#[ must_use]
1006
1012
pub fn pred_opt ( & self ) -> Option < NaiveDate > {
1007
- self . with_of ( self . of ( ) . pred ( ) ) . or_else ( || NaiveDate :: from_ymd_opt ( self . year ( ) - 1 , 12 , 31 ) )
1013
+ match self . of ( ) . pred ( ) {
1014
+ Some ( of) => Some ( self . with_of ( of) ) ,
1015
+ None => NaiveDate :: from_ymd_opt ( self . year ( ) - 1 , 12 , 31 ) ,
1016
+ }
1008
1017
}
1009
1018
1010
1019
/// Adds the `days` part of given `Duration` to the current date.
@@ -1036,7 +1045,7 @@ impl NaiveDate {
1036
1045
1037
1046
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1038
1047
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
1039
- 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)
1040
1049
}
1041
1050
1042
1051
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1068,7 +1077,7 @@ impl NaiveDate {
1068
1077
1069
1078
let ( year_mod_400, ordinal) = internals:: cycle_to_yo ( cycle as u32 ) ;
1070
1079
let flags = YearFlags :: from_year_mod_400 ( year_mod_400 as i32 ) ;
1071
- 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)
1072
1081
}
1073
1082
1074
1083
/// Subtracts another `NaiveDate` from the current date.
@@ -1623,7 +1632,7 @@ impl Datelike for NaiveDate {
1623
1632
/// ```
1624
1633
#[ inline]
1625
1634
fn with_ordinal ( & self , ordinal : u32 ) -> Option < NaiveDate > {
1626
- self . with_of ( self . of ( ) . with_ordinal ( ordinal) ? )
1635
+ self . of ( ) . with_ordinal ( ordinal) . map ( |of| self . with_of ( of ) )
1627
1636
}
1628
1637
1629
1638
/// Makes a new `NaiveDate` with the day of year (starting from 0) changed.
@@ -1648,7 +1657,7 @@ impl Datelike for NaiveDate {
1648
1657
#[ inline]
1649
1658
fn with_ordinal0 ( & self , ordinal0 : u32 ) -> Option < NaiveDate > {
1650
1659
let ordinal = ordinal0. checked_add ( 1 ) ?;
1651
- self . with_of ( self . of ( ) . with_ordinal ( ordinal) ? )
1660
+ self . with_ordinal ( ordinal)
1652
1661
}
1653
1662
}
1654
1663
0 commit comments