Skip to content

Commit

Permalink
use ManuallyDrop instead of mem::forget
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Mar 16, 2024
1 parent ab83a52 commit 9bd451c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,13 +829,15 @@ impl From<&'static str> for Bytes {
}

impl From<Vec<u8>> for Bytes {
fn from(mut vec: Vec<u8>) -> Bytes {
fn from(vec: Vec<u8>) -> Bytes {
let mut vec = ManuallyDrop::new(vec);
let ptr = vec.as_mut_ptr();
let len = vec.len();
let cap = vec.capacity();

// Avoid an extra allocation if possible.
if len == cap {
let vec = ManuallyDrop::into_inner(vec);
return Bytes::from(vec.into_boxed_slice());
}

Expand All @@ -844,7 +846,6 @@ impl From<Vec<u8>> for Bytes {
cap,
ref_cnt: AtomicUsize::new(1),
});
mem::forget(vec);

let shared = Box::into_raw(shared);
// The pointer should be aligned, so this assert should
Expand Down

0 comments on commit 9bd451c

Please sign in to comment.