Make eligible functions const.

This commit is contained in:
Tormod Gjeitnes Hellen 2023-03-08 18:23:56 +01:00 committed by Dirkjan Ochtman
parent 8fd7ca24bb
commit 2a6a07d967
11 changed files with 32 additions and 32 deletions

View File

@ -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)
}

View File

@ -254,7 +254,7 @@ fn timezone_offset_internal<F>(
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)

View File

@ -66,7 +66,7 @@ impl Month {
/// `m.succ()`: | `February` | `March` | `...` | `January`
#[inline]
#[must_use]
pub fn succ(&self) -> Month {
pub const fn succ(&self) -> Month {
match *self {
Month::January => Month::February,
Month::February => Month::March,
@ -90,7 +90,7 @@ impl Month {
/// `m.pred()`: | `December` | `January` | `...` | `November`
#[inline]
#[must_use]
pub fn pred(&self) -> Month {
pub const fn pred(&self) -> Month {
match *self {
Month::January => Month::December,
Month::February => Month::January,
@ -114,7 +114,7 @@ impl Month {
/// `m.number_from_month()`: | 1 | 2 | `...` | 12
#[inline]
#[must_use]
pub fn number_from_month(&self) -> u32 {
pub const fn number_from_month(&self) -> u32 {
match *self {
Month::January => 1,
Month::February => 2,
@ -139,7 +139,7 @@ impl Month {
/// assert_eq!(Month::January.name(), "January")
/// ```
#[must_use]
pub fn name(&self) -> &'static str {
pub const fn name(&self) -> &'static str {
match *self {
Month::January => "January",
Month::February => "February",

View File

@ -301,7 +301,7 @@ impl Of {
}
#[inline]
pub(super) fn with_ordinal(&self, ordinal: u32) -> Option<Of> {
pub(super) const fn with_ordinal(&self, ordinal: u32) -> Option<Of> {
if ordinal > 366 {
return None;
}
@ -405,7 +405,7 @@ impl Mdf {
}
#[inline]
pub(super) fn with_month(&self, month: u32) -> Option<Mdf> {
pub(super) const fn with_month(&self, month: u32) -> Option<Mdf> {
if month > 12 {
return None;
}
@ -421,7 +421,7 @@ impl Mdf {
}
#[inline]
pub(super) fn with_day(&self, day: u32) -> Option<Mdf> {
pub(super) const fn with_day(&self, day: u32) -> Option<Mdf> {
if day > 31 {
return None;
}

View File

@ -241,7 +241,7 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime> {
pub const fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime> {
NaiveTime::from_hms_nano_opt(hour, min, sec, 0)
}
@ -366,7 +366,7 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveTime> {
pub const fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveTime> {
if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 {
return None;
}
@ -409,7 +409,7 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> {
pub const fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> {
if secs >= 86_400 || nano >= 2_000_000_000 {
return None;
}

View File

@ -55,7 +55,7 @@ impl FixedOffset {
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
/// ```
#[must_use]
pub fn east_opt(secs: i32) -> Option<FixedOffset> {
pub const fn east_opt(secs: i32) -> Option<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: secs })
} else {
@ -89,7 +89,7 @@ impl FixedOffset {
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
/// ```
#[must_use]
pub fn west_opt(secs: i32) -> Option<FixedOffset> {
pub const fn west_opt(secs: i32) -> Option<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: -secs })
} else {

View File

@ -237,7 +237,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()
}

View File

@ -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<Self, Error> {
const fn julian_0(julian_day_0: u16) -> Result<Self, Error> {
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)
}

View File

@ -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<i64, Error> {
const fn unix_time_to_unix_leap_time(&self, unix_time: i64) -> Result<i64, Error> {
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<Self, Error> {
pub(super) const fn with_offset(ut_offset: i32) -> Result<Self, Error> {
if ut_offset == i32::min_value() {
return Err(Error::LocalTimeType("invalid UTC offset"));
}
@ -608,7 +608,7 @@ fn find_tz_file(path: impl AsRef<Path>) -> Result<File, Error> {
}
#[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() {

View File

@ -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,

View File

@ -57,7 +57,7 @@ impl Weekday {
/// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon`
#[inline]
#[must_use]
pub fn succ(&self) -> Weekday {
pub const fn succ(&self) -> Weekday {
match *self {
Weekday::Mon => Weekday::Tue,
Weekday::Tue => Weekday::Wed,
@ -76,7 +76,7 @@ impl Weekday {
/// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat`
#[inline]
#[must_use]
pub fn pred(&self) -> Weekday {
pub const fn pred(&self) -> Weekday {
match *self {
Weekday::Mon => Weekday::Sun,
Weekday::Tue => Weekday::Mon,