@@ -72,7 +72,9 @@ pub const fn days_in_year(year: i32) -> u16 {
7272/// ```
7373#[ inline( always) ]
7474pub fn weeks_in_year ( year : i32 ) -> u8 {
75- let weekday = Date :: from_yo ( year, 1 ) . weekday ( ) ;
75+ let weekday = Date :: try_from_yo ( year, 1 )
76+ . expect ( "date is always valid" )
77+ . weekday ( ) ;
7678
7779 if ( weekday == Thursday ) || ( weekday == Wednesday && is_leap_year ( year) ) {
7880 53
@@ -120,6 +122,8 @@ impl Date {
120122 /// Date::from_ymd(2019, 2, 29); // 2019 isn't a leap year.
121123 /// ```
122124 #[ inline]
125+ #[ cfg( feature = "panicking-api" ) ]
126+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
123127 pub fn from_ymd ( year : i32 , month : u8 , day : u8 ) -> Self {
124128 /// Cumulative days through the beginning of a month in both common and
125129 /// leap years.
@@ -188,6 +192,8 @@ impl Date {
188192 /// Date::from_yo(2019, 366); // 2019 isn't a leap year.
189193 /// ```
190194 #[ inline( always) ]
195+ #[ cfg( feature = "panicking-api" ) ]
196+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
191197 pub fn from_yo ( year : i32 , ordinal : u16 ) -> Self {
192198 assert_value_in_range ! ( ordinal in 1 => days_in_year( year) , given year) ;
193199 Self { year, ordinal }
@@ -238,6 +244,8 @@ impl Date {
238244 /// Date::from_iso_ywd(2019, 53, Monday); // 2019 doesn't have 53 weeks.
239245 /// ```
240246 #[ inline]
247+ #[ cfg( feature = "panicking-api" ) ]
248+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
241249 pub fn from_iso_ywd ( year : i32 , week : u8 , weekday : Weekday ) -> Self {
242250 assert_value_in_range ! ( week in 1 => weeks_in_year( year) , given year) ;
243251
@@ -280,17 +288,21 @@ impl Date {
280288 ensure_value_in_range ! ( week in 1 => weeks_in_year( year) , given year) ;
281289
282290 let ordinal = week as u16 * 7 + weekday. iso_weekday_number ( ) as u16
283- - ( Self :: from_yo ( year, 4 ) . weekday ( ) . iso_weekday_number ( ) as u16 + 3 ) ;
291+ - ( Self :: try_from_yo ( year, 4 )
292+ . expect ( "date is always valid" )
293+ . weekday ( )
294+ . iso_weekday_number ( ) as u16
295+ + 3 ) ;
284296
285297 if ordinal < 1 {
286- return Ok ( Self :: from_yo ( year - 1 , ordinal + days_in_year ( year - 1 ) ) ) ;
298+ return Self :: try_from_yo ( year - 1 , ordinal + days_in_year ( year - 1 ) ) ;
287299 }
288300
289301 let days_in_cur_year = days_in_year ( year) ;
290302 if ordinal > days_in_cur_year {
291- Ok ( Self :: from_yo ( year + 1 , ordinal - days_in_cur_year) )
303+ Self :: try_from_yo ( year + 1 , ordinal - days_in_cur_year)
292304 } else {
293- Ok ( Self :: from_yo ( year, ordinal) )
305+ Self :: try_from_yo ( year, ordinal)
294306 }
295307 }
296308
@@ -362,6 +374,7 @@ impl Date {
362374 /// assert_eq!(Date::from_ymd(2019, 1, 1).month_day(), (1, 1));
363375 /// assert_eq!(Date::from_ymd(2019, 12, 31).month_day(), (12, 31));
364376 /// ```
377+ // TODO Refactor to prove to the compiler that this can't panic.
365378 #[ inline]
366379 pub fn month_day ( self ) -> ( u8 , u8 ) {
367380 let mut ordinal = self . ordinal ;
@@ -662,7 +675,7 @@ impl Date {
662675 let year = ( e / P ) - Y + ( N + M - month) / N ;
663676
664677 #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
665- Self :: from_ymd ( year as i32 , month as u8 , day as u8 )
678+ Self :: try_from_ymd ( year as i32 , month as u8 , day as u8 ) . expect ( "date is always valid" )
666679 }
667680}
668681
@@ -707,6 +720,8 @@ impl Date {
707720 /// );
708721 /// ```
709722 #[ inline( always) ]
723+ #[ cfg( feature = "panicking-api" ) ]
724+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
710725 pub fn with_hms ( self , hour : u8 , minute : u8 , second : u8 ) -> PrimitiveDateTime {
711726 PrimitiveDateTime :: new ( self , Time :: from_hms ( hour, minute, second) )
712727 }
@@ -741,6 +756,8 @@ impl Date {
741756 /// );
742757 /// ```
743758 #[ inline( always) ]
759+ #[ cfg( feature = "panicking-api" ) ]
760+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
744761 pub fn with_hms_milli (
745762 self ,
746763 hour : u8 ,
@@ -789,6 +806,8 @@ impl Date {
789806 /// );
790807 /// ```
791808 #[ inline( always) ]
809+ #[ cfg( feature = "panicking-api" ) ]
810+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
792811 pub fn with_hms_micro (
793812 self ,
794813 hour : u8 ,
@@ -837,6 +856,8 @@ impl Date {
837856 /// );
838857 /// ```
839858 #[ inline( always) ]
859+ #[ cfg( feature = "panicking-api" ) ]
860+ #[ cfg_attr( doc, doc( cfg( feature = "panicking-api" ) ) ) ]
840861 pub fn with_hms_nano (
841862 self ,
842863 hour : u8 ,
@@ -924,7 +945,10 @@ impl Date {
924945 /// Monday-based week numbering.
925946 #[ inline( always) ]
926947 fn adjustment ( year : i32 ) -> i16 {
927- match Date :: from_yo ( year, 1 ) . weekday ( ) {
948+ match Date :: try_from_yo ( year, 1 )
949+ . expect ( "date is always valid" )
950+ . weekday ( )
951+ {
928952 Monday => 7 ,
929953 Tuesday => 1 ,
930954 Wednesday => 2 ,
@@ -936,29 +960,36 @@ impl Date {
936960 }
937961
938962 match items {
939- items ! ( year, month, day) => Ok ( Self :: from_ymd ( year, month. get ( ) , day. get ( ) ) ) ,
940- items ! ( year, ordinal_day) => Ok ( Self :: from_yo ( year, ordinal_day. get ( ) ) ) ,
963+ items ! ( year, month, day) => Ok ( Self :: try_from_ymd ( year, month. get ( ) , day. get ( ) )
964+ . expect ( "components are checked when parsing" ) ) ,
965+ items ! ( year, ordinal_day) => Ok ( Self :: try_from_yo ( year, ordinal_day. get ( ) )
966+ . expect ( "components are checked when parsing" ) ) ,
941967 items ! ( week_based_year, iso_week, weekday) => {
942- Ok ( Self :: from_iso_ywd ( week_based_year, iso_week. get ( ) , weekday) )
968+ Ok (
969+ Self :: try_from_iso_ywd ( week_based_year, iso_week. get ( ) , weekday)
970+ . expect ( "components are checked when parsing" ) ,
971+ )
943972 }
944- items ! ( year, sunday_week, weekday) => Ok ( Self :: from_yo (
973+ items ! ( year, sunday_week, weekday) => Ok ( Self :: try_from_yo (
945974 year,
946975 #[ allow( clippy:: cast_sign_loss) ]
947976 {
948977 ( sunday_week as i16 * 7 + weekday. number_days_from_sunday ( ) as i16
949978 - adjustment ( year)
950979 + 1 ) as u16
951980 } ,
952- ) ) ,
953- items ! ( year, monday_week, weekday) => Ok ( Self :: from_yo (
981+ )
982+ . expect ( "components are checked when parsing" ) ) ,
983+ items ! ( year, monday_week, weekday) => Ok ( Self :: try_from_yo (
954984 year,
955985 #[ allow( clippy:: cast_sign_loss) ]
956986 {
957987 ( monday_week as i16 * 7 + weekday. number_days_from_monday ( ) as i16
958988 - adjustment ( year)
959989 + 1 ) as u16
960990 } ,
961- ) ) ,
991+ )
992+ . expect ( "components are checked when parsing" ) ) ,
962993 _ => Err ( ParseError :: InsufficientInformation ) ,
963994 }
964995 }
0 commit comments