Release shared reference on zero truncate (#842)

Co-authored-by: JSap0914 <[email protected]>
This commit is contained in:
JSap0914
2026-07-16 12:19:37 +02:00
committed by GitHub
co-authored by JSap0914
parent 002df10b8c
commit d5c8ad3227
2 changed files with 28 additions and 1 deletions
+25
View File
@@ -339,6 +339,31 @@ fn truncate() {
assert_eq!(hello, "hello");
}
#[test]
fn truncate_to_zero_releases_shared_reference() {
let mut bytes = BytesMut::from(&b"hello"[..]);
drop(bytes.split_off(bytes.len()));
let mut truncated = bytes.freeze();
let remaining = truncated.clone();
truncated.truncate(0);
assert!(truncated.is_empty());
let mut remaining = remaining.try_into_mut().unwrap();
remaining[0] = b'H';
assert_eq!(remaining, b"Hello"[..]);
let mut bytes = BytesMut::from(&b"hello"[..]);
drop(bytes.split_off(bytes.len()));
let mut nonempty = bytes.freeze();
let remaining = nonempty.clone();
nonempty.truncate(1);
assert_eq!(nonempty, b"h"[..]);
assert!(remaining.try_into_mut().is_err());
}
#[test]
fn freeze_clone_shared() {
let s = &b"abcdefgh"[..];