Skip to content

Commit

Permalink
Various cleanup (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Oct 2, 2023
1 parent a4e16a5 commit fd9243f
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 137 deletions.
168 changes: 117 additions & 51 deletions src/buf/buf_impl.rs

Large diffs are not rendered by default.

261 changes: 187 additions & 74 deletions src/buf/buf_mut.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/buf/chain.rs
Expand Up @@ -135,7 +135,7 @@ where
U: Buf,
{
fn remaining(&self) -> usize {
self.a.remaining().checked_add(self.b.remaining()).unwrap()
self.a.remaining().saturating_add(self.b.remaining())
}

fn chunk(&self) -> &[u8] {
Expand Down
2 changes: 1 addition & 1 deletion src/buf/uninit_slice.rs
Expand Up @@ -184,7 +184,7 @@ impl UninitSlice {
/// };
/// ```
#[inline]
pub unsafe fn as_uninit_slice_mut<'a>(&'a mut self) -> &'a mut [MaybeUninit<u8>] {
pub unsafe fn as_uninit_slice_mut(&mut self) -> &mut [MaybeUninit<u8>] {
&mut *(self as *mut _ as *mut [MaybeUninit<u8>])
}

Expand Down
2 changes: 1 addition & 1 deletion src/bytes.rs
Expand Up @@ -242,7 +242,7 @@ impl Bytes {

let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Excluded(&n) => n.checked_add(1).expect("out of range"),
Bound::Unbounded => 0,
};

Expand Down
14 changes: 6 additions & 8 deletions src/bytes_mut.rs
Expand Up @@ -1087,14 +1087,12 @@ unsafe impl BufMut for BytesMut {

#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
let new_len = self.len() + cnt;
assert!(
new_len <= self.cap,
"new_len = {}; capacity = {}",
new_len,
self.cap
);
self.len = new_len;
let remaining = self.cap - self.len();
if cnt > remaining {
super::panic_advance(cnt, remaining);
}
// Addition won't overflow since it is at most `self.cap`.
self.len = self.len() + cnt;
}

#[inline]
Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Expand Up @@ -115,3 +115,40 @@ fn abort() -> ! {
panic!("abort");
}
}

#[inline(always)]
#[cfg(feature = "std")]
fn saturating_sub_usize_u64(a: usize, b: u64) -> usize {
use core::convert::TryFrom;
match usize::try_from(b) {
Ok(b) => a.saturating_sub(b),
Err(_) => 0,
}
}

#[inline(always)]
#[cfg(feature = "std")]
fn min_u64_usize(a: u64, b: usize) -> usize {
use core::convert::TryFrom;
match usize::try_from(a) {
Ok(a) => usize::min(a, b),
Err(_) => b,
}
}

/// Panic with a nice error message.
#[cold]
fn panic_advance(idx: usize, len: usize) -> ! {
panic!(
"advance out of bounds: the len is {} but advancing by {}",
len, idx
);
}

#[cold]
fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
panic!(
"size too large: the integer type can fit {} bytes, but nbytes is {}",
size, nbytes
);
}
2 changes: 1 addition & 1 deletion tests/test_buf_mut.rs
Expand Up @@ -83,7 +83,7 @@ fn test_put_int_le_nbytes_overflow() {
}

#[test]
#[should_panic(expected = "cannot advance")]
#[should_panic(expected = "advance out of bounds: the len is 8 but advancing by 12")]
fn test_vec_advance_mut() {
// Verify fix for #354
let mut buf = Vec::with_capacity(8);
Expand Down

0 comments on commit fd9243f

Please sign in to comment.