Skip to content

Commit

Permalink
Turn expect macro into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 18, 2024
1 parent 236b7ad commit f8b62ce
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[inline]
#[must_use]
pub const fn timestamp_nanos(&self) -> i64 {
expect!(
expect(

Check warning on line 282 in src/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/mod.rs#L282

Added line #L282 was not covered by tests
self.timestamp_nanos_opt(),
"value can not be represented in a timestamp with nanosecond precision."
)
Expand Down Expand Up @@ -857,7 +857,7 @@ impl DateTime<Utc> {
pub const fn from_timestamp_nanos(nanos: i64) -> Self {
let secs = nanos.div_euclid(1_000_000_000);
let nsecs = nanos.rem_euclid(1_000_000_000) as u32;
expect!(Self::from_timestamp(secs, nsecs), "timestamp in nanos is always in range")
expect(Self::from_timestamp(secs, nsecs), "timestamp in nanos is always in range")
}

/// The Unix Epoch, 1970-01-01 00:00:00 UTC.
Expand Down
14 changes: 5 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,11 @@ macro_rules! try_opt {
}

/// Workaround because `.expect()` is not (yet) available in const context.
#[macro_export]
#[doc(hidden)]
macro_rules! expect {
($e:expr, $m:literal) => {
match $e {
Some(v) => v,
None => panic!($m),
}
};
pub(crate) const fn expect<T: Copy>(opt: Option<T>, msg: &str) -> T {
match opt {
Some(val) => val,
None => panic!("{}", msg),
}
}

#[cfg(test)]
Expand Down
24 changes: 12 additions & 12 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl NaiveDate {
#[deprecated(since = "0.4.23", note = "use `from_ymd_opt()` instead")]
#[must_use]
pub const fn from_ymd(year: i32, month: u32, day: u32) -> NaiveDate {
expect!(NaiveDate::from_ymd_opt(year, month, day), "invalid or out-of-range date")
expect(NaiveDate::from_ymd_opt(year, month, day), "invalid or out-of-range date")

Check warning on line 168 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L168

Added line #L168 was not covered by tests
}

/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
Expand Down Expand Up @@ -213,7 +213,7 @@ impl NaiveDate {
#[deprecated(since = "0.4.23", note = "use `from_yo_opt()` instead")]
#[must_use]
pub const fn from_yo(year: i32, ordinal: u32) -> NaiveDate {
expect!(NaiveDate::from_yo_opt(year, ordinal), "invalid or out-of-range date")
expect(NaiveDate::from_yo_opt(year, ordinal), "invalid or out-of-range date")

Check warning on line 216 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L216

Added line #L216 was not covered by tests
}

/// Makes a new `NaiveDate` from the [ordinal date](#ordinal-date)
Expand Down Expand Up @@ -258,7 +258,7 @@ impl NaiveDate {
#[deprecated(since = "0.4.23", note = "use `from_isoywd_opt()` instead")]
#[must_use]
pub const fn from_isoywd(year: i32, week: u32, weekday: Weekday) -> NaiveDate {
expect!(NaiveDate::from_isoywd_opt(year, week, weekday), "invalid or out-of-range date")
expect(NaiveDate::from_isoywd_opt(year, week, weekday), "invalid or out-of-range date")

Check warning on line 261 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L261

Added line #L261 was not covered by tests
}

/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
Expand Down Expand Up @@ -349,7 +349,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn from_num_days_from_ce(days: i32) -> NaiveDate {
expect!(NaiveDate::from_num_days_from_ce_opt(days), "out-of-range date")
expect(NaiveDate::from_num_days_from_ce_opt(days), "out-of-range date")

Check warning on line 352 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L352

Added line #L352 was not covered by tests
}

/// Makes a new `NaiveDate` from a day's number in the proleptic Gregorian calendar, with
Expand Down Expand Up @@ -402,7 +402,7 @@ impl NaiveDate {
weekday: Weekday,
n: u8,
) -> NaiveDate {
expect!(NaiveDate::from_weekday_of_month_opt(year, month, weekday, n), "out-of-range date")
expect(NaiveDate::from_weekday_of_month_opt(year, month, weekday, n), "out-of-range date")

Check warning on line 405 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L405

Added line #L405 was not covered by tests
}

/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
Expand Down Expand Up @@ -752,7 +752,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime {
expect!(self.and_hms_opt(hour, min, sec), "invalid time")
expect(self.and_hms_opt(hour, min, sec), "invalid time")

Check warning on line 755 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L755

Added line #L755 was not covered by tests
}

/// Makes a new `NaiveDateTime` from the current date, hour, minute and second.
Expand Down Expand Up @@ -794,7 +794,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> NaiveDateTime {
expect!(self.and_hms_milli_opt(hour, min, sec, milli), "invalid time")
expect(self.and_hms_milli_opt(hour, min, sec, milli), "invalid time")

Check warning on line 797 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L797

Added line #L797 was not covered by tests
}

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
Expand Down Expand Up @@ -858,7 +858,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> NaiveDateTime {
expect!(self.and_hms_micro_opt(hour, min, sec, micro), "invalid time")
expect(self.and_hms_micro_opt(hour, min, sec, micro), "invalid time")

Check warning on line 861 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L861

Added line #L861 was not covered by tests
}

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
Expand Down Expand Up @@ -908,7 +908,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> NaiveDateTime {
expect!(self.and_hms_nano_opt(hour, min, sec, nano), "invalid time")
expect(self.and_hms_nano_opt(hour, min, sec, nano), "invalid time")

Check warning on line 911 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L911

Added line #L911 was not covered by tests
}

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
Expand Down Expand Up @@ -975,7 +975,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn succ(&self) -> NaiveDate {
expect!(self.succ_opt(), "out of bound")
expect(self.succ_opt(), "out of bound")

Check warning on line 978 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L978

Added line #L978 was not covered by tests
}

/// Makes a new `NaiveDate` for the next calendar date.
Expand Down Expand Up @@ -1014,7 +1014,7 @@ impl NaiveDate {
#[inline]
#[must_use]
pub const fn pred(&self) -> NaiveDate {
expect!(self.pred_opt(), "out of bound")
expect(self.pred_opt(), "out of bound")

Check warning on line 1017 in src/naive/date/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/date/mod.rs#L1017

Added line #L1017 was not covered by tests
}

/// Makes a new `NaiveDate` for the previous calendar date.
Expand Down Expand Up @@ -1158,7 +1158,7 @@ impl NaiveDate {
let days = (year1_div_400 as i64 - year2_div_400 as i64) * 146_097 + (cycle1 - cycle2);
// The range of `TimeDelta` is ca. 585 million years, the range of `NaiveDate` ca. 525.000
// years.
expect!(TimeDelta::try_days(days), "always in range")
expect(TimeDelta::try_days(days), "always in range")
}

/// Returns the number of whole years from the given `base` until `self`.
Expand Down
6 changes: 3 additions & 3 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl NaiveDateTime {
#[must_use]
pub const fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime {
let datetime =
expect!(DateTime::from_timestamp(secs, nsecs), "invalid or out-of-range datetime");
expect(DateTime::from_timestamp(secs, nsecs), "invalid or out-of-range datetime");

Check warning on line 126 in src/naive/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/datetime/mod.rs#L126

Added line #L126 was not covered by tests
datetime.naive_utc()
}

Expand Down Expand Up @@ -818,7 +818,7 @@ impl NaiveDateTime {
/// ```
#[must_use]
pub const fn signed_duration_since(self, rhs: NaiveDateTime) -> TimeDelta {
expect!(
expect(
self.date
.signed_duration_since(rhs.date)
.checked_add(&self.time.signed_duration_since(rhs.time)),
Expand Down Expand Up @@ -956,7 +956,7 @@ impl NaiveDateTime {

/// The Unix Epoch, 1970-01-01 00:00:00.
pub const UNIX_EPOCH: Self =
expect!(NaiveDate::from_ymd_opt(1970, 1, 1), "").and_time(NaiveTime::MIN);
expect(NaiveDate::from_ymd_opt(1970, 1, 1), "").and_time(NaiveTime::MIN);
}

impl From<NaiveDate> for NaiveDateTime {
Expand Down
4 changes: 2 additions & 2 deletions src/naive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl NaiveWeek {
// Do not construct an intermediate date beyond `self.date`, because that may be out of
// range if `date` is close to `NaiveDate::MAX`.
let days = start - ref_day - if start > ref_day { 7 } else { 0 };
expect!(self.date.add_days(days), "first weekday out of range for `NaiveDate`")
expect(self.date.add_days(days), "first weekday out of range for `NaiveDate`")
}

/// Returns a date representing the last day of the week.
Expand Down Expand Up @@ -97,7 +97,7 @@ impl NaiveWeek {
// Do not construct an intermediate date before `self.date` (like with `first_day()`),
// because that may be out of range if `date` is close to `NaiveDate::MIN`.
let days = end - ref_day + if end < ref_day { 7 } else { 0 };
expect!(self.date.add_days(days), "last weekday out of range for `NaiveDate`")
expect(self.date.add_days(days), "last weekday out of range for `NaiveDate`")
}

/// Returns a [`RangeInclusive<T>`] representing the whole week bounded by
Expand Down
12 changes: 6 additions & 6 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl NaiveTime {
#[inline]
#[must_use]
pub const fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime {
expect!(NaiveTime::from_hms_opt(hour, min, sec), "invalid time")
expect(NaiveTime::from_hms_opt(hour, min, sec), "invalid time")

Check warning on line 259 in src/naive/time/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/time/mod.rs#L259

Added line #L259 was not covered by tests
}

/// Makes a new `NaiveTime` from hour, minute and second.
Expand Down Expand Up @@ -299,7 +299,7 @@ impl NaiveTime {
#[inline]
#[must_use]
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")
expect(NaiveTime::from_hms_milli_opt(hour, min, sec, milli), "invalid time")

Check warning on line 302 in src/naive/time/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/time/mod.rs#L302

Added line #L302 was not covered by tests
}

/// Makes a new `NaiveTime` from hour, minute, second and millisecond.
Expand Down Expand Up @@ -350,7 +350,7 @@ impl NaiveTime {
#[inline]
#[must_use]
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")
expect(NaiveTime::from_hms_micro_opt(hour, min, sec, micro), "invalid time")

Check warning on line 353 in src/naive/time/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/time/mod.rs#L353

Added line #L353 was not covered by tests
}

/// Makes a new `NaiveTime` from hour, minute, second and microsecond.
Expand Down Expand Up @@ -401,7 +401,7 @@ impl NaiveTime {
#[inline]
#[must_use]
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")
expect(NaiveTime::from_hms_nano_opt(hour, min, sec, nano), "invalid time")

Check warning on line 404 in src/naive/time/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/time/mod.rs#L404

Added line #L404 was not covered by tests
}

/// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
Expand Down Expand Up @@ -453,7 +453,7 @@ impl NaiveTime {
#[inline]
#[must_use]
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")
expect(NaiveTime::from_num_seconds_from_midnight_opt(secs, nano), "invalid time")

Check warning on line 456 in src/naive/time/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/time/mod.rs#L456

Added line #L456 was not covered by tests
}

/// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
Expand Down Expand Up @@ -767,7 +767,7 @@ impl NaiveTime {
let secs_from_frac = frac.div_euclid(1_000_000_000);
let frac = frac.rem_euclid(1_000_000_000) as u32;

expect!(TimeDelta::new(secs + secs_from_frac, frac), "must be in range")
expect(TimeDelta::new(secs + secs_from_frac, frac), "must be in range")
}

/// Adds given `FixedOffset` to the current time, and returns the number of days that should be
Expand Down
24 changes: 12 additions & 12 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl TimeDelta {
#[must_use]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_weeks` instead")]
pub const fn weeks(weeks: i64) -> TimeDelta {
expect!(TimeDelta::try_weeks(weeks), "TimeDelta::weeks out of bounds")
expect(TimeDelta::try_weeks(weeks), "TimeDelta::weeks out of bounds")
}

/// Makes a new `TimeDelta` with the given number of weeks.
Expand Down Expand Up @@ -132,7 +132,7 @@ impl TimeDelta {
#[must_use]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_days` instead")]
pub const fn days(days: i64) -> TimeDelta {
expect!(TimeDelta::try_days(days), "TimeDelta::days out of bounds")
expect(TimeDelta::try_days(days), "TimeDelta::days out of bounds")
}

/// Makes a new `TimeDelta` with the given number of days.
Expand All @@ -159,7 +159,7 @@ impl TimeDelta {
#[must_use]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_hours` instead")]
pub const fn hours(hours: i64) -> TimeDelta {
expect!(TimeDelta::try_hours(hours), "TimeDelta::hours out of bounds")
expect(TimeDelta::try_hours(hours), "TimeDelta::hours out of bounds")

Check warning on line 162 in src/time_delta.rs

View check run for this annotation

Codecov / codecov/patch

src/time_delta.rs#L162

Added line #L162 was not covered by tests
}

/// Makes a new `TimeDelta` with the given number of hours.
Expand All @@ -185,7 +185,7 @@ impl TimeDelta {
#[must_use]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_minutes` instead")]
pub const fn minutes(minutes: i64) -> TimeDelta {
expect!(TimeDelta::try_minutes(minutes), "TimeDelta::minutes out of bounds")
expect(TimeDelta::try_minutes(minutes), "TimeDelta::minutes out of bounds")

Check warning on line 188 in src/time_delta.rs

View check run for this annotation

Codecov / codecov/patch

src/time_delta.rs#L188

Added line #L188 was not covered by tests
}

/// Makes a new `TimeDelta` with the given number of minutes.
Expand All @@ -210,7 +210,7 @@ impl TimeDelta {
#[must_use]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_seconds` instead")]
pub const fn seconds(seconds: i64) -> TimeDelta {
expect!(TimeDelta::try_seconds(seconds), "TimeDelta::seconds out of bounds")
expect(TimeDelta::try_seconds(seconds), "TimeDelta::seconds out of bounds")
}

/// Makes a new `TimeDelta` with the given number of seconds.
Expand All @@ -234,7 +234,7 @@ impl TimeDelta {
#[inline]
#[deprecated(since = "0.4.35", note = "Use `TimeDelta::try_milliseconds` instead")]
pub const fn milliseconds(milliseconds: i64) -> TimeDelta {
expect!(TimeDelta::try_milliseconds(milliseconds), "TimeDelta::milliseconds out of bounds")
expect(TimeDelta::try_milliseconds(milliseconds), "TimeDelta::milliseconds out of bounds")
}

/// Makes a new `TimeDelta` with the given number of milliseconds.
Expand Down Expand Up @@ -1244,12 +1244,12 @@ mod tests {

#[test]
fn test_duration_const() {
const ONE_WEEK: TimeDelta = expect!(TimeDelta::try_weeks(1), "");
const ONE_DAY: TimeDelta = expect!(TimeDelta::try_days(1), "");
const ONE_HOUR: TimeDelta = expect!(TimeDelta::try_hours(1), "");
const ONE_MINUTE: TimeDelta = expect!(TimeDelta::try_minutes(1), "");
const ONE_SECOND: TimeDelta = expect!(TimeDelta::try_seconds(1), "");
const ONE_MILLI: TimeDelta = expect!(TimeDelta::try_milliseconds(1), "");
const ONE_WEEK: TimeDelta = expect(TimeDelta::try_weeks(1), "");
const ONE_DAY: TimeDelta = expect(TimeDelta::try_days(1), "");
const ONE_HOUR: TimeDelta = expect(TimeDelta::try_hours(1), "");
const ONE_MINUTE: TimeDelta = expect(TimeDelta::try_minutes(1), "");
const ONE_SECOND: TimeDelta = expect(TimeDelta::try_seconds(1), "");
const ONE_MILLI: TimeDelta = expect(TimeDelta::try_milliseconds(1), "");
const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1);
const ONE_NANO: TimeDelta = TimeDelta::nanoseconds(1);
let combo: TimeDelta = ONE_WEEK
Expand Down

0 comments on commit f8b62ce

Please sign in to comment.