Skip to content

Commit ecda7f3

Browse files
committed
remove the wrapping arithmetics
1 parent 7403ee9 commit ecda7f3

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/libcore/fmt/mod.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,12 @@ impl<'a> Arguments<'a> {
274274
#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
275275
issue = "0")]
276276
pub fn estimated_capacity(&self) -> usize {
277-
// Using wrapping arithmetics in this function, because
278-
// wrong result is highly unlikely and doesn't cause unsafety.
279-
use ::num::Wrapping as W;
280-
281-
let pieces_length: W<usize> = self.pieces.iter()
282-
.map(|x| W(x.len())).sum();
277+
let pieces_length: usize = self.pieces.iter()
278+
.map(|x| x.len()).sum();
283279

284280
if self.args.is_empty() {
285-
pieces_length.0
286-
} else if self.pieces[0] == "" && pieces_length < W(16) {
281+
pieces_length
282+
} else if self.pieces[0] == "" && pieces_length < 16 {
287283
// If the format string starts with an argument,
288284
// don't preallocate anything, unless length
289285
// of pieces is significant.
@@ -292,9 +288,8 @@ impl<'a> Arguments<'a> {
292288
// There are some arguments, so any additional push
293289
// will reallocate the string. To avoid that,
294290
// we're "pre-doubling" the capacity here.
295-
(pieces_length * W(2)).0
291+
pieces_length.checked_mul(2).unwrap_or(0)
296292
}
297-
298293
}
299294
}
300295

0 commit comments

Comments
 (0)