From 5fb8dfebdcb95f0720fd5a67dd9929ce28a7c999 Mon Sep 17 00:00:00 2001 From: Tormod Gjeitnes Hellen Date: Wed, 8 Mar 2023 18:23:56 +0100 Subject: [PATCH] Make eligible functions const. --- src/format/locales.rs | 16 ++++++++-------- src/format/scan.rs | 2 +- src/lib.rs | 2 +- src/month.rs | 8 ++++---- src/naive/internals.rs | 6 +++--- src/naive/time/mod.rs | 6 +++--- src/offset/fixed.rs | 4 ++-- src/offset/local/tz_info/mod.rs | 2 +- src/offset/local/tz_info/parser.rs | 2 +- src/offset/local/tz_info/rule.rs | 8 ++++---- src/offset/local/tz_info/timezone.rs | 6 +++--- src/round.rs | 2 +- src/weekday.rs | 4 ++-- 13 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/format/locales.rs b/src/format/locales.rs index f7b4bbde5b..d6fecc978b 100644 --- a/src/format/locales.rs +++ b/src/format/locales.rs @@ -1,33 +1,33 @@ use pure_rust_locales::{locale_match, Locale}; -pub(crate) fn short_months(locale: Locale) -> &'static [&'static str] { +pub(crate) const fn short_months(locale: Locale) -> &'static [&'static str] { locale_match!(locale => LC_TIME::ABMON) } -pub(crate) fn long_months(locale: Locale) -> &'static [&'static str] { +pub(crate) const fn long_months(locale: Locale) -> &'static [&'static str] { locale_match!(locale => LC_TIME::MON) } -pub(crate) fn short_weekdays(locale: Locale) -> &'static [&'static str] { +pub(crate) const fn short_weekdays(locale: Locale) -> &'static [&'static str] { locale_match!(locale => LC_TIME::ABDAY) } -pub(crate) fn long_weekdays(locale: Locale) -> &'static [&'static str] { +pub(crate) const fn long_weekdays(locale: Locale) -> &'static [&'static str] { locale_match!(locale => LC_TIME::DAY) } -pub(crate) fn am_pm(locale: Locale) -> &'static [&'static str] { +pub(crate) const fn am_pm(locale: Locale) -> &'static [&'static str] { locale_match!(locale => LC_TIME::AM_PM) } -pub(crate) fn d_fmt(locale: Locale) -> &'static str { +pub(crate) const fn d_fmt(locale: Locale) -> &'static str { locale_match!(locale => LC_TIME::D_FMT) } -pub(crate) fn d_t_fmt(locale: Locale) -> &'static str { +pub(crate) const fn d_t_fmt(locale: Locale) -> &'static str { locale_match!(locale => LC_TIME::D_T_FMT) } -pub(crate) fn t_fmt(locale: Locale) -> &'static str { +pub(crate) const fn t_fmt(locale: Locale) -> &'static str { locale_match!(locale => LC_TIME::T_FMT) } diff --git a/src/format/scan.rs b/src/format/scan.rs index 2ac81318a7..e70d7b8cda 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -254,7 +254,7 @@ fn timezone_offset_internal( where F: FnMut(&str) -> ParseResult<&str>, { - fn digits(s: &str) -> ParseResult<(u8, u8)> { + const fn digits(s: &str) -> ParseResult<(u8, u8)> { let b = s.as_bytes(); if b.len() < 2 { Err(TOO_SHORT) diff --git a/src/lib.rs b/src/lib.rs index d3859017aa..d108b73eaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -500,7 +500,7 @@ pub struct OutOfRange { } impl OutOfRange { - fn new() -> OutOfRange { + const fn new() -> OutOfRange { OutOfRange { _private: () } } } diff --git a/src/month.rs b/src/month.rs index 7abf4eb196..4ff42ba24e 100644 --- a/src/month.rs +++ b/src/month.rs @@ -66,7 +66,7 @@ impl Month { /// ----------- | --------- | ---------- | --- | --------- /// `m.succ()`: | `February` | `March` | `...` | `January` #[inline] - pub fn succ(&self) -> Month { + pub const fn succ(&self) -> Month { match *self { Month::January => Month::February, Month::February => Month::March, @@ -89,7 +89,7 @@ impl Month { /// ----------- | --------- | ---------- | --- | --------- /// `m.pred()`: | `December` | `January` | `...` | `November` #[inline] - pub fn pred(&self) -> Month { + pub const fn pred(&self) -> Month { match *self { Month::January => Month::December, Month::February => Month::January, @@ -112,7 +112,7 @@ impl Month { /// -------------------------| --------- | ---------- | --- | ----- /// `m.number_from_month()`: | 1 | 2 | `...` | 12 #[inline] - pub fn number_from_month(&self) -> u32 { + pub const fn number_from_month(&self) -> u32 { match *self { Month::January => 1, Month::February => 2, @@ -136,7 +136,7 @@ impl Month { /// /// assert_eq!(Month::January.name(), "January") /// ``` - pub fn name(&self) -> &'static str { + pub const fn name(&self) -> &'static str { match *self { Month::January => "January", Month::February => "February", diff --git a/src/naive/internals.rs b/src/naive/internals.rs index 6cbf638f7f..b9e9ef2bd8 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -301,7 +301,7 @@ impl Of { } #[inline] - pub(super) fn with_ordinal(&self, ordinal: u32) -> Option { + pub(super) const fn with_ordinal(&self, ordinal: u32) -> Option { if ordinal > 366 { return None; } @@ -405,7 +405,7 @@ impl Mdf { } #[inline] - pub(super) fn with_month(&self, month: u32) -> Option { + pub(super) const fn with_month(&self, month: u32) -> Option { if month > 12 { return None; } @@ -421,7 +421,7 @@ impl Mdf { } #[inline] - pub(super) fn with_day(&self, day: u32) -> Option { + pub(super) const fn with_day(&self, day: u32) -> Option { if day > 31 { return None; } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 0fd00be297..863e21b198 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -235,7 +235,7 @@ impl NaiveTime { /// assert!(from_hms_opt(23, 59, 60).is_none()); /// ``` #[inline] - pub fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option { + pub const fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option { NaiveTime::from_hms_nano_opt(hour, min, sec, 0) } @@ -354,7 +354,7 @@ impl NaiveTime { /// assert!(from_hmsn_opt(23, 59, 59, 2_000_000_000).is_none()); /// ``` #[inline] - pub fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option { + pub const fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option { if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 { return None; } @@ -395,7 +395,7 @@ impl NaiveTime { /// assert!(from_nsecs_opt(86399, 2_000_000_000).is_none()); /// ``` #[inline] - pub fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option { + pub const fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option { if secs >= 86_400 || nano >= 2_000_000_000 { return None; } diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 8bdf102d7b..be638ef5c8 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -52,7 +52,7 @@ impl FixedOffset { /// .and_hms_opt(0, 0, 0).unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ``` - pub fn east_opt(secs: i32) -> Option { + pub const fn east_opt(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: secs }) } else { @@ -83,7 +83,7 @@ impl FixedOffset { /// .and_hms_opt(0, 0, 0).unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ``` - pub fn west_opt(secs: i32) -> Option { + pub const fn west_opt(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: -secs }) } else { diff --git a/src/offset/local/tz_info/mod.rs b/src/offset/local/tz_info/mod.rs index bd2693b6bb..3143c30d6e 100644 --- a/src/offset/local/tz_info/mod.rs +++ b/src/offset/local/tz_info/mod.rs @@ -102,7 +102,7 @@ impl From for Error { // MSRV: 1.38 #[inline] -fn rem_euclid(v: i64, rhs: i64) -> i64 { +const fn rem_euclid(v: i64, rhs: i64) -> i64 { let r = v % rhs; if r < 0 { if rhs < 0 { diff --git a/src/offset/local/tz_info/parser.rs b/src/offset/local/tz_info/parser.rs index 5652a0ea95..78c667ac23 100644 --- a/src/offset/local/tz_info/parser.rs +++ b/src/offset/local/tz_info/parser.rs @@ -238,7 +238,7 @@ impl<'a> Cursor<'a> { } /// Returns `true` if data is remaining - pub(crate) fn is_empty(&self) -> bool { + pub(crate) const fn is_empty(&self) -> bool { self.remaining.is_empty() } diff --git a/src/offset/local/tz_info/rule.rs b/src/offset/local/tz_info/rule.rs index 7befddb5cf..50e092c86f 100644 --- a/src/offset/local/tz_info/rule.rs +++ b/src/offset/local/tz_info/rule.rs @@ -127,7 +127,7 @@ pub(super) struct AlternateTime { impl AlternateTime { /// Construct a transition rule representing alternate local time types - fn new( + const fn new( std: LocalTimeType, dst: LocalTimeType, dst_start: RuleDay, @@ -504,7 +504,7 @@ impl RuleDay { } /// Construct a transition rule day represented by a zero-based Julian day in `[0, 365]`, taking occasional Feb 29 into account - fn julian_0(julian_day_0: u16) -> Result { + const fn julian_0(julian_day_0: u16) -> Result { if julian_day_0 > 365 { return Err(Error::TransitionRule("invalid rule day julian day")); } @@ -737,7 +737,7 @@ const DAY_IN_MONTHS_LEAP_YEAR_FROM_MARCH: [i64; 12] = /// * `year`: Year /// * `month`: Month in `[1, 12]` /// * `month_day`: Day of the month in `[1, 31]` -pub(crate) fn days_since_unix_epoch(year: i32, month: usize, month_day: i64) -> i64 { +pub(crate) const fn days_since_unix_epoch(year: i32, month: usize, month_day: i64) -> i64 { let is_leap_year = is_leap_year(year); let year = year as i64; @@ -768,7 +768,7 @@ pub(crate) fn days_since_unix_epoch(year: i32, month: usize, month_day: i64) -> } /// Check if a year is a leap year -pub(crate) fn is_leap_year(year: i32) -> bool { +pub(crate) const fn is_leap_year(year: i32) -> bool { year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) } diff --git a/src/offset/local/tz_info/timezone.rs b/src/offset/local/tz_info/timezone.rs index 8572825a89..956064ba84 100644 --- a/src/offset/local/tz_info/timezone.rs +++ b/src/offset/local/tz_info/timezone.rs @@ -374,7 +374,7 @@ impl<'a> TimeZoneRef<'a> { } /// Convert Unix time to Unix leap time, from the list of leap seconds in a time zone - fn unix_time_to_unix_leap_time(&self, unix_time: i64) -> Result { + const fn unix_time_to_unix_leap_time(&self, unix_time: i64) -> Result { let mut unix_leap_time = unix_time; let mut i = 0; @@ -563,7 +563,7 @@ impl LocalTimeType { } /// Construct a local time type with the specified UTC offset in seconds - pub(super) fn with_offset(ut_offset: i32) -> Result { + pub(super) const fn with_offset(ut_offset: i32) -> Result { if ut_offset == i32::min_value() { return Err(Error::LocalTimeType("invalid UTC offset")); } @@ -608,7 +608,7 @@ fn find_tz_file(path: impl AsRef) -> Result { } #[inline] -fn saturating_abs(v: i32) -> i32 { +const fn saturating_abs(v: i32) -> i32 { if v.is_positive() { v } else if v == i32::min_value() { diff --git a/src/round.rs b/src/round.rs index 0143fa186b..07cfbbb17a 100644 --- a/src/round.rs +++ b/src/round.rs @@ -75,7 +75,7 @@ where } // Return the maximum span in nanoseconds for the target number of digits. -fn span_for_digits(digits: u16) -> u32 { +const fn span_for_digits(digits: u16) -> u32 { // fast lookup form of: 10^(9-min(9,digits)) match digits { 0 => 1_000_000_000, diff --git a/src/weekday.rs b/src/weekday.rs index 87a2a05340..5bfa4e2e0d 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -37,7 +37,7 @@ impl Weekday { /// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon` #[inline] - pub fn succ(&self) -> Weekday { + pub const fn succ(&self) -> Weekday { match *self { Weekday::Mon => Weekday::Tue, Weekday::Tue => Weekday::Wed, @@ -55,7 +55,7 @@ impl Weekday { /// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` #[inline] - pub fn pred(&self) -> Weekday { + pub const fn pred(&self) -> Weekday { match *self { Weekday::Mon => Weekday::Sun, Weekday::Tue => Weekday::Mon,