@@ -60,14 +60,14 @@ impl NaiveWeek {
6060 /// ```
6161 #[ inline]
6262 #[ must_use]
63- pub fn first_day ( & self ) -> NaiveDate {
63+ pub const fn first_day ( & self ) -> NaiveDate {
6464 let start = self . start . num_days_from_monday ( ) as i32 ;
6565 let ref_day = self . date . weekday ( ) . num_days_from_monday ( ) as i32 ;
6666 // Calculate the number of days to subtract from `self.date`.
6767 // Do not construct an intermediate date beyond `self.date`, because that may be out of
6868 // range if `date` is close to `NaiveDate::MAX`.
6969 let days = start - ref_day - if start > ref_day { 7 } else { 0 } ;
70- self . date . add_days ( days) . unwrap ( )
70+ expect ! ( self . date. add_days( days) , "first weekday out of range for `NaiveDate`" )
7171 }
7272
7373 /// Returns a date representing the last day of the week.
@@ -88,14 +88,14 @@ impl NaiveWeek {
8888 /// ```
8989 #[ inline]
9090 #[ must_use]
91- pub fn last_day ( & self ) -> NaiveDate {
91+ pub const fn last_day ( & self ) -> NaiveDate {
9292 let end = self . start . pred ( ) . num_days_from_monday ( ) as i32 ;
9393 let ref_day = self . date . weekday ( ) . num_days_from_monday ( ) as i32 ;
9494 // Calculate the number of days to add to `self.date`.
9595 // Do not construct an intermediate date before `self.date` (like with `first_day()`),
9696 // because that may be out of range if `date` is close to `NaiveDate::MIN`.
9797 let days = end - ref_day + if end < ref_day { 7 } else { 0 } ;
98- self . date . add_days ( days) . unwrap ( )
98+ expect ! ( self . date. add_days( days) , "last weekday out of range for `NaiveDate`" )
9999 }
100100
101101 /// Returns a [`RangeInclusive<T>`] representing the whole week bounded by
@@ -118,7 +118,7 @@ impl NaiveWeek {
118118 /// ```
119119 #[ inline]
120120 #[ must_use]
121- pub fn days ( & self ) -> RangeInclusive < NaiveDate > {
121+ pub const fn days ( & self ) -> RangeInclusive < NaiveDate > {
122122 self . first_day ( ) ..=self . last_day ( )
123123 }
124124}
@@ -206,7 +206,7 @@ pub const MIN_DATE: NaiveDate = NaiveDate::MIN;
206206#[ deprecated( since = "0.4.20" , note = "Use NaiveDate::MAX instead" ) ]
207207pub const MAX_DATE : NaiveDate = NaiveDate :: MAX ;
208208
209- #[ cfg( feature = "arbitrary" ) ]
209+ #[ cfg( all ( feature = "arbitrary" , feature = "std" ) ) ]
210210impl arbitrary:: Arbitrary < ' _ > for NaiveDate {
211211 fn arbitrary ( u : & mut arbitrary:: Unstructured ) -> arbitrary:: Result < NaiveDate > {
212212 let year = u. int_in_range ( MIN_YEAR ..=MAX_YEAR ) ?;
@@ -1154,9 +1154,12 @@ impl NaiveDate {
11541154 /// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1)), None);
11551155 /// ```
11561156 #[ must_use]
1157- pub fn checked_add_signed ( self , rhs : TimeDelta ) -> Option < NaiveDate > {
1158- let days = i32:: try_from ( rhs. num_days ( ) ) . ok ( ) ?;
1159- self . add_days ( days)
1157+ pub const fn checked_add_signed ( self , rhs : TimeDelta ) -> Option < NaiveDate > {
1158+ let days = rhs. num_days ( ) ;
1159+ if days < i32:: MIN as i64 || days > i32:: MAX as i64 {
1160+ return None ;
1161+ }
1162+ self . add_days ( days as i32 )
11601163 }
11611164
11621165 /// Subtracts the number of whole days in the given `TimeDelta` from the current date.
@@ -1180,9 +1183,12 @@ impl NaiveDate {
11801183 /// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1)), None);
11811184 /// ```
11821185 #[ must_use]
1183- pub fn checked_sub_signed ( self , rhs : TimeDelta ) -> Option < NaiveDate > {
1184- let days = i32:: try_from ( -rhs. num_days ( ) ) . ok ( ) ?;
1185- self . add_days ( days)
1186+ pub const fn checked_sub_signed ( self , rhs : TimeDelta ) -> Option < NaiveDate > {
1187+ let days = -rhs. num_days ( ) ;
1188+ if days < i32:: MIN as i64 || days > i32:: MAX as i64 {
1189+ return None ;
1190+ }
1191+ self . add_days ( days as i32 )
11861192 }
11871193
11881194 /// Subtracts another `NaiveDate` from the current date.
@@ -1208,7 +1214,7 @@ impl NaiveDate {
12081214 /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), TimeDelta::days(365*400 + 97));
12091215 /// ```
12101216 #[ must_use]
1211- pub fn signed_duration_since ( self , rhs : NaiveDate ) -> TimeDelta {
1217+ pub const fn signed_duration_since ( self , rhs : NaiveDate ) -> TimeDelta {
12121218 let year1 = self . year ( ) ;
12131219 let year2 = rhs. year ( ) ;
12141220 let ( year1_div_400, year1_mod_400) = div_mod_floor ( year1, 400 ) ;
@@ -1553,7 +1559,7 @@ impl Datelike for NaiveDate {
15531559 /// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().day(), 14);
15541560 /// ```
15551561 ///
1556- /// Combined with [`NaiveDate::pred `](#method.pred ),
1562+ /// Combined with [`NaiveDate::pred_opt `](#method.pred_opt ),
15571563 /// one can determine the number of days in a particular month.
15581564 /// (Note that this panics when `year` is out of range.)
15591565 ///
@@ -1610,7 +1616,7 @@ impl Datelike for NaiveDate {
16101616 /// assert_eq!(NaiveDate::from_ymd_opt(-308, 3, 14).unwrap().ordinal(), 74);
16111617 /// ```
16121618 ///
1613- /// Combined with [`NaiveDate::pred `](#method.pred ),
1619+ /// Combined with [`NaiveDate::pred_opt `](#method.pred_opt ),
16141620 /// one can determine the number of days in a particular year.
16151621 /// (Note that this panics when `year` is out of range.)
16161622 ///
0 commit comments