Skip to content

Commit dff533f

Browse files
Improve code by using unsigned_abs
1 parent f74b38a commit dff533f

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

core/src/fmt/num.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,13 @@ macro_rules! impl_Display {
219219
#[stable(feature = "rust1", since = "1.0.0")]
220220
impl fmt::Display for $signed {
221221
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222-
let is_nonnegative = *self >= 0;
223-
224-
if !is_nonnegative {
225-
#[cfg(not(feature = "optimize_for_size"))]
226-
{
227-
// convert the negative num to positive by summing 1 to its 2s complement
228-
return (!self as $unsigned).wrapping_add(1)._fmt(false, f);
229-
}
230-
#[cfg(feature = "optimize_for_size")]
231-
{
232-
// convert the negative num to positive by summing 1 to its 2s complement
233-
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
234-
}
235-
}
236-
237-
// If it's a positive integer.
238222
#[cfg(not(feature = "optimize_for_size"))]
239223
{
240-
(*self as $unsigned)._fmt(true, f)
224+
return self.unsigned_abs()._fmt(*self >= 0, f);
241225
}
242226
#[cfg(feature = "optimize_for_size")]
243227
{
244-
$gen_name(self.$conv_fn(), true, f)
228+
return $gen_name(self.unsigned_abs().$conv_fn(), *self >= 0, f);
245229
}
246230
}
247231
}

0 commit comments

Comments
 (0)