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

5 comments on commit be6ae6e

@bors
Copy link
Contributor

@bors bors commented on be6ae6e Nov 30, 2013

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at huonw@be6ae6e

@bors
Copy link
Contributor

@bors bors commented on be6ae6e Nov 30, 2013

Choose a reason for hiding this comment

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

merging huonw/rust/with-cap = be6ae6e into auto

@bors
Copy link
Contributor

@bors bors commented on be6ae6e Nov 30, 2013

Choose a reason for hiding this comment

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

huonw/rust/with-cap = be6ae6e merged ok, testing candidate = dfe46f7

@bors
Copy link
Contributor

@bors bors commented on be6ae6e Nov 30, 2013

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on be6ae6e Nov 30, 2013

Choose a reason for hiding this comment

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

fast-forwarding master to auto = dfe46f7

Please sign in to comment.