Skip to content

Commit

Permalink
Improve code by using wrapping_neg
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 20, 2024
1 parent d318878 commit 75f1ff7
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,14 @@ macro_rules! impl_Display {
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for $signed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let is_nonnegative = *self >= 0;

if !is_nonnegative {
if *self < 0 {
#[cfg(not(feature = "optimize_for_size"))]
{
// convert the negative num to positive by summing 1 to its 2s complement
return (!self as $unsigned).wrapping_add(1)._fmt(false, f);
return (self.wrapping_neg() as $unsigned)._fmt(false, f);
}
#[cfg(feature = "optimize_for_size")]
{
// convert the negative num to positive by summing 1 to its 2s complement
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
return $gen_name(self.wrapping_neg().$conv_fn(), false, f);
}
}

Expand Down

0 comments on commit 75f1ff7

Please sign in to comment.