From 4b47a96acd52fb2bc8f107fb970afc8bd1adf0eb Mon Sep 17 00:00:00 2001 From: James Dyson Date: Mon, 21 Jan 2019 14:12:27 +0000 Subject: [PATCH] Ensure inner buffer is empty for generic writer --- sehn/src/utils/writer.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sehn/src/utils/writer.rs b/sehn/src/utils/writer.rs index 4a70ba3..e283ee6 100644 --- a/sehn/src/utils/writer.rs +++ b/sehn/src/utils/writer.rs @@ -11,7 +11,10 @@ impl From for GenericBufferWriter where T: GenericWriteBuffer + GenericReadBuffer { 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 GenericBufferWriter self.buf } - pub fn clear(&mut self) { - self.buf.clear(); - } - pub fn as_str(&self) -> &str { unsafe { core::str::from_utf8_unchecked(self.buf.as_slice()) } } @@ -34,6 +33,11 @@ impl GenericBufferWriter pub fn to_string(self) -> String { unsafe { String::from_utf8_unchecked(self.buf.to_vec()) } } + + #[inline] + pub fn clear(&mut self) { + self.buf.clear(); + } } impl Write for &mut GenericBufferWriter