diff options
author | 2020-01-23 18:04:13 +0000 | |
---|---|---|
committer | 2020-01-23 10:04:13 -0800 | |
commit | ab028eb6a8e4ea858022b8767336b08e83a882c6 (patch) | |
tree | e05d7c8a2ea3cd64696beb48fdc6e68889eec491 /src/bytes.rs | |
parent | e0eebde9938d03eb60fe97c4a41f464e4c83d414 (diff) | |
download | bytes-ab028eb6a8e4ea858022b8767336b08e83a882c6.tar.gz bytes-ab028eb6a8e4ea858022b8767336b08e83a882c6.tar.zst bytes-ab028eb6a8e4ea858022b8767336b08e83a882c6.zip |
rebuild_boxed_slice instead of rebuild_boxed_vec (#364)
"Promotable" `Bytes` object is constructed from disassembling a
boxed slice object, not a vec.
Thus we should reassemble data into a boxed slice, not into a vec.
Although, it does not create any problems in practice (`Box<[u8]>`
is allocated exactly the same way as `Vec<u8>`), technically it is
a violation of `Vec::from_raw_parts` spec which says that a pointer
"needs to have been previously allocated via `String`/`Vec<T>`".
Diffstat (limited to 'src/bytes.rs')
-rw-r--r-- | src/bytes.rs | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/bytes.rs b/src/bytes.rs index 8ecc853..d8af5b5 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -863,7 +863,7 @@ unsafe fn promotable_even_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: us } else { debug_assert_eq!(kind, KIND_VEC); let buf = (shared as usize & !KIND_MASK) as *mut u8; - drop(rebuild_vec(buf, ptr, len)); + drop(rebuild_boxed_slice(buf, ptr, len)); } } @@ -888,13 +888,13 @@ unsafe fn promotable_odd_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: usi } else { debug_assert_eq!(kind, KIND_VEC); - drop(rebuild_vec(shared as *mut u8, ptr, len)); + drop(rebuild_boxed_slice(shared as *mut u8, ptr, len)); } } -unsafe fn rebuild_vec(buf: *mut u8, offset: *const u8, len: usize) -> Vec<u8> { +unsafe fn rebuild_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) -> Box<[u8]> { let cap = (offset as usize - buf as usize) + len; - Vec::from_raw_parts(buf, cap, cap) + Box::from_raw(slice::from_raw_parts_mut(buf, cap)) } // ===== impl SharedVtable ===== @@ -952,7 +952,7 @@ unsafe fn shallow_clone_vec(atom: &AtomicPtr<()>, ptr: *const (), buf: *mut u8, // updated and since the buffer hasn't been promoted to an // `Arc`, those three fields still are the components of the // vector. - let vec = rebuild_vec(buf, offset, len); + let vec = rebuild_boxed_slice(buf, offset, len).into_vec(); let shared = Box::new(Shared { _vec: vec, // Initialize refcount to 2. One for this reference, and one |