diff options
-rw-r--r-- | src/bytes.rs | 4 | ||||
-rw-r--r-- | tests/test_bytes.rs | 12 |
2 files changed, 13 insertions, 3 deletions
diff --git a/src/bytes.rs b/src/bytes.rs index db716c8..d0c9a14 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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; } } diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 6e3c4b6..f615766 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -313,6 +313,18 @@ fn split_off_to_at_gt_len() { } #[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"[..]; let b = BytesMut::from(s).split().freeze(); |