Skip to content

Commit

Permalink
std::io::mem: add a with_capacity constructor to MemWriter.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huonw committed Nov 30, 2013
1 parent 80991bb commit be6ae6e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/libstd/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down

1 comment on commit be6ae6e

@alexcrichton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.