Skip to content

Commit

Permalink
Make eligible functions const.
Browse files Browse the repository at this point in the history
  • Loading branch information
tormeh committed Mar 8, 2023
1 parent 43579a5 commit e31192e
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 90 deletions.
16 changes: 8 additions & 8 deletions 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)
}
2 changes: 1 addition & 1 deletion src/format/mod.rs
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/format/scan.rs
Expand Up @@ -222,7 +222,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
Expand Up @@ -498,7 +498,7 @@ pub struct OutOfRange {
}

impl OutOfRange {
fn new() -> OutOfRange {
const fn new() -> OutOfRange {
OutOfRange { _private: () }
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/month.rs
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 Expand Up @@ -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)
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/naive/date.rs
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -1204,7 +1204,7 @@ impl NaiveDate {
/// }
/// ```
#[inline]
pub fn iter_days(&self) -> NaiveDateDaysIterator {
pub const fn iter_days(&self) -> NaiveDateDaysIterator {
NaiveDateDaysIterator { value: *self }
}

Expand Down Expand Up @@ -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 }
}

Expand Down
6 changes: 3 additions & 3 deletions src/naive/datetime/mod.rs
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions src/naive/internals.rs
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<Of> {
pub(super) const fn with_ordinal(&self, ordinal: u32) -> Option<Of> {
if ordinal > 366 {
return None;
}
Expand All @@ -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)
}
Expand All @@ -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))
}
Expand Down Expand Up @@ -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<Mdf> {
pub(super) const fn with_month(&self, month: u32) -> Option<Mdf> {
if month > 12 {
return None;
}
Expand All @@ -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<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/isoweek.rs
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/naive/time/mod.rs
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
8 changes: 4 additions & 4 deletions src/offset/fixed.rs
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 All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/tz_info/mod.rs
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
6 changes: 3 additions & 3 deletions src/offset/local/tz_info/parser.rs
Expand Up @@ -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 }
}

Expand All @@ -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()
}

Expand Down

0 comments on commit e31192e

Please sign in to comment.