mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-07 00:00:13 +02:00
Add conversion from BytesMut to Vec<u8> (#543)
This commit is contained in:
@@ -1028,3 +1028,40 @@ fn box_slice_empty() {
|
||||
let b = Bytes::from(empty);
|
||||
assert!(b.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_into_vec() {
|
||||
// Test kind == KIND_VEC
|
||||
let content = b"helloworld";
|
||||
|
||||
let mut bytes = BytesMut::new();
|
||||
bytes.put_slice(content);
|
||||
|
||||
let vec: Vec<u8> = bytes.into();
|
||||
assert_eq!(&vec, content);
|
||||
|
||||
// Test kind == KIND_ARC, shared.is_unique() == True
|
||||
let mut bytes = BytesMut::new();
|
||||
bytes.put_slice(b"abcdewe23");
|
||||
bytes.put_slice(content);
|
||||
|
||||
// Overwrite the bytes to make sure only one reference to the underlying
|
||||
// Vec exists.
|
||||
bytes = bytes.split_off(9);
|
||||
|
||||
let vec: Vec<u8> = bytes.into();
|
||||
assert_eq!(&vec, content);
|
||||
|
||||
// Test kind == KIND_ARC, shared.is_unique() == False
|
||||
let prefix = b"abcdewe23";
|
||||
|
||||
let mut bytes = BytesMut::new();
|
||||
bytes.put_slice(prefix);
|
||||
bytes.put_slice(content);
|
||||
|
||||
let vec: Vec<u8> = bytes.split_off(prefix.len()).into();
|
||||
assert_eq!(&vec, content);
|
||||
|
||||
let vec: Vec<u8> = bytes.into();
|
||||
assert_eq!(&vec, prefix);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user