From 9ae51c9f0c191f9087e768f1163e9f3546d07d68 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov <stepan.koltsov@gmail.com> Date: Fri, 26 May 2017 19:29:31 +0300 Subject: [PATCH] Implement `truncate`, `clear` for `Bytes` (#128) --- src/bytes.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 4b0ef42..fd0317d 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -618,6 +618,45 @@ impl Bytes { self.split_to(at) } + /// Shortens the buffer, keeping the first `len` bytes and dropping the + /// rest. + /// + /// If `len` is greater than the buffer's current length, this has no + /// effect. + /// + /// The [`split_off`] method can emulate `truncate`, but this causes the + /// excess bytes to be returned instead of dropped. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut buf = Bytes::from(&b"hello world"[..]); + /// buf.truncate(5); + /// assert_eq!(buf, b"hello"[..]); + /// ``` + /// + /// [`split_off`]: #method.split_off + pub fn truncate(&mut self, len: usize) { + self.inner.truncate(len); + } + + /// Clears the buffer, removing all data. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut buf = Bytes::from(&b"hello world"[..]); + /// buf.clear(); + /// assert!(buf.is_empty()); + /// ``` + pub fn clear(&mut self) { + self.truncate(0); + } + /// Attempt to convert into a `BytesMut` handle. /// /// This will only succeed if there are no other outstanding references to @@ -1133,9 +1172,7 @@ impl BytesMut { /// /// [`split_off`]: #method.split_off pub fn truncate(&mut self, len: usize) { - if len <= self.len() { - unsafe { self.set_len(len); } - } + self.inner.truncate(len); } /// Clears the buffer, removing all data. @@ -1709,6 +1746,12 @@ impl Inner { return other } + fn truncate(&mut self, len: usize) { + if len <= self.len() { + unsafe { self.set_len(len); } + } + } + unsafe fn set_start(&mut self, start: usize) { // This function should never be called when the buffer is still backed // by a `Vec<u8>` -- GitLab