]> git.lizzy.rs Git - rust.git/commitdiff
std::io::mem: add a with_capacity constructor to MemWriter.
authorHuon Wilson <dbau.pp+github@gmail.com>
Sat, 30 Nov 2013 13:58:27 +0000 (00:58 +1100)
committerHuon Wilson <dbau.pp+github@gmail.com>
Sat, 30 Nov 2013 13:58:27 +0000 (00:58 +1100)
This allows one to reduce the number of reallocs of the internal buffer
if one has an approximate idea of the size of the final output.

src/libstd/io/mem.rs

index b08f4af9a54c6c3db981e1801ab8fecbb355429d..aa3eb9a83176333bb2a2a04bad37eea7faa77977 100644 (file)
@@ -27,8 +27,14 @@ pub struct MemWriter {
 }
 
 impl MemWriter {
+    /// Create a new `MemWriter`.
     pub fn new() -> MemWriter {
-        MemWriter { buf: vec::with_capacity(128), pos: 0 }
+        MemWriter::with_capacity(128)
+    }
+    /// Create a new `MemWriter`, allocating at least `n` bytes for
+    /// the internal buffer.
+    pub fn with_capacity(n: uint) -> MemWriter {
+        MemWriter { buf: vec::with_capacity(n), pos: 0 }
     }
 }