about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-12-01 00:58:27 +1100
committerHuon Wilson <dbau.pp+github@gmail.com>2013-12-01 00:58:27 +1100
commitbe6ae6eb373551936fffe83ebe085e4819ab00c0 (patch)
tree263b5667c7a3f2252c9e09d716855301a84ebddb
parent80991bb578329ca921fdc910d9b6b064e8f521d2 (diff)
downloadrust-be6ae6eb373551936fffe83ebe085e4819ab00c0.tar.gz
rust-be6ae6eb373551936fffe83ebe085e4819ab00c0.zip
std::io::mem: add a with_capacity constructor to MemWriter.
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.
-rw-r--r--src/libstd/io/mem.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs
index b08f4af9a54..aa3eb9a8317 100644
--- a/src/libstd/io/mem.rs
+++ b/src/libstd/io/mem.rs
@@ -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 }
     }
 }