Skip to content

Commit

Permalink
Properly handle i64::MIN when converting to Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
hollmmax authored and Max Hollmann committed Aug 3, 2023
1 parent 1f465b9 commit 7503651
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions serde_with/src/utils/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,12 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<i64, Strict> {
where
D: Deserializer<'de>,
{
i64::deserialize(deserializer).map(|mut secs: i64| {
let mut sign = Sign::Positive;
if secs.is_negative() {
secs = -secs;
sign = Sign::Negative;
}
DurationSigned::new(sign, secs as u64, 0)
i64::deserialize(deserializer).map(|secs: i64| {
let sign = match secs.is_negative() {
true => Sign::Negative,
false => Sign::Positive,
};
DurationSigned::new(sign, secs.abs_diff(0), 0)
})
}
}
Expand Down Expand Up @@ -398,13 +397,12 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict>
where
E: DeError,
{
let mut secs: i64 = value.parse().map_err(DeError::custom)?;
let mut sign = Sign::Positive;
if secs.is_negative() {
secs = -secs;
sign = Sign::Negative;
}
Ok(DurationSigned::new(sign, secs as u64, 0))
let secs: i64 = value.parse().map_err(DeError::custom)?;
let sign = match secs.is_negative() {
true => Sign::Negative,
false => Sign::Positive,
};
Ok(DurationSigned::new(sign, secs.abs_diff(0), 0))
}
}

Expand Down

0 comments on commit 7503651

Please sign in to comment.