Skip to content

Commit

Permalink
Remove from_vec_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed May 4, 2024
1 parent fdad89c commit 00e54b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
11 changes: 7 additions & 4 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,9 @@ unsafe fn promotable_to_mut(
let cap = off + len;
let v = Vec::from_raw_parts(buf, cap, cap);

BytesMut::from_vec_offset(v, off)
let mut b = BytesMut::from_vec(v);
b.advance_unchecked(off);
b
}
}

Expand Down Expand Up @@ -1242,10 +1244,11 @@ unsafe fn shared_to_mut_impl(shared: *mut Shared, ptr: *const u8, len: usize) ->

// Rebuild Vec
let off = offset_from(ptr, buf);
let len = len + off;
let v = Vec::from_raw_parts(buf, len, cap);
let v = Vec::from_raw_parts(buf, len + off, cap);

BytesMut::from_vec_offset(v, off)
let mut b = BytesMut::from_vec(v);
b.advance_unchecked(off);
b
} else {
// Copy the data from Shared in a new Vec, then release it
let v = slice::from_raw_parts(ptr, len).to_vec();
Expand Down
19 changes: 6 additions & 13 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,20 +829,13 @@ impl BytesMut {
// suddenly a lot more expensive.
#[inline]
pub(crate) fn from_vec(vec: Vec<u8>) -> BytesMut {
unsafe { BytesMut::from_vec_offset(vec, 0) }
}

#[inline]
pub(crate) unsafe fn from_vec_offset(vec: Vec<u8>, off: usize) -> BytesMut {
let mut vec = ManuallyDrop::new(vec);
let ptr = vptr(vec.as_mut_ptr().add(off));
let len = vec.len().checked_sub(off).unwrap_or(0);
let cap = vec.capacity().checked_sub(off).unwrap_or(0);
let ptr = vptr(vec.as_mut_ptr());
let len = vec.len();
let cap = vec.capacity();

let original_capacity_repr = original_capacity_to_repr(vec.capacity());
let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET)
| (off << VEC_POS_OFFSET)
| KIND_VEC;
let original_capacity_repr = original_capacity_to_repr(cap);
let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC;

BytesMut {
ptr,
Expand All @@ -867,7 +860,7 @@ impl BytesMut {
/// # SAFETY
///
/// The caller must ensure that `count` <= `self.cap`.
unsafe fn advance_unchecked(&mut self, count: usize) {
pub(crate) unsafe fn advance_unchecked(&mut self, count: usize) {
// Setting the start to 0 is a no-op, so return early if this is the
// case.
if count == 0 {
Expand Down

0 comments on commit 00e54b3

Please sign in to comment.