mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-27 00:00:17 +02:00
Refactor split_at/split_to (#663)
* set len a little more concisely * inline set_end * remove kind assertions * remove a duplicate assertion * remove redundant assertion and min * rename set_start to advance_unchecked
This commit is contained in:
+24
-26
@@ -317,8 +317,10 @@ impl BytesMut {
|
|||||||
);
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
other.set_start(at);
|
// SAFETY: We've checked that `at` <= `self.capacity()` above.
|
||||||
self.set_end(at);
|
other.advance_unchecked(at);
|
||||||
|
self.cap = at;
|
||||||
|
self.len = cmp::min(self.len, at);
|
||||||
other
|
other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,8 +393,11 @@ impl BytesMut {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
other.set_end(at);
|
// SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
|
||||||
self.set_start(at);
|
// `self.capacity()`.
|
||||||
|
self.advance_unchecked(at);
|
||||||
|
other.cap = at;
|
||||||
|
other.len = at;
|
||||||
other
|
other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -851,14 +856,19 @@ impl BytesMut {
|
|||||||
unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
|
unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn set_start(&mut self, start: usize) {
|
/// Advance the buffer without bounds checking.
|
||||||
|
///
|
||||||
|
/// # SAFETY
|
||||||
|
///
|
||||||
|
/// The caller must ensure that `count` <= `self.cap`.
|
||||||
|
unsafe fn advance_unchecked(&mut self, count: usize) {
|
||||||
// Setting the start to 0 is a no-op, so return early if this is the
|
// Setting the start to 0 is a no-op, so return early if this is the
|
||||||
// case.
|
// case.
|
||||||
if start == 0 {
|
if count == 0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_assert!(start <= self.cap, "internal: set_start out of bounds");
|
debug_assert!(count <= self.cap, "internal: set_start out of bounds");
|
||||||
|
|
||||||
let kind = self.kind();
|
let kind = self.kind();
|
||||||
|
|
||||||
@@ -867,7 +877,7 @@ impl BytesMut {
|
|||||||
// complicated. First, we have to track how far ahead the
|
// complicated. First, we have to track how far ahead the
|
||||||
// "start" of the byte buffer from the beginning of the vec. We
|
// "start" of the byte buffer from the beginning of the vec. We
|
||||||
// also have to ensure that we don't exceed the maximum shift.
|
// also have to ensure that we don't exceed the maximum shift.
|
||||||
let pos = self.get_vec_pos() + start;
|
let pos = self.get_vec_pos() + count;
|
||||||
|
|
||||||
if pos <= MAX_VEC_POS {
|
if pos <= MAX_VEC_POS {
|
||||||
self.set_vec_pos(pos);
|
self.set_vec_pos(pos);
|
||||||
@@ -883,23 +893,9 @@ impl BytesMut {
|
|||||||
// Updating the start of the view is setting `ptr` to point to the
|
// Updating the start of the view is setting `ptr` to point to the
|
||||||
// new start and updating the `len` field to reflect the new length
|
// new start and updating the `len` field to reflect the new length
|
||||||
// of the view.
|
// of the view.
|
||||||
self.ptr = vptr(self.ptr.as_ptr().add(start));
|
self.ptr = vptr(self.ptr.as_ptr().add(count));
|
||||||
|
self.len = self.len.checked_sub(count).unwrap_or(0);
|
||||||
if self.len >= start {
|
self.cap -= count;
|
||||||
self.len -= start;
|
|
||||||
} else {
|
|
||||||
self.len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.cap -= start;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn set_end(&mut self, end: usize) {
|
|
||||||
debug_assert_eq!(self.kind(), KIND_ARC);
|
|
||||||
assert!(end <= self.cap, "set_end out of bounds");
|
|
||||||
|
|
||||||
self.cap = end;
|
|
||||||
self.len = cmp::min(self.len, end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
|
fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
|
||||||
@@ -1069,7 +1065,9 @@ impl Buf for BytesMut {
|
|||||||
self.remaining(),
|
self.remaining(),
|
||||||
);
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
self.set_start(cnt);
|
// SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that
|
||||||
|
// `self.remaining()` <= `self.cap`.
|
||||||
|
self.advance_unchecked(cnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user