Skip to content
Snippets Groups Projects
Commit 9ae51c9f authored by Stepan Koltsov's avatar Stepan Koltsov Committed by Carl Lerche
Browse files

Implement `truncate`, `clear` for `Bytes` (#128)

parent 2b0602e7
No related branches found
No related tags found
No related merge requests found
......@@ -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>`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment