Skip to content

Commit

Permalink
Make some methods on NaiveTime const
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker authored and djc committed Jul 27, 2023
1 parent 307aa6d commit a9cc8e9
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::format::{
};
use crate::oldtime::Duration as OldDuration;
use crate::Timelike;
use crate::{expect, try_opt};

#[cfg(feature = "rustc-serialize")]
mod rustc_serialize;
Expand Down Expand Up @@ -219,8 +220,8 @@ impl NaiveTime {
#[deprecated(since = "0.4.23", note = "use `from_hms_opt()` instead")]
#[inline]
#[must_use]
pub fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime {
NaiveTime::from_hms_opt(hour, min, sec).expect("invalid time")
pub const fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime {
expect!(NaiveTime::from_hms_opt(hour, min, sec), "invalid time")
}

/// Makes a new `NaiveTime` from hour, minute and second.
Expand Down Expand Up @@ -262,8 +263,8 @@ impl NaiveTime {
#[deprecated(since = "0.4.23", note = "use `from_hms_milli_opt()` instead")]
#[inline]
#[must_use]
pub fn from_hms_milli(hour: u32, min: u32, sec: u32, milli: u32) -> NaiveTime {
NaiveTime::from_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
pub const fn from_hms_milli(hour: u32, min: u32, sec: u32, milli: u32) -> NaiveTime {
expect!(NaiveTime::from_hms_milli_opt(hour, min, sec, milli), "invalid time")
}

/// Makes a new `NaiveTime` from hour, minute, second and millisecond.
Expand Down Expand Up @@ -292,10 +293,14 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn from_hms_milli_opt(hour: u32, min: u32, sec: u32, milli: u32) -> Option<NaiveTime> {
milli
.checked_mul(1_000_000)
.and_then(|nano| NaiveTime::from_hms_nano_opt(hour, min, sec, nano))
pub const fn from_hms_milli_opt(
hour: u32,
min: u32,
sec: u32,
milli: u32,
) -> Option<NaiveTime> {
let nano = try_opt!(milli.checked_mul(1_000_000));
NaiveTime::from_hms_nano_opt(hour, min, sec, nano)
}

/// Makes a new `NaiveTime` from hour, minute, second and microsecond.
Expand All @@ -309,8 +314,8 @@ impl NaiveTime {
#[deprecated(since = "0.4.23", note = "use `from_hms_micro_opt()` instead")]
#[inline]
#[must_use]
pub fn from_hms_micro(hour: u32, min: u32, sec: u32, micro: u32) -> NaiveTime {
NaiveTime::from_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
pub const fn from_hms_micro(hour: u32, min: u32, sec: u32, micro: u32) -> NaiveTime {
expect!(NaiveTime::from_hms_micro_opt(hour, min, sec, micro), "invalid time")
}

/// Makes a new `NaiveTime` from hour, minute, second and microsecond.
Expand Down Expand Up @@ -339,8 +344,14 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn from_hms_micro_opt(hour: u32, min: u32, sec: u32, micro: u32) -> Option<NaiveTime> {
micro.checked_mul(1_000).and_then(|nano| NaiveTime::from_hms_nano_opt(hour, min, sec, nano))
pub const fn from_hms_micro_opt(
hour: u32,
min: u32,
sec: u32,
micro: u32,
) -> Option<NaiveTime> {
let nano = try_opt!(micro.checked_mul(1_000));
NaiveTime::from_hms_nano_opt(hour, min, sec, nano)
}

/// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
Expand All @@ -354,8 +365,8 @@ impl NaiveTime {
#[deprecated(since = "0.4.23", note = "use `from_hms_nano_opt()` instead")]
#[inline]
#[must_use]
pub fn from_hms_nano(hour: u32, min: u32, sec: u32, nano: u32) -> NaiveTime {
NaiveTime::from_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
pub const fn from_hms_nano(hour: u32, min: u32, sec: u32, nano: u32) -> NaiveTime {
expect!(NaiveTime::from_hms_nano_opt(hour, min, sec, nano), "invalid time")
}

/// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
Expand Down Expand Up @@ -403,8 +414,8 @@ impl NaiveTime {
#[deprecated(since = "0.4.23", note = "use `from_num_seconds_from_midnight_opt()` instead")]
#[inline]
#[must_use]
pub fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime {
NaiveTime::from_num_seconds_from_midnight_opt(secs, nano).expect("invalid time")
pub const fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime {
expect!(NaiveTime::from_num_seconds_from_midnight_opt(secs, nano), "invalid time")
}

/// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
Expand Down

0 comments on commit a9cc8e9

Please sign in to comment.