1
1
use core:: iter:: FromIterator ;
2
+ use core:: mem:: { self , ManuallyDrop } ;
2
3
use core:: ops:: { Deref , RangeBounds } ;
3
- use core:: { cmp, fmt, hash, mem , ptr, slice, usize} ;
4
+ use core:: { cmp, fmt, hash, ptr, slice, usize} ;
4
5
5
6
use alloc:: {
6
7
alloc:: { dealloc, Layout } ,
@@ -828,13 +829,15 @@ impl From<&'static str> for Bytes {
828
829
}
829
830
830
831
impl From < Vec < u8 > > for Bytes {
831
- fn from ( mut vec : Vec < u8 > ) -> Bytes {
832
+ fn from ( vec : Vec < u8 > ) -> Bytes {
833
+ let mut vec = ManuallyDrop :: new ( vec) ;
832
834
let ptr = vec. as_mut_ptr ( ) ;
833
835
let len = vec. len ( ) ;
834
836
let cap = vec. capacity ( ) ;
835
837
836
838
// Avoid an extra allocation if possible.
837
839
if len == cap {
840
+ let vec = ManuallyDrop :: into_inner ( vec) ;
838
841
return Bytes :: from ( vec. into_boxed_slice ( ) ) ;
839
842
}
840
843
@@ -843,7 +846,6 @@ impl From<Vec<u8>> for Bytes {
843
846
cap,
844
847
ref_cnt : AtomicUsize :: new ( 1 ) ,
845
848
} ) ;
846
- mem:: forget ( vec) ;
847
849
848
850
let shared = Box :: into_raw ( shared) ;
849
851
// The pointer should be aligned, so this assert should
@@ -900,7 +902,7 @@ impl From<String> for Bytes {
900
902
901
903
impl From < Bytes > for Vec < u8 > {
902
904
fn from ( bytes : Bytes ) -> Vec < u8 > {
903
- let bytes = mem :: ManuallyDrop :: new ( bytes) ;
905
+ let bytes = ManuallyDrop :: new ( bytes) ;
904
906
unsafe { ( bytes. vtable . to_vec ) ( & bytes. data , bytes. ptr , bytes. len ) }
905
907
}
906
908
}
@@ -1116,11 +1118,11 @@ unsafe fn shared_to_vec_impl(shared: *mut Shared, ptr: *const u8, len: usize) ->
1116
1118
. compare_exchange ( 1 , 0 , Ordering :: AcqRel , Ordering :: Relaxed )
1117
1119
. is_ok ( )
1118
1120
{
1119
- let buf = ( * shared ) . buf ;
1120
- let cap = ( * shared) . cap ;
1121
-
1122
- // Deallocate Shared
1123
- drop ( Box :: from_raw ( shared as * mut mem :: ManuallyDrop < Shared > ) ) ;
1121
+ // Deallocate the `Shared` instance without running its destructor.
1122
+ let shared = * Box :: from_raw ( shared) ;
1123
+ let shared = ManuallyDrop :: new ( shared ) ;
1124
+ let buf = shared . buf ;
1125
+ let cap = shared . cap ;
1124
1126
1125
1127
// Copy back buffer
1126
1128
ptr:: copy ( ptr, buf, len) ;
0 commit comments