mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-21 00:00:12 +02:00
Fix regression in Bytes::truncate (#333)
When the length to truncate is greater than the buffer's current length, do nothing instead of clearing the contents.
This commit is contained in:
committed by
Carl Lerche
parent
98951ba26c
commit
af606aab9b
+1
-3
@@ -373,9 +373,7 @@ impl Bytes {
|
|||||||
/// [`split_off`]: #method.split_off
|
/// [`split_off`]: #method.split_off
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn truncate(&mut self, len: usize) {
|
pub fn truncate(&mut self, len: usize) {
|
||||||
if len >= self.len {
|
if len < self.len {
|
||||||
self.len = 0;
|
|
||||||
} else {
|
|
||||||
self.len = len;
|
self.len = len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -312,6 +312,18 @@ fn split_off_to_at_gt_len() {
|
|||||||
}).is_err());
|
}).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate() {
|
||||||
|
let s = &b"helloworld"[..];
|
||||||
|
let mut hello = Bytes::from(s);
|
||||||
|
hello.truncate(15);
|
||||||
|
assert_eq!(hello, s);
|
||||||
|
hello.truncate(10);
|
||||||
|
assert_eq!(hello, s);
|
||||||
|
hello.truncate(5);
|
||||||
|
assert_eq!(hello, "hello");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn freeze_clone_shared() {
|
fn freeze_clone_shared() {
|
||||||
let s = &b"abcdefgh"[..];
|
let s = &b"abcdefgh"[..];
|
||||||
|
|||||||
Reference in New Issue
Block a user