Skip to content

Commit

Permalink
Remove unnecessary try_opt!() macro
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Aug 8, 2022
1 parent 0b7feac commit a185d3b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<Tz: TimeZone> Date<Tz> {
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
let date = try_opt!(self.date.checked_add_signed(rhs));
let date = self.date.checked_add_signed(rhs)?;
Some(Date { date, offset: self.offset })
}

Expand All @@ -250,7 +250,7 @@ impl<Tz: TimeZone> Date<Tz> {
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
let date = try_opt!(self.date.checked_sub_signed(rhs));
let date = self.date.checked_sub_signed(rhs)?;
Some(Date { date, offset: self.offset })
}

Expand Down
4 changes: 2 additions & 2 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
let datetime = try_opt!(self.datetime.checked_add_signed(rhs));
let datetime = self.datetime.checked_add_signed(rhs)?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
}
Expand All @@ -334,7 +334,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
let datetime = try_opt!(self.datetime.checked_sub_signed(rhs));
let datetime = self.datetime.checked_sub_signed(rhs)?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
}
Expand Down
10 changes: 0 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,6 @@ pub mod prelude {
pub use crate::{Offset, TimeZone};
}

// useful throughout the codebase
macro_rules! try_opt {
($e:expr) => {
match $e {
Some(v) => v,
None => return None,
}
};
}

mod date;
#[allow(deprecated)]
pub use date::{Date, MAX_DATE, MIN_DATE};
Expand Down
4 changes: 2 additions & 2 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ impl NaiveDate {
let year = self.year();
let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
let cycle = try_opt!((cycle as i32).checked_add(try_opt!(rhs.num_days().to_i32())));
let cycle = (cycle as i32).checked_add(rhs.num_days().to_i32()?)?;
let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146_097);
year_div_400 += cycle_div_400y;

Expand Down Expand Up @@ -1084,7 +1084,7 @@ impl NaiveDate {
let year = self.year();
let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
let cycle = try_opt!((cycle as i32).checked_sub(try_opt!(rhs.num_days().to_i32())));
let cycle = (cycle as i32).checked_sub(rhs.num_days().to_i32()?)?;
let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146_097);
year_div_400 += cycle_div_400y;

Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl NaiveDateTime {
return None;
}

let date = try_opt!(self.date.checked_add_signed(OldDuration::seconds(rhs)));
let date = self.date.checked_add_signed(OldDuration::seconds(rhs))?;
Some(NaiveDateTime { date, time })
}

Expand Down Expand Up @@ -602,7 +602,7 @@ impl NaiveDateTime {
return None;
}

let date = try_opt!(self.date.checked_sub_signed(OldDuration::seconds(rhs)));
let date = self.date.checked_sub_signed(OldDuration::seconds(rhs))?;
Some(NaiveDateTime { date, time })
}

Expand Down

0 comments on commit a185d3b

Please sign in to comment.