From be6ae6eb373551936fffe83ebe085e4819ab00c0 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 1 Dec 2013 00:58:27 +1100 Subject: [PATCH] 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. --- src/libstd/io/mem.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index b08f4af9a54c6..aa3eb9a831763 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 } } }