@@ -557,89 +557,83 @@ impl NaiveDate {
557557/// 
558558/// # Errors 
559559/// 
560- /// Returns `None`  if the resulting date would be out of range. 
560+ /// Returns [`Error::OutOfRange`]  if the resulting date would be out of range. 
561561/// 
562562/// # Example 
563563/// 
564564/// ``` 
565- /// # use chrono::{NaiveDate, Days}; 
566- /// assert_eq!( 
567- ///     NaiveDate::from_ymd(2022, 2, 20).unwrap().checked_add_days(Days::new(9)), 
568- ///     Some(NaiveDate::from_ymd(2022, 3, 1).unwrap()) 
569- /// ); 
565+ /// # use chrono::{NaiveDate, Days, Error}; 
570566/// assert_eq!( 
571- ///     NaiveDate::from_ymd(2022, 7, 31).unwrap(). checked_add_days(Days::new(2 )), 
572- ///     Some( NaiveDate::from_ymd(2022, 8, 2).unwrap() ) 
567+ ///     NaiveDate::from_ymd(2022, 2, 20)?. checked_add_days(Days::new(9 )), 
568+ ///     NaiveDate::from_ymd(2022, 3, 1 ) 
573569/// ); 
574570/// assert_eq!( 
575- ///     NaiveDate::from_ymd(2022, 7, 31).unwrap() .checked_add_days(Days::new(1000000000000)), 
576- ///     None  
571+ ///     NaiveDate::from_ymd(2022, 7, 31)? .checked_add_days(Days::new(1000000000000)), 
572+ ///     Err(Error::OutOfRange)  
577573/// ); 
574+ /// # Ok::<(), Error>(()) 
578575/// ``` 
579- #[ must_use]  
580-     pub  const  fn  checked_add_days ( self ,  days :  Days )  -> Option < Self >  { 
576+ pub  const  fn  checked_add_days ( self ,  days :  Days )  -> Result < Self ,  Error >  { 
581577        match  days. 0  <= i32:: MAX  as  u64  { 
582578            true  => self . add_days ( days. 0  as  i32 ) , 
583-             false  => None , 
579+             false  => Err ( Error :: OutOfRange ) , 
584580        } 
585581    } 
586582
587583    /// Subtract a duration in [`Days`] from the date 
588584/// 
589585/// # Errors 
590586/// 
591- /// Returns `None`  if the resulting date would be out of range. 
587+ /// Returns [`Error::OutOfRange`]  if the resulting date would be out of range. 
592588/// 
593589/// # Example 
594590/// 
595591/// ``` 
596- /// # use chrono::{NaiveDate, Days}; 
592+ /// # use chrono::{NaiveDate, Days, Error }; 
597593/// assert_eq!( 
598- ///     NaiveDate::from_ymd(2022, 2, 20).unwrap() .checked_sub_days(Days::new(6)), 
599- ///     Some( NaiveDate::from_ymd(2022, 2, 14).unwrap() ) 
594+ ///     NaiveDate::from_ymd(2022, 2, 20)? .checked_sub_days(Days::new(6)), 
595+ ///     NaiveDate::from_ymd(2022, 2, 14) 
600596/// ); 
601597/// assert_eq!( 
602- ///     NaiveDate::from_ymd(2022, 2, 20).unwrap() .checked_sub_days(Days::new(1000000000000)), 
603- ///     None  
598+ ///     NaiveDate::from_ymd(2022, 2, 20)? .checked_sub_days(Days::new(1000000000000)), 
599+ ///     Err(Error::OutOfRange)  
604600/// ); 
601+ /// # Ok::<(), Error>(()) 
605602/// ``` 
606- #[ must_use]  
607-     pub  const  fn  checked_sub_days ( self ,  days :  Days )  -> Option < Self >  { 
603+ pub  const  fn  checked_sub_days ( self ,  days :  Days )  -> Result < Self ,  Error >  { 
608604        match  days. 0  <= i32:: MAX  as  u64  { 
609605            true  => self . add_days ( -( days. 0  as  i32 ) ) , 
610-             false  => None , 
606+             false  => Err ( Error :: OutOfRange ) , 
611607        } 
612608    } 
613609
614610    /// Add a duration of `i32` days to the date. 
615- pub ( crate )  const  fn  add_days ( self ,  days :  i32 )  -> Option < Self >  { 
611+ pub ( crate )  const  fn  add_days ( self ,  days :  i32 )  -> Result < Self ,   Error >  { 
616612        // Fast path if the result is within the same year. 
617613        // Also `DateTime::checked_(add|sub)_days` relies on this path, because if the value remains 
618614        // within the year it doesn't do a check if the year is in range. 
619615        // This way `DateTime:checked_(add|sub)_days(Days::new(0))` can be a no-op on dates were the 
620616        // local datetime is beyond `NaiveDate::{MIN, MAX}. 
621617        const  ORDINAL_MASK :  i32  = 0b1_1111_1111_0000 ; 
622-         if  let  Some ( ordinal)  = ( ( self . yof ( )  &  ORDINAL_MASK )  >> 4 ) . checked_add ( days)  { 
618+         let  ordinal =
619+             try_ok_or ! ( ( ( self . yof( )  &  ORDINAL_MASK )  >> 4 ) . checked_add( days) ,  Error :: OutOfRange ) ; 
620+         { 
623621            if  ordinal > 0  && ordinal <= ( 365  + self . leap_year ( )  as  i32 )  { 
624622                let  year_and_flags = self . yof ( )  &  !ORDINAL_MASK ; 
625-                 return  Some ( NaiveDate :: from_yof ( year_and_flags | ( ordinal << 4 ) ) ) ; 
623+                 return  Ok ( NaiveDate :: from_yof ( year_and_flags | ( ordinal << 4 ) ) ) ; 
626624            } 
627625        } 
628626        // do the full check 
629627        let  year = self . year ( ) ; 
630628        let  ( mut  year_div_400,  year_mod_400)  = div_mod_floor ( year,  400 ) ; 
631629        let  cycle = yo_to_cycle ( year_mod_400 as  u32 ,  self . ordinal ( ) ) ; 
632-         let  cycle = try_opt ! ( ( cycle as  i32 ) . checked_add( days) ) ; 
630+         let  cycle = try_ok_or ! ( ( cycle as  i32 ) . checked_add( days) ,   Error :: OutOfRange ) ; 
633631        let  ( cycle_div_400y,  cycle)  = div_mod_floor ( cycle,  146_097 ) ; 
634632        year_div_400 += cycle_div_400y; 
635633
636634        let  ( year_mod_400,  ordinal)  = cycle_to_yo ( cycle as  u32 ) ; 
637635        let  flags = YearFlags :: from_year_mod_400 ( year_mod_400 as  i32 ) ; 
638-         ok ! ( NaiveDate :: from_ordinal_and_flags( 
639-             year_div_400 *  400  + year_mod_400 as  i32 , 
640-             ordinal, 
641-             flags
642-         ) ) 
636+         NaiveDate :: from_ordinal_and_flags ( year_div_400 *  400  + year_mod_400 as  i32 ,  ordinal,  flags) 
643637    } 
644638
645639    /// Makes a new `NaiveDateTime` from the current date and given `NaiveTime`. 
@@ -903,7 +897,7 @@ impl NaiveDate {
903897        if  days < i32:: MIN  as  i64  || days > i32:: MAX  as  i64  { 
904898            return  None ; 
905899        } 
906-         self . add_days ( days as  i32 ) 
900+         ok ! ( self . add_days( days as  i32 ) ) 
907901    } 
908902
909903    /// Subtracts the number of whole days in the given `TimeDelta` from the current date. 
@@ -936,7 +930,7 @@ impl NaiveDate {
936930        if  days < i32:: MIN  as  i64  || days > i32:: MAX  as  i64  { 
937931            return  None ; 
938932        } 
939-         self . add_days ( days as  i32 ) 
933+         ok ! ( self . add_days( days as  i32 ) ) 
940934    } 
941935
942936    /// Subtracts another `NaiveDate` from the current date. 
@@ -1995,7 +1989,7 @@ impl Iterator for NaiveDateWeeksIterator {
19951989
19961990    fn  next ( & mut  self )  -> Option < Self :: Item >  { 
19971991        let  current = self . value ; 
1998-         self . value  = current. checked_add_days ( Days :: new ( 7 ) ) ?; 
1992+         self . value  = current. checked_add_days ( Days :: new ( 7 ) ) . ok ( ) ?; 
19991993        Some ( current) 
20001994    } 
20011995
@@ -2010,7 +2004,7 @@ impl ExactSizeIterator for NaiveDateWeeksIterator {}
20102004impl  DoubleEndedIterator  for  NaiveDateWeeksIterator  { 
20112005    fn  next_back ( & mut  self )  -> Option < Self :: Item >  { 
20122006        let  current = self . value ; 
2013-         self . value  = current. checked_sub_days ( Days :: new ( 7 ) ) ?; 
2007+         self . value  = current. checked_sub_days ( Days :: new ( 7 ) ) . ok ( ) ?; 
20142008        Some ( current) 
20152009    } 
20162010} 
0 commit comments