Skip to content

Commit 1e17279

Browse files
committed
Make fns in internals const
1 parent e3f591f commit 1e17279

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

src/naive/internals.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(super) const FE: YearFlags = YearFlags(0o07);
5252
pub(super) const G: YearFlags = YearFlags(0o16);
5353
pub(super) const GF: YearFlags = YearFlags(0o06);
5454

55-
static YEAR_TO_FLAGS: [YearFlags; 400] = [
55+
const YEAR_TO_FLAGS: &[YearFlags; 400] = &[
5656
BA, G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA,
5757
G, F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G,
5858
F, E, DC, B, A, G, FE, D, C, B, AG, F, E, D, CB, A, G, F, ED, C, B, A, GF, E, D, C, BA, G, F,
@@ -71,7 +71,7 @@ static YEAR_TO_FLAGS: [YearFlags; 400] = [
7171
D, CB, A, G, F, ED, C, B, A, GF, E, D, C, // 400
7272
];
7373

74-
static YEAR_DELTAS: [u8; 401] = [
74+
const YEAR_DELTAS: &[u8; 401] = &[
7575
0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
7676
8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14,
7777
15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20,
@@ -93,48 +93,48 @@ static YEAR_DELTAS: [u8; 401] = [
9393
96, 97, 97, 97, 97, // 400+1
9494
];
9595

96-
pub(super) fn cycle_to_yo(cycle: u32) -> (u32, u32) {
96+
pub(super) const fn cycle_to_yo(cycle: u32) -> (u32, u32) {
9797
let mut year_mod_400 = cycle / 365;
9898
let mut ordinal0 = cycle % 365;
99-
let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
99+
let delta = YEAR_DELTAS[year_mod_400 as usize] as u32;
100100
if ordinal0 < delta {
101101
year_mod_400 -= 1;
102-
ordinal0 += 365 - u32::from(YEAR_DELTAS[year_mod_400 as usize]);
102+
ordinal0 += 365 - YEAR_DELTAS[year_mod_400 as usize] as u32;
103103
} else {
104104
ordinal0 -= delta;
105105
}
106106
(year_mod_400, ordinal0 + 1)
107107
}
108108

109-
pub(super) fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 {
110-
year_mod_400 * 365 + u32::from(YEAR_DELTAS[year_mod_400 as usize]) + ordinal - 1
109+
pub(super) const fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 {
110+
year_mod_400 * 365 + YEAR_DELTAS[year_mod_400 as usize] as u32 + ordinal - 1
111111
}
112112

113113
impl YearFlags {
114114
#[allow(unreachable_pub)] // public as an alias for benchmarks only
115115
#[doc(hidden)] // for benchmarks only
116116
#[inline]
117117
#[must_use]
118-
pub fn from_year(year: i32) -> YearFlags {
118+
pub const fn from_year(year: i32) -> YearFlags {
119119
let year = year.rem_euclid(400);
120120
YearFlags::from_year_mod_400(year)
121121
}
122122

123123
#[inline]
124-
pub(super) fn from_year_mod_400(year: i32) -> YearFlags {
124+
pub(super) const fn from_year_mod_400(year: i32) -> YearFlags {
125125
YEAR_TO_FLAGS[year as usize]
126126
}
127127

128128
#[inline]
129-
pub(super) fn ndays(&self) -> u32 {
129+
pub(super) const fn ndays(&self) -> u32 {
130130
let YearFlags(flags) = *self;
131-
366 - u32::from(flags >> 3)
131+
366 - (flags >> 3) as u32
132132
}
133133

134134
#[inline]
135-
pub(super) fn isoweek_delta(&self) -> u32 {
135+
pub(super) const fn isoweek_delta(&self) -> u32 {
136136
let YearFlags(flags) = *self;
137-
let mut delta = u32::from(flags) & 0b0111;
137+
let mut delta = (flags & 0b0111) as u32;
138138
if delta < 3 {
139139
delta += 7;
140140
}
@@ -178,7 +178,7 @@ pub(super) const MAX_OL: u32 = 366 << 1; // larger than the non-leap last day `(
178178
pub(super) const MAX_MDL: u32 = (12 << 6) | (31 << 1) | 1;
179179

180180
const XX: i8 = -128;
181-
static MDL_TO_OL: [i8; MAX_MDL as usize + 1] = [
181+
const MDL_TO_OL: &[i8; MAX_MDL as usize + 1] = &[
182182
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
183183
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
184184
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, // 0
@@ -221,7 +221,7 @@ static MDL_TO_OL: [i8; MAX_MDL as usize + 1] = [
221221
100, // 12
222222
];
223223

224-
static OL_TO_MDL: [u8; MAX_OL as usize + 1] = [
224+
const OL_TO_MDL: &[u8; MAX_OL as usize + 1] = &[
225225
0, 0, // 0
226226
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
227227
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
@@ -271,27 +271,31 @@ pub(super) struct Of(pub(crate) u32);
271271

272272
impl Of {
273273
#[inline]
274-
pub(super) fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Option<Of> {
274+
pub(super) const fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Option<Of> {
275275
match ordinal <= 366 {
276-
true => Some(Of((ordinal << 4) | u32::from(flags))),
276+
true => Some(Of((ordinal << 4) | flags as u32)),
277277
false => None,
278278
}
279279
}
280280

281281
#[inline]
282-
pub(super) fn from_mdf(Mdf(mdf): Mdf) -> Of {
282+
pub(super) const fn from_mdf(Mdf(mdf): Mdf) -> Of {
283283
let mdl = mdf >> 3;
284-
match MDL_TO_OL.get(mdl as usize) {
285-
Some(&v) => Of(mdf.wrapping_sub((i32::from(v) as u32 & 0x3ff) << 3)),
286-
None => Of(0),
284+
if mdl <= MAX_MDL {
285+
// Array is indexed from `[1..=MAX_MDL]`, with a `0` index having a meaningless value.
286+
let v = MDL_TO_OL[mdl as usize];
287+
Of(mdf.wrapping_sub((v as i32 as u32 & 0x3ff) << 3))
288+
} else {
289+
// Panicking here would be reasonable, but we are just going on with a safe value.
290+
Of(0)
287291
}
288292
}
289293

290294
#[inline]
291-
pub(super) fn valid(&self) -> bool {
295+
pub(super) const fn valid(&self) -> bool {
292296
let Of(of) = *self;
293297
let ol = of >> 3;
294-
(MIN_OL..=MAX_OL).contains(&ol)
298+
ol >= MIN_OL && ol <= MAX_OL
295299
}
296300

297301
#[inline]
@@ -332,7 +336,7 @@ impl Of {
332336

333337
#[cfg_attr(feature = "cargo-clippy", allow(clippy::wrong_self_convention))]
334338
#[inline]
335-
pub(super) fn to_mdf(&self) -> Mdf {
339+
pub(super) const fn to_mdf(&self) -> Mdf {
336340
Mdf::from_of(*self)
337341
}
338342

@@ -368,33 +372,39 @@ impl fmt::Debug for Of {
368372
/// (month, day of month and leap flag),
369373
/// which is an index to the `MDL_TO_OL` lookup table.
370374
#[derive(PartialEq, PartialOrd, Copy, Clone)]
371-
pub(super) struct Mdf(pub(super) u32);
375+
pub(super) struct Mdf(u32);
372376

373377
impl Mdf {
374378
#[inline]
375-
pub(super) fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Option<Mdf> {
379+
pub(super) const fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Option<Mdf> {
376380
match month <= 12 && day <= 31 {
377-
true => Some(Mdf((month << 9) | (day << 4) | u32::from(flags))),
381+
true => Some(Mdf((month << 9) | (day << 4) | flags as u32)),
378382
false => None,
379383
}
380384
}
381385

382386
#[inline]
383-
pub(super) fn from_of(Of(of): Of) -> Mdf {
387+
pub(super) const fn from_of(Of(of): Of) -> Mdf {
384388
let ol = of >> 3;
385-
match OL_TO_MDL.get(ol as usize) {
386-
Some(&v) => Mdf(of + (u32::from(v) << 3)),
387-
None => Mdf(0),
389+
if ol <= MAX_OL {
390+
// Array is indexed from `[1..=MAX_OL]`, with a `0` index having a meaningless value.
391+
Mdf(of + ((OL_TO_MDL[ol as usize] as u32) << 3))
392+
} else {
393+
// Panicking here would be reasonable, but we are just going on with a safe value.
394+
Mdf(0)
388395
}
389396
}
390397

391398
#[cfg(test)]
392-
pub(super) fn valid(&self) -> bool {
399+
pub(super) const fn valid(&self) -> bool {
393400
let Mdf(mdf) = *self;
394401
let mdl = mdf >> 3;
395-
match MDL_TO_OL.get(mdl as usize) {
396-
Some(&v) => v >= 0,
397-
None => false,
402+
if mdl <= MAX_MDL {
403+
// Array is indexed from `[1..=MAX_MDL]`, with a `0` index having a meaningless value.
404+
MDL_TO_OL[mdl as usize] >= 0
405+
} else {
406+
// Panicking here would be reasonable, but we are just going on with a safe value.
407+
false
398408
}
399409
}
400410

@@ -431,14 +441,14 @@ impl Mdf {
431441
}
432442

433443
#[inline]
434-
pub(super) fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
444+
pub(super) const fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
435445
let Mdf(mdf) = *self;
436-
Mdf((mdf & !0b1111) | u32::from(flags))
446+
Mdf((mdf & !0b1111) | flags as u32)
437447
}
438448

439449
#[cfg_attr(feature = "cargo-clippy", allow(clippy::wrong_self_convention))]
440450
#[inline]
441-
pub(super) fn to_of(&self) -> Of {
451+
pub(super) const fn to_of(&self) -> Of {
442452
Of::from_mdf(*self)
443453
}
444454
}

0 commit comments

Comments
 (0)