mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-07 00:00:13 +02:00
Implement truncate, clear for Bytes (#128)
This commit is contained in:
committed by
Carl Lerche
parent
2b0602e756
commit
9ae51c9f0c
+46
-3
@@ -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>`
|
||||
|
||||
Reference in New Issue
Block a user