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 1717
1818use crate :: Weekday ;
1919use core:: { fmt, i32} ;
20- use num_traits:: FromPrimitive ;
2120
2221/// The internal date representation. This also includes the packed `Mdf` value.
2322pub ( super ) type DateImpl = i32 ;
@@ -320,17 +319,17 @@ impl Of {
320319 }
321320
322321 #[ inline]
323- pub ( super ) fn weekday ( & self ) -> Weekday {
322+ pub ( super ) const fn weekday ( & self ) -> Weekday {
324323 let Of ( of) = * self ;
325- Weekday :: from_u32 ( ( ( of >> 4 ) + ( of & 0b111 ) ) % 7 ) . unwrap ( )
324+ Weekday :: from_u32_mod7 ( ( of >> 4 ) + ( of & 0b111 ) )
326325 }
327326
328327 #[ inline]
329328 pub ( super ) fn isoweekdate_raw ( & self ) -> ( u32 , Weekday ) {
330329 // week ordinal = ordinal + delta
331330 let Of ( of) = * self ;
332331 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) )
334333 }
335334
336335 #[ 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 {
5050}
5151
5252impl 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+
5368 /// The next day in the week.
5469 ///
5570 /// `w`: | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
5671 /// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
5772 /// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon`
5873 #[ inline]
5974 #[ must_use]
60- pub fn succ ( & self ) -> Weekday {
75+ pub const fn succ ( & self ) -> Weekday {
6176 match * self {
6277 Weekday :: Mon => Weekday :: Tue ,
6378 Weekday :: Tue => Weekday :: Wed ,
@@ -76,7 +91,7 @@ impl Weekday {
7691 /// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat`
7792 #[ inline]
7893 #[ must_use]
79- pub fn pred ( & self ) -> Weekday {
94+ pub const fn pred ( & self ) -> Weekday {
8095 match * self {
8196 Weekday :: Mon => Weekday :: Sun ,
8297 Weekday :: Tue => Weekday :: Mon ,
You can’t perform that action at this time.
0 commit comments