Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make eligible functions const. #983

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/format/locales.rs
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ pub struct OutOfRange {
}

impl OutOfRange {
fn new() -> OutOfRange {
const fn new() -> OutOfRange {
OutOfRange { _private: () }
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NaiveTime> {
pub const fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime> {
NaiveTime::from_hms_nano_opt(hour, min, sec, 0)
}

Expand Down Expand Up @@ -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<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;
}
Expand Down Expand Up @@ -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<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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FixedOffset> {
pub const fn east_opt(secs: i32) -> Option<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: secs })
} else {
Expand Down Expand Up @@ -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<FixedOffset> {
pub const fn west_opt(secs: i32) -> Option<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: -secs })
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/tz_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl From<Utf8Error> 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/tz_info/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 4 additions & 4 deletions src/offset/local/tz_info/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions src/offset/local/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down