Skip to content

Commit 7503651

Browse files
hollmmaxMax Hollmann
authored and
Max Hollmann
committed
Properly handle i64::MIN when converting to Duration
1 parent 1f465b9 commit 7503651

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

serde_with/src/utils/duration.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,12 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<i64, Strict> {
356356
where
357357
D: Deserializer<'de>,
358358
{
359-
i64::deserialize(deserializer).map(|mut secs: i64| {
360-
let mut sign = Sign::Positive;
361-
if secs.is_negative() {
362-
secs = -secs;
363-
sign = Sign::Negative;
364-
}
365-
DurationSigned::new(sign, secs as u64, 0)
359+
i64::deserialize(deserializer).map(|secs: i64| {
360+
let sign = match secs.is_negative() {
361+
true => Sign::Negative,
362+
false => Sign::Positive,
363+
};
364+
DurationSigned::new(sign, secs.abs_diff(0), 0)
366365
})
367366
}
368367
}
@@ -398,13 +397,12 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict>
398397
where
399398
E: DeError,
400399
{
401-
let mut secs: i64 = value.parse().map_err(DeError::custom)?;
402-
let mut sign = Sign::Positive;
403-
if secs.is_negative() {
404-
secs = -secs;
405-
sign = Sign::Negative;
406-
}
407-
Ok(DurationSigned::new(sign, secs as u64, 0))
400+
let secs: i64 = value.parse().map_err(DeError::custom)?;
401+
let sign = match secs.is_negative() {
402+
true => Sign::Negative,
403+
false => Sign::Positive,
404+
};
405+
Ok(DurationSigned::new(sign, secs.abs_diff(0), 0))
408406
}
409407
}
410408

0 commit comments

Comments
 (0)