Skip to content

Commit 4628927

Browse files
authored
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
1 parent 1bcd212 commit 4628927

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/bytes_mut.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ impl BytesMut {
317317
);
318318
unsafe {
319319
let mut other = self.shallow_clone();
320-
other.set_start(at);
321-
self.set_end(at);
320+
// SAFETY: We've checked that `at` <= `self.capacity()` above.
321+
other.advance_unchecked(at);
322+
self.cap = at;
323+
self.len = cmp::min(self.len, at);
322324
other
323325
}
324326
}
@@ -391,8 +393,11 @@ impl BytesMut {
391393

392394
unsafe {
393395
let mut other = self.shallow_clone();
394-
other.set_end(at);
395-
self.set_start(at);
396+
// SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
397+
// `self.capacity()`.
398+
self.advance_unchecked(at);
399+
other.cap = at;
400+
other.len = at;
396401
other
397402
}
398403
}
@@ -851,14 +856,19 @@ impl BytesMut {
851856
unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
852857
}
853858

854-
unsafe fn set_start(&mut self, start: usize) {
859+
/// Advance the buffer without bounds checking.
860+
///
861+
/// # SAFETY
862+
///
863+
/// The caller must ensure that `count` <= `self.cap`.
864+
unsafe fn advance_unchecked(&mut self, count: usize) {
855865
// Setting the start to 0 is a no-op, so return early if this is the
856866
// case.
857-
if start == 0 {
867+
if count == 0 {
858868
return;
859869
}
860870

861-
debug_assert!(start <= self.cap, "internal: set_start out of bounds");
871+
debug_assert!(count <= self.cap, "internal: set_start out of bounds");
862872

863873
let kind = self.kind();
864874

@@ -867,7 +877,7 @@ impl BytesMut {
867877
// complicated. First, we have to track how far ahead the
868878
// "start" of the byte buffer from the beginning of the vec. We
869879
// also have to ensure that we don't exceed the maximum shift.
870-
let pos = self.get_vec_pos() + start;
880+
let pos = self.get_vec_pos() + count;
871881

872882
if pos <= MAX_VEC_POS {
873883
self.set_vec_pos(pos);
@@ -883,23 +893,9 @@ impl BytesMut {
883893
// Updating the start of the view is setting `ptr` to point to the
884894
// new start and updating the `len` field to reflect the new length
885895
// of the view.
886-
self.ptr = vptr(self.ptr.as_ptr().add(start));
887-
888-
if self.len >= start {
889-
self.len -= start;
890-
} else {
891-
self.len = 0;
892-
}
893-
894-
self.cap -= start;
895-
}
896-
897-
unsafe fn set_end(&mut self, end: usize) {
898-
debug_assert_eq!(self.kind(), KIND_ARC);
899-
assert!(end <= self.cap, "set_end out of bounds");
900-
901-
self.cap = end;
902-
self.len = cmp::min(self.len, end);
896+
self.ptr = vptr(self.ptr.as_ptr().add(count));
897+
self.len = self.len.checked_sub(count).unwrap_or(0);
898+
self.cap -= count;
903899
}
904900

905901
fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
@@ -1069,7 +1065,9 @@ impl Buf for BytesMut {
10691065
self.remaining(),
10701066
);
10711067
unsafe {
1072-
self.set_start(cnt);
1068+
// SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that
1069+
// `self.remaining()` <= `self.cap`.
1070+
self.advance_unchecked(cnt);
10731071
}
10741072
}
10751073

0 commit comments

Comments
 (0)