From 882e43203c853528312f414a6e8f79b5cb3b8b79 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/mod.rs | 2 +- src/month.rs | 2 +- src/naive/date.rs | 12 ++++---- src/naive/datetime/mod.rs | 6 ++-- src/naive/internals.rs | 14 +++++----- src/naive/isoweek.rs | 6 ++-- src/offset/fixed.rs | 4 +-- src/offset/local/tz_info/parser.rs | 4 +-- src/offset/local/tz_info/timezone.rs | 12 ++++---- src/oldtime.rs | 42 ++++++++++++++-------------- src/weekday.rs | 10 +++---- 11 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index c6b8eaee09..c05ba4d044 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/month.rs b/src/month.rs index f444dc0270..46f09d0fb8 100644 --- a/src/month.rs +++ b/src/month.rs @@ -197,7 +197,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 46c2092513..64af978f3d 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -131,7 +131,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) } } @@ -708,7 +708,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) } @@ -898,7 +898,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) } @@ -1221,7 +1221,7 @@ impl NaiveDate { /// } /// ``` #[inline] - pub fn iter_days(&self) -> NaiveDateDaysIterator { + pub const fn iter_days(&self) -> NaiveDateDaysIterator { NaiveDateDaysIterator { value: *self } } @@ -1252,14 +1252,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 68a1be20e9..ec0d842c06 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -131,7 +131,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 } } @@ -335,7 +335,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 } @@ -350,7 +350,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 cd87973712..05305b506f 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -141,7 +141,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) } @@ -294,7 +294,7 @@ impl Of { } #[inline] - pub(super) fn ordinal(&self) -> u32 { + pub(super) const fn ordinal(&self) -> u32 { let Of(of) = *self; of >> 4 } @@ -310,7 +310,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) } @@ -336,13 +336,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)) } @@ -398,7 +398,7 @@ impl Mdf { } #[inline] - pub(super) fn month(&self) -> u32 { + pub(super) const fn month(&self) -> u32 { let Mdf(mdf) = *self; mdf >> 9 } @@ -414,7 +414,7 @@ impl Mdf { } #[inline] - pub(super) fn day(&self) -> u32 { + pub(super) const fn day(&self) -> u32 { let Mdf(mdf) = *self; (mdf >> 4) & 0b1_1111 } 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/offset/fixed.rs b/src/offset/fixed.rs index 4d336b9db6..0989dfa5ba 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -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/parser.rs b/src/offset/local/tz_info/parser.rs index 77f8e481b5..5652a0ea95 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,7 +233,7 @@ impl<'a> Cursor<'a> { } /// Returns remaining data - pub(crate) fn remaining(&self) -> &'a [u8] { + pub(crate) const fn remaining(&self) -> &'a [u8] { self.remaining } diff --git a/src/offset/local/tz_info/timezone.rs b/src/offset/local/tz_info/timezone.rs index ae6fb5f848..8572825a89 100644 --- a/src/offset/local/tz_info/timezone.rs +++ b/src/offset/local/tz_info/timezone.rs @@ -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 } } @@ -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 } diff --git a/src/oldtime.rs b/src/oldtime.rs index f604524899..8e2b3d2c09 100644 --- a/src/oldtime.rs +++ b/src/oldtime.rs @@ -120,7 +120,7 @@ impl Duration { /// Makes a new `Duration` with given number of milliseconds. #[inline] - pub fn milliseconds(milliseconds: i64) -> Duration { + pub const fn milliseconds(milliseconds: i64) -> Duration { let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC); let nanos = millis as i32 * NANOS_PER_MILLI; Duration { secs: secs, nanos: nanos } @@ -128,7 +128,7 @@ impl Duration { /// Makes a new `Duration` with given number of microseconds. #[inline] - pub fn microseconds(microseconds: i64) -> Duration { + pub const fn microseconds(microseconds: i64) -> Duration { let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC); let nanos = micros as i32 * NANOS_PER_MICRO; Duration { secs: secs, nanos: nanos } @@ -136,36 +136,36 @@ impl Duration { /// Makes a new `Duration` with given number of nanoseconds. #[inline] - pub fn nanoseconds(nanos: i64) -> Duration { + pub const fn nanoseconds(nanos: i64) -> Duration { let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64); Duration { secs: 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 Duration { /// 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 Duration { } /// 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 Duration { /// 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 Duration { /// 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 Duration { /// Returns the duration as an absolute (non-negative) value. #[inline] - pub fn abs(&self) -> Duration { + pub const fn abs(&self) -> Duration { if self.secs < 0 && self.nanos != 0 { Duration { secs: (self.secs + 1).abs(), nanos: NANOS_PER_SEC - self.nanos } } else { @@ -258,25 +258,25 @@ impl Duration { /// The minimum possible `Duration`: `i64::MIN` milliseconds. #[inline] - pub fn min_value() -> Duration { + pub const fn min_value() -> Duration { MIN } /// The maximum possible `Duration`: `i64::MAX` milliseconds. #[inline] - pub fn max_value() -> Duration { + pub const fn max_value() -> Duration { MAX } /// A duration where the stored seconds and nanoseconds are equal to zero. #[inline] - pub fn zero() -> Duration { + pub const fn zero() -> Duration { Duration { 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 } @@ -457,12 +457,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, @@ -470,7 +470,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, @@ -478,7 +478,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 c12dd01c64..72e384673f 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -72,7 +72,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 { self.num_days_from(Weekday::Mon) + 1 } @@ -82,7 +82,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 { self.num_days_from(Weekday::Sun) + 1 } @@ -92,7 +92,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 { self.num_days_from(Weekday::Mon) } @@ -102,7 +102,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 { self.num_days_from(Weekday::Sun) } @@ -112,7 +112,7 @@ impl Weekday { /// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- /// `w.num_days_from(wd)`: | 0 | 1 | 2 | 3 | 4 | 5 | 6 #[inline] - pub(crate) fn num_days_from(&self, day: Weekday) -> u32 { + pub(crate) const fn num_days_from(&self, day: Weekday) -> u32 { (*self as u32 + 7 - day as u32) % 7 } }