Skip to content

Commit

Permalink
Fix regression in Bytes::truncate (#333)
Browse files Browse the repository at this point in the history
When the length to truncate is greater than the buffer's current
length, do nothing instead of clearing the contents.
  • Loading branch information
mzabaluev authored and carllerche committed Dec 1, 2019
1 parent 98951ba commit af606aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,7 @@ impl Bytes {
/// [`split_off`]: #method.split_off
#[inline]
pub fn truncate(&mut self, len: usize) {
if len >= self.len {
self.len = 0;
} else {
if len < self.len {
self.len = len;
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ fn split_off_to_at_gt_len() {
}).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]
fn freeze_clone_shared() {
let s = &b"abcdefgh"[..];
Expand Down

0 comments on commit af606aa

Please sign in to comment.