]> git.lizzy.rs Git - rust.git/commitdiff
remove the wrapping arithmetics
authorMichał Krasnoborski <mkrdln@gmail.com>
Thu, 2 Feb 2017 02:38:12 +0000 (03:38 +0100)
committerMichał Krasnoborski <mkrdln@gmail.com>
Thu, 2 Feb 2017 02:38:12 +0000 (03:38 +0100)
src/libcore/fmt/mod.rs

index a870b6f88fb8385db163313a9101fa803ca03004..a989f914db616dbd6aedc179d37fa7cddca6c3b5 100644 (file)
@@ -274,16 +274,12 @@ pub fn new_v1_formatted(pieces: &'a [&'a str],
     #[unstable(feature = "fmt_internals", reason = "internal to format_args!",
                issue = "0")]
     pub fn estimated_capacity(&self) -> usize {
-        // Using wrapping arithmetics in this function, because
-        // wrong result is highly unlikely and doesn't cause unsafety.
-        use ::num::Wrapping as W;
-
-        let pieces_length: W<usize> = self.pieces.iter()
-            .map(|x| W(x.len())).sum();
+        let pieces_length: usize = self.pieces.iter()
+            .map(|x| x.len()).sum();
 
         if self.args.is_empty() {
-            pieces_length.0
-        } else if self.pieces[0] == "" && pieces_length < W(16) {
+            pieces_length
+        } else if self.pieces[0] == "" && pieces_length < 16 {
             // If the format string starts with an argument,
             // don't preallocate anything, unless length
             // of pieces is significant.
@@ -292,9 +288,8 @@ pub fn estimated_capacity(&self) -> usize {
             // There are some arguments, so any additional push
             // will reallocate the string. To avoid that,
             // we're "pre-doubling" the capacity here.
-            (pieces_length * W(2)).0
+            pieces_length.checked_mul(2).unwrap_or(0)
         }
-
     }
 }