Ensure inner buffer is empty for generic writer

This commit is contained in:
James Dyson 2019-01-21 14:12:27 +00:00
parent 0c2768068c
commit 4b47a96acd

View File

@ -11,7 +11,10 @@ impl<T> From<T> for GenericBufferWriter<T>
where T: GenericWriteBuffer + GenericReadBuffer where T: GenericWriteBuffer + GenericReadBuffer
{ {
fn from(buf: T) -> Self { fn from(buf: T) -> Self {
Self { buf } let mut gen_buf = Self { buf };
// Ensure the buffer is empty.
gen_buf.clear();
gen_buf
} }
} }
@ -22,10 +25,6 @@ impl<T> GenericBufferWriter<T>
self.buf self.buf
} }
pub fn clear(&mut self) {
self.buf.clear();
}
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.buf.as_slice()) } unsafe { core::str::from_utf8_unchecked(self.buf.as_slice()) }
} }
@ -34,6 +33,11 @@ impl<T> GenericBufferWriter<T>
pub fn to_string(self) -> String { pub fn to_string(self) -> String {
unsafe { String::from_utf8_unchecked(self.buf.to_vec()) } unsafe { String::from_utf8_unchecked(self.buf.to_vec()) }
} }
#[inline]
pub fn clear(&mut self) {
self.buf.clear();
}
} }
impl<T> Write for &mut GenericBufferWriter<T> impl<T> Write for &mut GenericBufferWriter<T>