From dd963fb6d4238b25140b0267e7ba2c7a46bb283e 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/mod.rs | 2 +- src/format/scan.rs | 2 +- src/lib.rs | 2 +- src/month.rs | 10 +++---- src/naive/date.rs | 12 ++++---- src/naive/datetime/mod.rs | 6 ++-- src/naive/internals.rs | 20 ++++++------- src/naive/isoweek.rs | 6 ++-- src/naive/time/mod.rs | 6 ++-- src/offset/fixed.rs | 8 +++--- src/offset/local/tz_info/mod.rs | 2 +- src/offset/local/tz_info/parser.rs | 6 ++-- src/offset/local/tz_info/rule.rs | 8 +++--- src/offset/local/tz_info/timezone.rs | 18 ++++++------ src/round.rs | 2 +- src/time_delta.rs | 42 ++++++++++++++-------------- src/weekday.rs | 12 ++++---- 18 files changed, 90 insertions(+), 90 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/mod.rs b/src/format/mod.rs index 3d78f0a476..96fb79612e 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -350,7 +350,7 @@ pub struct ParseError(ParseErrorKind); impl ParseError { /// The category of parse error - pub fn kind(&self) -> ParseErrorKind { + pub const fn kind(&self) -> ParseErrorKind { self.0 } } diff --git a/src/format/scan.rs b/src/format/scan.rs index 27849767e4..bb51a24e9d 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -222,7 +222,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 44303bde77..95bc5520d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -498,7 +498,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 5c66ce161a..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", @@ -183,7 +183,7 @@ pub struct Months(pub(crate) u32); impl Months { /// Construct a new `Months` from a number of months - pub fn new(num: u32) -> Self { + pub const fn new(num: u32) -> Self { Self(num) } } diff --git a/src/naive/date.rs b/src/naive/date.rs index 9603066708..c06261f6a7 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -129,7 +129,7 @@ pub struct Days(pub(crate) u64); impl Days { /// Construct a new `Days` from a number of days - pub fn new(num: u64) -> Self { + pub const fn new(num: u64) -> Self { Self(num) } } @@ -691,7 +691,7 @@ impl NaiveDate { /// assert_eq!(dt.time(), t); /// ``` #[inline] - pub fn and_time(&self, time: NaiveTime) -> NaiveDateTime { + pub const fn and_time(&self, time: NaiveTime) -> NaiveDateTime { NaiveDateTime::new(*self, time) } @@ -881,7 +881,7 @@ impl NaiveDate { /// Returns the packed ordinal-flags. #[inline] - fn of(&self) -> Of { + const fn of(&self) -> Of { Of((self.ymdf & 0b1_1111_1111_1111) as u32) } @@ -1204,7 +1204,7 @@ impl NaiveDate { /// } /// ``` #[inline] - pub fn iter_days(&self) -> NaiveDateDaysIterator { + pub const fn iter_days(&self) -> NaiveDateDaysIterator { NaiveDateDaysIterator { value: *self } } @@ -1235,14 +1235,14 @@ impl NaiveDate { /// } /// ``` #[inline] - pub fn iter_weeks(&self) -> NaiveDateWeeksIterator { + pub const fn iter_weeks(&self) -> NaiveDateWeeksIterator { NaiveDateWeeksIterator { value: *self } } /// Returns the [`NaiveWeek`] that the date belongs to, starting with the [`Weekday`] /// specified. #[inline] - pub fn week(&self, start: Weekday) -> NaiveWeek { + pub const fn week(&self, start: Weekday) -> NaiveWeek { NaiveWeek { date: *self, start } } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index cf4d20d064..988d2c9426 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -126,7 +126,7 @@ impl NaiveDateTime { /// assert_eq!(dt.time(), t); /// ``` #[inline] - pub fn new(date: NaiveDate, time: NaiveTime) -> NaiveDateTime { + pub const fn new(date: NaiveDate, time: NaiveTime) -> NaiveDateTime { NaiveDateTime { date, time } } @@ -320,7 +320,7 @@ impl NaiveDateTime { /// assert_eq!(dt.date(), NaiveDate::from_ymd_opt(2016, 7, 8).unwrap()); /// ``` #[inline] - pub fn date(&self) -> NaiveDate { + pub const fn date(&self) -> NaiveDate { self.date } @@ -335,7 +335,7 @@ impl NaiveDateTime { /// assert_eq!(dt.time(), NaiveTime::from_hms_opt(9, 10, 11).unwrap()); /// ``` #[inline] - pub fn time(&self) -> NaiveTime { + pub const fn time(&self) -> NaiveTime { self.time } diff --git a/src/naive/internals.rs b/src/naive/internals.rs index 7d642e914d..71ddfa22bd 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -143,7 +143,7 @@ impl YearFlags { } #[inline] - pub(super) fn nisoweeks(&self) -> u32 { + pub(super) const fn nisoweeks(&self) -> u32 { let YearFlags(flags) = *self; 52 + ((0b0000_0100_0000_0110 >> flags as usize) & 1) } @@ -296,13 +296,13 @@ impl Of { } #[inline] - pub(super) fn ordinal(&self) -> u32 { + pub(super) const fn ordinal(&self) -> u32 { let Of(of) = *self; of >> 4 } #[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; } @@ -312,7 +312,7 @@ impl Of { } #[inline] - pub(super) fn flags(&self) -> YearFlags { + pub(super) const fn flags(&self) -> YearFlags { let Of(of) = *self; YearFlags((of & 0b1111) as u8) } @@ -338,13 +338,13 @@ impl Of { } #[inline] - pub(super) fn succ(&self) -> Of { + pub(super) const fn succ(&self) -> Of { let Of(of) = *self; Of(of + (1 << 4)) } #[inline] - pub(super) fn pred(&self) -> Of { + pub(super) const fn pred(&self) -> Of { let Of(of) = *self; Of(of - (1 << 4)) } @@ -400,13 +400,13 @@ impl Mdf { } #[inline] - pub(super) fn month(&self) -> u32 { + pub(super) const fn month(&self) -> u32 { let Mdf(mdf) = *self; mdf >> 9 } #[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; } @@ -416,13 +416,13 @@ impl Mdf { } #[inline] - pub(super) fn day(&self) -> u32 { + pub(super) const fn day(&self) -> u32 { let Mdf(mdf) = *self; (mdf >> 4) & 0b1_1111 } #[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/isoweek.rs b/src/naive/isoweek.rs index 501b08c769..6a4fcfd110 100644 --- a/src/naive/isoweek.rs +++ b/src/naive/isoweek.rs @@ -71,7 +71,7 @@ impl IsoWeek { /// assert_eq!(d, NaiveDate::from_ymd_opt(2014, 12, 29).unwrap()); /// ``` #[inline] - pub fn year(&self) -> i32 { + pub const fn year(&self) -> i32 { self.ywf >> 10 } @@ -88,7 +88,7 @@ impl IsoWeek { /// assert_eq!(d.iso_week().week(), 15); /// ``` #[inline] - pub fn week(&self) -> u32 { + pub const fn week(&self) -> u32 { ((self.ywf >> 4) & 0x3f) as u32 } @@ -105,7 +105,7 @@ impl IsoWeek { /// assert_eq!(d.iso_week().week0(), 14); /// ``` #[inline] - pub fn week0(&self) -> u32 { + pub const fn week0(&self) -> u32 { ((self.ywf >> 4) & 0x3f) as u32 - 1 } } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index f0f69d485e..baf345472a 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 abfb0c9358..06880d6d08 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 { @@ -93,13 +93,13 @@ impl FixedOffset { /// Returns the number of seconds to add to convert from UTC to the local time. #[inline] - pub fn local_minus_utc(&self) -> i32 { + pub const fn local_minus_utc(&self) -> i32 { self.local_minus_utc } /// Returns the number of seconds to add to convert from the local time to UTC. #[inline] - pub fn utc_minus_local(&self) -> i32 { + pub const fn utc_minus_local(&self) -> i32 { -self.local_minus_utc } } 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 77f8e481b5..78c667ac23 100644 --- a/src/offset/local/tz_info/parser.rs +++ b/src/offset/local/tz_info/parser.rs @@ -224,7 +224,7 @@ pub(crate) struct Cursor<'a> { impl<'a> Cursor<'a> { /// Construct a new `Cursor` from remaining data - pub(crate) fn new(remaining: &'a [u8]) -> Self { + pub(crate) const fn new(remaining: &'a [u8]) -> Self { Self { remaining, read_count: 0 } } @@ -233,12 +233,12 @@ impl<'a> Cursor<'a> { } /// Returns remaining data - pub(crate) fn remaining(&self) -> &'a [u8] { + pub(crate) const fn remaining(&self) -> &'a [u8] { self.remaining } /// 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 ae6fb5f848..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; @@ -438,12 +438,12 @@ pub(super) struct Transition { impl Transition { /// Construct a TZif file transition - pub(super) fn new(unix_leap_time: i64, local_time_type_index: usize) -> Self { + pub(super) const fn new(unix_leap_time: i64, local_time_type_index: usize) -> Self { Self { unix_leap_time, local_time_type_index } } /// Returns Unix leap time - fn unix_leap_time(&self) -> i64 { + const fn unix_leap_time(&self) -> i64 { self.unix_leap_time } } @@ -459,12 +459,12 @@ pub(super) struct LeapSecond { impl LeapSecond { /// Construct a TZif file leap second - pub(super) fn new(unix_leap_time: i64, correction: i32) -> Self { + pub(super) const fn new(unix_leap_time: i64, correction: i32) -> Self { Self { unix_leap_time, correction } } /// Returns Unix leap time - fn unix_leap_time(&self) -> i64 { + const fn unix_leap_time(&self) -> i64 { self.unix_leap_time } } @@ -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")); } @@ -572,12 +572,12 @@ impl LocalTimeType { } /// Returns offset from UTC in seconds - pub(crate) fn offset(&self) -> i32 { + pub(crate) const fn offset(&self) -> i32 { self.ut_offset } /// Returns daylight saving time indicator - pub(super) fn is_dst(&self) -> bool { + pub(super) const fn is_dst(&self) -> bool { self.is_dst } @@ -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/time_delta.rs b/src/time_delta.rs index 58f2ff5ab8..1f4a4cb5dd 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -120,7 +120,7 @@ impl TimeDelta { /// Makes a new `Duration` with given number of milliseconds. #[inline] - pub fn milliseconds(milliseconds: i64) -> TimeDelta { + pub const fn milliseconds(milliseconds: i64) -> TimeDelta { let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC); let nanos = millis as i32 * NANOS_PER_MILLI; TimeDelta { secs, nanos } @@ -128,7 +128,7 @@ impl TimeDelta { /// Makes a new `Duration` with given number of microseconds. #[inline] - pub fn microseconds(microseconds: i64) -> TimeDelta { + pub const fn microseconds(microseconds: i64) -> TimeDelta { let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC); let nanos = micros as i32 * NANOS_PER_MICRO; TimeDelta { secs, nanos } @@ -136,36 +136,36 @@ impl TimeDelta { /// Makes a new `Duration` with given number of nanoseconds. #[inline] - pub fn nanoseconds(nanos: i64) -> TimeDelta { + pub const fn nanoseconds(nanos: i64) -> TimeDelta { let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64); TimeDelta { secs, nanos: nanos as i32 } } /// Returns the total number of whole weeks in the duration. #[inline] - pub fn num_weeks(&self) -> i64 { + pub const fn num_weeks(&self) -> i64 { self.num_days() / 7 } /// Returns the total number of whole days in the duration. - pub fn num_days(&self) -> i64 { + pub const fn num_days(&self) -> i64 { self.num_seconds() / SECS_PER_DAY } /// Returns the total number of whole hours in the duration. #[inline] - pub fn num_hours(&self) -> i64 { + pub const fn num_hours(&self) -> i64 { self.num_seconds() / SECS_PER_HOUR } /// Returns the total number of whole minutes in the duration. #[inline] - pub fn num_minutes(&self) -> i64 { + pub const fn num_minutes(&self) -> i64 { self.num_seconds() / SECS_PER_MINUTE } /// Returns the total number of whole seconds in the duration. - pub fn num_seconds(&self) -> i64 { + pub const fn num_seconds(&self) -> i64 { // If secs is negative, nanos should be subtracted from the duration. if self.secs < 0 && self.nanos > 0 { self.secs + 1 @@ -177,7 +177,7 @@ impl TimeDelta { /// Returns the number of nanoseconds such that /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of /// nanoseconds in the duration. - fn nanos_mod_sec(&self) -> i32 { + const fn nanos_mod_sec(&self) -> i32 { if self.secs < 0 && self.nanos > 0 { self.nanos - NANOS_PER_SEC } else { @@ -186,7 +186,7 @@ impl TimeDelta { } /// Returns the total number of whole milliseconds in the duration, - pub fn num_milliseconds(&self) -> i64 { + pub const fn num_milliseconds(&self) -> i64 { // A proper Duration will not overflow, because MIN and MAX are defined // such that the range is exactly i64 milliseconds. let secs_part = self.num_seconds() * MILLIS_PER_SEC; @@ -196,7 +196,7 @@ impl TimeDelta { /// Returns the total number of whole microseconds in the duration, /// or `None` on overflow (exceeding 2^63 microseconds in either direction). - pub fn num_microseconds(&self) -> Option { + pub const fn num_microseconds(&self) -> Option { let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC)); let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO; secs_part.checked_add(nanos_part as i64) @@ -204,7 +204,7 @@ impl TimeDelta { /// Returns the total number of whole nanoseconds in the duration, /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction). - pub fn num_nanoseconds(&self) -> Option { + pub const fn num_nanoseconds(&self) -> Option { let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64)); let nanos_part = self.nanos_mod_sec(); secs_part.checked_add(nanos_part as i64) @@ -248,7 +248,7 @@ impl TimeDelta { /// Returns the duration as an absolute (non-negative) value. #[inline] - pub fn abs(&self) -> TimeDelta { + pub const fn abs(&self) -> TimeDelta { if self.secs < 0 && self.nanos != 0 { TimeDelta { secs: (self.secs + 1).abs(), nanos: NANOS_PER_SEC - self.nanos } } else { @@ -258,25 +258,25 @@ impl TimeDelta { /// The minimum possible `Duration`: `i64::MIN` milliseconds. #[inline] - pub fn min_value() -> TimeDelta { + pub const fn min_value() -> TimeDelta { MIN } /// The maximum possible `Duration`: `i64::MAX` milliseconds. #[inline] - pub fn max_value() -> TimeDelta { + pub const fn max_value() -> TimeDelta { MAX } /// A duration where the stored seconds and nanoseconds are equal to zero. #[inline] - pub fn zero() -> TimeDelta { + pub const fn zero() -> TimeDelta { TimeDelta { secs: 0, nanos: 0 } } /// Returns `true` if the duration equals `Duration::zero()`. #[inline] - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.secs == 0 && self.nanos == 0 } @@ -458,12 +458,12 @@ impl Error for OutOfRangeError { // Copied from libnum #[inline] -fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) { +const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) { (div_floor_64(this, other), mod_floor_64(this, other)) } #[inline] -fn div_floor_64(this: i64, other: i64) -> i64 { +const fn div_floor_64(this: i64, other: i64) -> i64 { match div_rem_64(this, other) { (d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1, (d, _) => d, @@ -471,7 +471,7 @@ fn div_floor_64(this: i64, other: i64) -> i64 { } #[inline] -fn mod_floor_64(this: i64, other: i64) -> i64 { +const fn mod_floor_64(this: i64, other: i64) -> i64 { match this % other { r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other, r => r, @@ -479,7 +479,7 @@ fn mod_floor_64(this: i64, other: i64) -> i64 { } #[inline] -fn div_rem_64(this: i64, other: i64) -> (i64, i64) { +const fn div_rem_64(this: i64, other: i64) -> (i64, i64) { (this / other, this % other) } diff --git a/src/weekday.rs b/src/weekday.rs index 09edd408a7..90596030da 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, @@ -73,7 +73,7 @@ impl Weekday { /// ------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.number_from_monday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 7 #[inline] - pub fn number_from_monday(&self) -> u32 { + pub const fn number_from_monday(&self) -> u32 { match *self { Weekday::Mon => 1, Weekday::Tue => 2, @@ -91,7 +91,7 @@ impl Weekday { /// ------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.number_from_sunday()`: | 2 | 3 | 4 | 5 | 6 | 7 | 1 #[inline] - pub fn number_from_sunday(&self) -> u32 { + pub const fn number_from_sunday(&self) -> u32 { match *self { Weekday::Mon => 2, Weekday::Tue => 3, @@ -109,7 +109,7 @@ impl Weekday { /// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.num_days_from_monday()`: | 0 | 1 | 2 | 3 | 4 | 5 | 6 #[inline] - pub fn num_days_from_monday(&self) -> u32 { + pub const fn num_days_from_monday(&self) -> u32 { match *self { Weekday::Mon => 0, Weekday::Tue => 1, @@ -127,7 +127,7 @@ impl Weekday { /// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.num_days_from_sunday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 0 #[inline] - pub fn num_days_from_sunday(&self) -> u32 { + pub const fn num_days_from_sunday(&self) -> u32 { match *self { Weekday::Mon => 1, Weekday::Tue => 2,