File tree 5 files changed +56
-13
lines changed
5 files changed +56
-13
lines changed Original file line number Diff line number Diff line change
1
+ //! Hints to the compiler that affects how code should be emitted or optimized.
2
+
3
+ #![ allow( dead_code) ] // may be used in the future and has minimal overhead
4
+
5
+ /// Indicate that a given branch is **not** likely to be taken, relatively speaking.
6
+ #[ inline( always) ]
7
+ #[ cold]
8
+ pub ( crate ) const fn cold_path ( ) { }
9
+
10
+ /// Indicate that a given condition is likely to be true.
11
+ #[ inline( always) ]
12
+ pub ( crate ) const fn likely ( b : bool ) -> bool {
13
+ if !b {
14
+ cold_path ( ) ;
15
+ }
16
+ b
17
+ }
18
+
19
+ /// Indicate that a given condition is likely to be false.
20
+ #[ inline( always) ]
21
+ pub ( crate ) const fn unlikely ( b : bool ) -> bool {
22
+ if b {
23
+ cold_path ( ) ;
24
+ }
25
+ b
26
+ }
Original file line number Diff line number Diff line change 8
8
#![ doc( test( attr( deny( warnings) ) ) ) ]
9
9
10
10
pub mod convert;
11
+ mod hint;
11
12
pub mod util;
Original file line number Diff line number Diff line change 1
1
//! Utility functions.
2
2
3
+ use crate :: hint;
4
+
3
5
/// Returns if the provided year is a leap year in the proleptic Gregorian calendar. Uses
4
6
/// [astronomical year numbering](https://en.wikipedia.org/wiki/Astronomical_year_numbering).
5
7
///
@@ -61,3 +63,26 @@ pub const fn weeks_in_year(year: i32) -> u8 {
61
63
_ => 52 ,
62
64
}
63
65
}
66
+
67
+ /// Get the number of days in the month of a given year.
68
+ ///
69
+ /// ```rust
70
+ /// # use time_core::util::days_in_month;
71
+ /// assert_eq!(days_in_month(2, 2020), 29);
72
+ /// ```
73
+ ///
74
+ /// Note: This function is not exposed by the `time` crate. It is an implementation detail.
75
+ pub const fn days_in_month ( month : u8 , year : i32 ) -> u8 {
76
+ debug_assert ! ( month >= 1 ) ;
77
+ debug_assert ! ( month <= 12 ) ;
78
+
79
+ if hint:: unlikely ( month == 2 ) {
80
+ if is_leap_year ( year) {
81
+ 29
82
+ } else {
83
+ 28
84
+ }
85
+ } else {
86
+ 30 | month ^ ( month >> 3 )
87
+ }
88
+ }
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ use core::str::FromStr;
7
7
use powerfmt:: smart_display:: { FormatterOptions , Metadata , SmartDisplay } ;
8
8
9
9
use self :: Month :: * ;
10
- use crate :: { error, hint , util} ;
10
+ use crate :: { error, util} ;
11
11
12
12
/// Months of the year.
13
13
#[ repr( u8 ) ]
@@ -72,16 +72,7 @@ impl Month {
72
72
/// assert_eq!(Month::February.length(2020), 29);
73
73
/// ```
74
74
pub const fn length ( self , year : i32 ) -> u8 {
75
- let val = self as u8 ;
76
- if hint:: unlikely ( val == 2 ) {
77
- if util:: is_leap_year ( year) {
78
- 29
79
- } else {
80
- 28
81
- }
82
- } else {
83
- 30 | val ^ ( val >> 3 )
84
- }
75
+ util:: days_in_month ( self , year)
85
76
}
86
77
87
78
/// Get the previous month.
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ pub(crate) enum DateAdjustment {
21
21
/// assert_eq!(util::days_in_month(Month::February, 2020), 29);
22
22
/// ```
23
23
pub const fn days_in_month ( month : Month , year : i32 ) -> u8 {
24
- month . length ( year)
24
+ time_core :: util :: days_in_month ( month as u8 , year)
25
25
}
26
26
27
27
/// Get the number of days in the month of a given year.
@@ -36,7 +36,7 @@ pub const fn days_in_month(month: Month, year: i32) -> u8 {
36
36
note = "use `days_in_month` or `Month::length` instead"
37
37
) ]
38
38
pub const fn days_in_year_month ( year : i32 , month : Month ) -> u8 {
39
- month . length ( year)
39
+ days_in_month ( month , year)
40
40
}
41
41
42
42
/// Update time zone information from the system.
You can’t perform that action at this time.
0 commit comments