mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-24 00:00:14 +02:00
Improve assertion messages (#349)
This commit is contained in:
committed by
Carl Lerche
parent
39b6646e66
commit
8695c08bcc
+36
-7
@@ -209,8 +209,18 @@ impl Bytes {
|
|||||||
Bound::Unbounded => len,
|
Bound::Unbounded => len,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert!(begin <= end);
|
assert!(
|
||||||
assert!(end <= len);
|
begin <= end,
|
||||||
|
"range start must not be greater than end: {:?} <= {:?}",
|
||||||
|
begin,
|
||||||
|
end,
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
end <= len,
|
||||||
|
"range end out of bounds: {:?} <= {:?}",
|
||||||
|
end,
|
||||||
|
len,
|
||||||
|
);
|
||||||
|
|
||||||
if end == begin {
|
if end == begin {
|
||||||
return Bytes::new();
|
return Bytes::new();
|
||||||
@@ -302,7 +312,12 @@ impl Bytes {
|
|||||||
/// Panics if `at > len`.
|
/// Panics if `at > len`.
|
||||||
#[must_use = "consider Bytes::truncate if you don't need the other half"]
|
#[must_use = "consider Bytes::truncate if you don't need the other half"]
|
||||||
pub fn split_off(&mut self, at: usize) -> Bytes {
|
pub fn split_off(&mut self, at: usize) -> Bytes {
|
||||||
assert!(at <= self.len());
|
assert!(
|
||||||
|
at <= self.len(),
|
||||||
|
"split_off out of bounds: {:?} <= {:?}",
|
||||||
|
at,
|
||||||
|
self.len(),
|
||||||
|
);
|
||||||
|
|
||||||
if at == self.len() {
|
if at == self.len() {
|
||||||
return Bytes::new();
|
return Bytes::new();
|
||||||
@@ -346,7 +361,12 @@ impl Bytes {
|
|||||||
/// Panics if `at > len`.
|
/// Panics if `at > len`.
|
||||||
#[must_use = "consider Bytes::advance if you don't need the other half"]
|
#[must_use = "consider Bytes::advance if you don't need the other half"]
|
||||||
pub fn split_to(&mut self, at: usize) -> Bytes {
|
pub fn split_to(&mut self, at: usize) -> Bytes {
|
||||||
assert!(at <= self.len());
|
assert!(
|
||||||
|
at <= self.len(),
|
||||||
|
"split_to out of bounds: {:?} <= {:?}",
|
||||||
|
at,
|
||||||
|
self.len(),
|
||||||
|
);
|
||||||
|
|
||||||
if at == self.len() {
|
if at == self.len() {
|
||||||
return mem::replace(self, Bytes::new());
|
return mem::replace(self, Bytes::new());
|
||||||
@@ -430,7 +450,7 @@ impl Bytes {
|
|||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn inc_start(&mut self, by: usize) {
|
unsafe fn inc_start(&mut self, by: usize) {
|
||||||
// should already be asserted, but debug assert for tests
|
// should already be asserted, but debug assert for tests
|
||||||
debug_assert!(self.len >= by);
|
debug_assert!(self.len >= by, "internal: inc_start out of bounds");
|
||||||
self.len -= by;
|
self.len -= by;
|
||||||
self.ptr = self.ptr.offset(by as isize);
|
self.ptr = self.ptr.offset(by as isize);
|
||||||
}
|
}
|
||||||
@@ -477,7 +497,13 @@ impl Buf for Bytes {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
assert!(cnt <= self.len(), "cannot advance past `remaining`");
|
assert!(
|
||||||
|
cnt <= self.len(),
|
||||||
|
"cannot advance past `remaining`: {:?} <= {:?}",
|
||||||
|
cnt,
|
||||||
|
self.len(),
|
||||||
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.inc_start(cnt);
|
self.inc_start(cnt);
|
||||||
}
|
}
|
||||||
@@ -921,7 +947,10 @@ unsafe fn shallow_clone_vec(atom: &AtomicPtr<()>, ptr: *const (), buf: *mut u8,
|
|||||||
|
|
||||||
// The pointer should be aligned, so this assert should
|
// The pointer should be aligned, so this assert should
|
||||||
// always succeed.
|
// always succeed.
|
||||||
debug_assert!(0 == (shared as usize & KIND_MASK));
|
debug_assert!(
|
||||||
|
0 == (shared as usize & KIND_MASK),
|
||||||
|
"internal: Box<Shared> should have an aligned pointer",
|
||||||
|
);
|
||||||
|
|
||||||
// Try compare & swapping the pointer into the `arc` field.
|
// Try compare & swapping the pointer into the `arc` field.
|
||||||
// `Release` is used synchronize with other threads that
|
// `Release` is used synchronize with other threads that
|
||||||
|
|||||||
+21
-6
@@ -277,7 +277,12 @@ impl BytesMut {
|
|||||||
/// Panics if `at > capacity`.
|
/// Panics if `at > capacity`.
|
||||||
#[must_use = "consider BytesMut::truncate if you don't need the other half"]
|
#[must_use = "consider BytesMut::truncate if you don't need the other half"]
|
||||||
pub fn split_off(&mut self, at: usize) -> BytesMut {
|
pub fn split_off(&mut self, at: usize) -> BytesMut {
|
||||||
assert!(at <= self.capacity());
|
assert!(
|
||||||
|
at <= self.capacity(),
|
||||||
|
"split_off out of bounds: {:?} <= {:?}",
|
||||||
|
at,
|
||||||
|
self.capacity(),
|
||||||
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
other.set_start(at);
|
other.set_start(at);
|
||||||
@@ -345,7 +350,12 @@ impl BytesMut {
|
|||||||
/// Panics if `at > len`.
|
/// Panics if `at > len`.
|
||||||
#[must_use = "consider BytesMut::advance if you don't need the other half"]
|
#[must_use = "consider BytesMut::advance if you don't need the other half"]
|
||||||
pub fn split_to(&mut self, at: usize) -> BytesMut {
|
pub fn split_to(&mut self, at: usize) -> BytesMut {
|
||||||
assert!(at <= self.len());
|
assert!(
|
||||||
|
at <= self.len(),
|
||||||
|
"split_to out of bounds: {:?} <= {:?}",
|
||||||
|
at,
|
||||||
|
self.len(),
|
||||||
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
@@ -459,7 +469,7 @@ impl BytesMut {
|
|||||||
/// assert_eq!(&b[..], b"hello world");
|
/// assert_eq!(&b[..], b"hello world");
|
||||||
/// ```
|
/// ```
|
||||||
pub unsafe fn set_len(&mut self, len: usize) {
|
pub unsafe fn set_len(&mut self, len: usize) {
|
||||||
debug_assert!(len <= self.cap);
|
debug_assert!(len <= self.cap, "set_len out of bounds");
|
||||||
self.len = len;
|
self.len = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -755,7 +765,7 @@ impl BytesMut {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_assert!(start <= self.cap);
|
debug_assert!(start <= self.cap, "internal: set_start out of bounds");
|
||||||
|
|
||||||
let kind = self.kind();
|
let kind = self.kind();
|
||||||
|
|
||||||
@@ -794,7 +804,7 @@ impl BytesMut {
|
|||||||
|
|
||||||
unsafe fn set_end(&mut self, end: usize) {
|
unsafe fn set_end(&mut self, end: usize) {
|
||||||
debug_assert_eq!(self.kind(), KIND_ARC);
|
debug_assert_eq!(self.kind(), KIND_ARC);
|
||||||
assert!(end <= self.cap);
|
assert!(end <= self.cap, "set_end out of bounds");
|
||||||
|
|
||||||
self.cap = end;
|
self.cap = end;
|
||||||
self.len = cmp::min(self.len, end);
|
self.len = cmp::min(self.len, end);
|
||||||
@@ -932,7 +942,12 @@ impl Buf for BytesMut {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
assert!(cnt <= self.remaining(), "cannot advance past `remaining`");
|
assert!(
|
||||||
|
cnt <= self.remaining(),
|
||||||
|
"cannot advance past `remaining`: {:?} <= {:?}",
|
||||||
|
cnt,
|
||||||
|
self.remaining(),
|
||||||
|
);
|
||||||
unsafe { self.set_start(cnt); }
|
unsafe { self.set_start(cnt); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user