-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move rebuild_vec into a method #659
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,7 +248,7 @@ impl BytesMut { | |
// Just re-use `Bytes` internal Vec vtable | ||
unsafe { | ||
let (off, _) = self.get_vec_pos(); | ||
let vec = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); | ||
let vec = self.rebuild_vec(off); | ||
mem::forget(self); | ||
let mut b: Bytes = vec.into(); | ||
b.advance(off); | ||
|
@@ -629,8 +629,7 @@ impl BytesMut { | |
} else { | ||
// Not enough space, or reusing might be too much overhead: | ||
// allocate more space! | ||
let mut v = | ||
ManuallyDrop::new(rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off)); | ||
let mut v = ManuallyDrop::new(self.rebuild_vec(off)); | ||
v.reserve(additional); | ||
|
||
// Update the info | ||
|
@@ -950,7 +949,7 @@ impl BytesMut { | |
// `Arc`, those three fields still are the components of the | ||
// vector. | ||
let shared = Box::new(Shared { | ||
vec: rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off), | ||
vec: self.rebuild_vec(off), | ||
original_capacity_repr, | ||
ref_count: AtomicUsize::new(ref_cnt), | ||
}); | ||
|
@@ -1035,6 +1034,16 @@ impl BytesMut { | |
slice::from_raw_parts_mut(ptr.cast(), len) | ||
} | ||
} | ||
|
||
/// Rebuild a vec from `offset` with the existing pointer, length, and capacity. | ||
#[inline] | ||
unsafe fn rebuild_vec(&self, offset: usize) -> Vec<u8> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you document a bit about why this is unsafe? That is, explain that this transfers ownership of the allocation to the vector, but that it doesn't remove the pointers from the I know that it didn't explain this before your change, but by moving it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing that out @Darksonn! I added some information in f62a4d0. Let me know if you prefer something different. Also, the source of |
||
let ptr = self.ptr.as_ptr().offset(-(offset as isize)); | ||
let len = self.len + offset; | ||
let cap = self.cap + offset; | ||
|
||
Vec::from_raw_parts(ptr, len, cap) | ||
} | ||
} | ||
|
||
impl Drop for BytesMut { | ||
|
@@ -1046,7 +1055,7 @@ impl Drop for BytesMut { | |
let (off, _) = self.get_vec_pos(); | ||
|
||
// Vector storage, free the vector | ||
let _ = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); | ||
let _ = self.rebuild_vec(off); | ||
} | ||
} else if kind == KIND_ARC { | ||
unsafe { release_shared(self.data) }; | ||
|
@@ -1626,7 +1635,7 @@ impl From<BytesMut> for Vec<u8> { | |
let mut vec = if kind == KIND_VEC { | ||
unsafe { | ||
let (off, _) = bytes.get_vec_pos(); | ||
rebuild_vec(bytes.ptr.as_ptr(), bytes.len, bytes.cap, off) | ||
bytes.rebuild_vec(off) | ||
} | ||
} else if kind == KIND_ARC { | ||
let shared = bytes.data as *mut Shared; | ||
|
@@ -1695,14 +1704,6 @@ fn offset_from(dst: *mut u8, original: *mut u8) -> usize { | |
dst as usize - original as usize | ||
} | ||
|
||
unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec<u8> { | ||
let ptr = ptr.offset(-(off as isize)); | ||
len += off; | ||
cap += off; | ||
|
||
Vec::from_raw_parts(ptr, len, cap) | ||
} | ||
|
||
// ===== impl SharedVtable ===== | ||
|
||
static SHARED_VTABLE: Vtable = Vtable { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you don't actually follow the safety requirements as stated here.