Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Add MemWriter.clear() to empty the underlying buffer. #18882

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libstd/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,26 @@ impl MemWriter {
pub fn new() -> MemWriter {
MemWriter::with_capacity(BUF_CAPACITY)
}

/// Create a new `MemWriter`, allocating at least `n` bytes for
/// the internal buffer.
#[inline]
pub fn with_capacity(n: uint) -> MemWriter {
MemWriter::from_vec(Vec::with_capacity(n))
}

/// Create a new `MemWriter` that will append to an existing `Vec`.
#[inline]
pub fn from_vec(buf: Vec<u8>) -> MemWriter {
MemWriter { buf: buf }
}

/// Clear the underlying buffer.
#[inline]
pub fn clear(&mut self) {
self.buf.clear();
}

/// Acquires an immutable reference to the underlying buffer of this
/// `MemWriter`.
#[inline]
Expand Down Expand Up @@ -351,6 +359,9 @@ mod test {
writer.write([4, 5, 6, 7]).unwrap();
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
assert_eq!(writer.get_ref(), b);

writer.clear();
assert_eq!(writer.get_ref(), &[]);
}

#[test]
Expand Down