File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change 17
17
18
18
use crate :: Weekday ;
19
19
use core:: { fmt, i32} ;
20
- use num_traits:: FromPrimitive ;
21
20
22
21
/// The internal date representation. This also includes the packed `Mdf` value.
23
22
pub ( super ) type DateImpl = i32 ;
@@ -320,17 +319,17 @@ impl Of {
320
319
}
321
320
322
321
#[ inline]
323
- pub ( super ) fn weekday ( & self ) -> Weekday {
322
+ pub ( super ) const fn weekday ( & self ) -> Weekday {
324
323
let Of ( of) = * self ;
325
- Weekday :: from_u32 ( ( ( of >> 4 ) + ( of & 0b111 ) ) % 7 ) . unwrap ( )
324
+ Weekday :: from_u32_mod7 ( ( of >> 4 ) + ( of & 0b111 ) )
326
325
}
327
326
328
327
#[ inline]
329
328
pub ( super ) fn isoweekdate_raw ( & self ) -> ( u32 , Weekday ) {
330
329
// week ordinal = ordinal + delta
331
330
let Of ( of) = * self ;
332
331
let weekord = ( of >> 4 ) . wrapping_add ( self . flags ( ) . isoweek_delta ( ) ) ;
333
- ( weekord / 7 , Weekday :: from_u32 ( weekord % 7 ) . unwrap ( ) )
332
+ ( weekord / 7 , Weekday :: from_u32_mod7 ( weekord) )
334
333
}
335
334
336
335
#[ cfg_attr( feature = "cargo-clippy" , allow( clippy:: wrong_self_convention) ) ]
Original file line number Diff line number Diff line change @@ -50,14 +50,29 @@ pub enum Weekday {
50
50
}
51
51
52
52
impl Weekday {
53
+ /// Create a `Weekday` from an `u32`, with Monday = 0.
54
+ /// Infallible, takes `n % 7`.
55
+ #[ inline]
56
+ pub ( crate ) const fn from_u32_mod7 ( n : u32 ) -> Weekday {
57
+ match n % 7 {
58
+ 0 => Weekday :: Mon ,
59
+ 1 => Weekday :: Tue ,
60
+ 2 => Weekday :: Wed ,
61
+ 3 => Weekday :: Thu ,
62
+ 4 => Weekday :: Fri ,
63
+ 5 => Weekday :: Sat ,
64
+ _ => Weekday :: Sun ,
65
+ }
66
+ }
67
+
53
68
/// The next day in the week.
54
69
///
55
70
/// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
56
71
/// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
57
72
/// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon`
58
73
#[ inline]
59
74
#[ must_use]
60
- pub fn succ ( & self ) -> Weekday {
75
+ pub const fn succ ( & self ) -> Weekday {
61
76
match * self {
62
77
Weekday :: Mon => Weekday :: Tue ,
63
78
Weekday :: Tue => Weekday :: Wed ,
@@ -76,7 +91,7 @@ impl Weekday {
76
91
/// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat`
77
92
#[ inline]
78
93
#[ must_use]
79
- pub fn pred ( & self ) -> Weekday {
94
+ pub const fn pred ( & self ) -> Weekday {
80
95
match * self {
81
96
Weekday :: Mon => Weekday :: Sun ,
82
97
Weekday :: Tue => Weekday :: Mon ,
You can’t perform that action at this time.
0 commit comments