mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-29 00:00:15 +02:00
chore: add safety comments on unsafe blocks (#827)
This commit is contained in:
@@ -489,6 +489,8 @@ impl Bytes {
|
|||||||
|
|
||||||
self.len = at;
|
self.len = at;
|
||||||
|
|
||||||
|
// SAFETY: `at` has been asserted to be <= `self.len()`, and the
|
||||||
|
// `at == self.len()` and `at == 0` cases were handled above.
|
||||||
unsafe { ret.inc_start(at) };
|
unsafe { ret.inc_start(at) };
|
||||||
|
|
||||||
ret
|
ret
|
||||||
@@ -537,6 +539,8 @@ impl Bytes {
|
|||||||
|
|
||||||
let mut ret = self.clone();
|
let mut ret = self.clone();
|
||||||
|
|
||||||
|
// SAFETY: `at` has been asserted to be <= `self.len()`, and the
|
||||||
|
// `at == self.len()` and `at == 0` cases were handled above.
|
||||||
unsafe { self.inc_start(at) };
|
unsafe { self.inc_start(at) };
|
||||||
|
|
||||||
ret.len = at;
|
ret.len = at;
|
||||||
|
|||||||
@@ -324,6 +324,10 @@ impl BytesMut {
|
|||||||
self.capacity(),
|
self.capacity(),
|
||||||
);
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
|
// SAFETY: `shallow_clone` increments the reference count (or
|
||||||
|
// promotes to shared) and returns a bitwise copy of the handle.
|
||||||
|
// The caller immediately adjusts both handles so they represent
|
||||||
|
// disjoint regions.
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
// SAFETY: We've checked that `at` <= `self.capacity()` above.
|
// SAFETY: We've checked that `at` <= `self.capacity()` above.
|
||||||
other.advance_unchecked(at);
|
other.advance_unchecked(at);
|
||||||
@@ -400,6 +404,10 @@ impl BytesMut {
|
|||||||
);
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
// SAFETY: `shallow_clone` increments the reference count (or
|
||||||
|
// promotes to shared) and returns a bitwise copy of the handle.
|
||||||
|
// The caller immediately adjusts both handles so they represent
|
||||||
|
// disjoint regions.
|
||||||
let mut other = self.shallow_clone();
|
let mut other = self.shallow_clone();
|
||||||
// SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
|
// SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
|
||||||
// `self.capacity()`.
|
// `self.capacity()`.
|
||||||
|
|||||||
@@ -1543,6 +1543,65 @@ fn split_to_empty_addr_mut() {
|
|||||||
let _ = &buf[..];
|
let _ = &buf[..];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bytes_mut_split_boundary_capacities() {
|
||||||
|
// VEC mode
|
||||||
|
for at in [0, 5, 11] {
|
||||||
|
let mut buf = BytesMut::with_capacity(64);
|
||||||
|
buf.extend_from_slice(b"hello world");
|
||||||
|
|
||||||
|
let other = buf.split_off(at);
|
||||||
|
assert_eq!(
|
||||||
|
buf.capacity() + other.capacity(),
|
||||||
|
64,
|
||||||
|
"split_off at {} should preserve total capacity",
|
||||||
|
at
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for at in [0, 5, 11] {
|
||||||
|
let mut buf = BytesMut::with_capacity(64);
|
||||||
|
buf.extend_from_slice(b"hello world");
|
||||||
|
|
||||||
|
let other = buf.split_to(at);
|
||||||
|
assert_eq!(
|
||||||
|
buf.capacity() + other.capacity(),
|
||||||
|
64,
|
||||||
|
"split_to at {} should preserve total capacity",
|
||||||
|
at
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ARC mode (promote via a no-op split)
|
||||||
|
for at in [0, 5, 11] {
|
||||||
|
let mut buf = BytesMut::with_capacity(64);
|
||||||
|
buf.extend_from_slice(b"hello world");
|
||||||
|
let _ = buf.split_to(0); // promotes to ARC
|
||||||
|
|
||||||
|
let other = buf.split_off(at);
|
||||||
|
assert_eq!(
|
||||||
|
buf.capacity() + other.capacity(),
|
||||||
|
64,
|
||||||
|
"ARC split_off at {} should preserve total capacity",
|
||||||
|
at
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for at in [0, 5, 11] {
|
||||||
|
let mut buf = BytesMut::with_capacity(64);
|
||||||
|
buf.extend_from_slice(b"hello world");
|
||||||
|
let _ = buf.split_to(0); // promotes to ARC
|
||||||
|
|
||||||
|
let other = buf.split_to(at);
|
||||||
|
assert_eq!(
|
||||||
|
buf.capacity() + other.capacity(),
|
||||||
|
64,
|
||||||
|
"ARC split_to at {} should preserve total capacity",
|
||||||
|
at
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct SharedAtomicCounter(Arc<AtomicUsize>);
|
struct SharedAtomicCounter(Arc<AtomicUsize>);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user