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.
This commit is contained in:
Huon Wilson 2013-12-01 00:58:27 +11:00
parent 80991bb578
commit be6ae6eb37

View 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 }
}
}