Add must_use to split, split_off, and split_to

This commit is contained in:
Sean McArthur
2019-12-04 12:39:48 -08:00
parent af606aab9b
commit a4908213a6
3 changed files with 70 additions and 11 deletions
+24
View File
@@ -288,6 +288,7 @@ impl Bytes {
/// # Panics /// # Panics
/// ///
/// Panics if `at > len`. /// Panics if `at > len`.
#[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());
@@ -331,6 +332,7 @@ impl Bytes {
/// # Panics /// # Panics
/// ///
/// Panics if `at > len`. /// Panics if `at > len`.
#[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());
@@ -913,6 +915,28 @@ unsafe fn release_shared(ptr: *mut Shared) {
Box::from_raw(ptr); Box::from_raw(ptr);
} }
// compile-fails
/// ```compile_fail
/// use bytes::Bytes;
/// #[deny(unused_must_use)]
/// {
/// let mut b1 = Bytes::from("hello world");
/// b1.split_to(6);
/// }
/// ```
fn _split_to_must_use() {}
/// ```compile_fail
/// use bytes::Bytes;
/// #[deny(unused_must_use)]
/// {
/// let mut b1 = Bytes::from("hello world");
/// b1.split_off(6);
/// }
/// ```
fn _split_off_must_use() {}
// fuzz tests // fuzz tests
#[cfg(all(test, loom))] #[cfg(all(test, loom))]
mod fuzz { mod fuzz {
+35
View File
@@ -275,6 +275,7 @@ impl BytesMut {
/// # Panics /// # Panics
/// ///
/// Panics if `at > capacity`. /// Panics if `at > capacity`.
#[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());
unsafe { unsafe {
@@ -310,6 +311,7 @@ impl BytesMut {
/// ///
/// assert_eq!(other, b"hello world"[..]); /// assert_eq!(other, b"hello world"[..]);
/// ``` /// ```
#[must_use = "consider BytesMut::advance(len()) if you don't need the other half"]
pub fn split(&mut self) -> BytesMut { pub fn split(&mut self) -> BytesMut {
let len = self.len(); let len = self.len();
self.split_to(len) self.split_to(len)
@@ -341,6 +343,7 @@ impl BytesMut {
/// # Panics /// # Panics
/// ///
/// Panics if `at > len`. /// Panics if `at > len`.
#[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());
@@ -1428,6 +1431,38 @@ unsafe fn shared_v_drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize)
release_shared(shared as *mut Shared); release_shared(shared as *mut Shared);
} }
// compile-fails
/// ```compile_fail
/// use bytes::BytesMut;
/// #[deny(unused_must_use)]
/// {
/// let mut b1 = BytesMut::from("hello world");
/// b1.split_to(6);
/// }
/// ```
fn _split_to_must_use() {}
/// ```compile_fail
/// use bytes::BytesMut;
/// #[deny(unused_must_use)]
/// {
/// let mut b1 = BytesMut::from("hello world");
/// b1.split_off(6);
/// }
/// ```
fn _split_off_must_use() {}
/// ```compile_fail
/// use bytes::BytesMut;
/// #[deny(unused_must_use)]
/// {
/// let mut b1 = BytesMut::from("hello world");
/// b1.split();
/// }
/// ```
fn _split_must_use() {}
// fuzz tests // fuzz tests
#[cfg(all(test, loom))] #[cfg(all(test, loom))]
mod fuzz { mod fuzz {
+11 -11
View File
@@ -176,7 +176,7 @@ fn split_off() {
#[should_panic] #[should_panic]
fn split_off_oob() { fn split_off_oob() {
let mut hello = Bytes::from(&b"helloworld"[..]); let mut hello = Bytes::from(&b"helloworld"[..]);
hello.split_off(44); let _ = hello.split_off(44);
} }
#[test] #[test]
@@ -273,14 +273,14 @@ fn split_to_2() {
#[should_panic] #[should_panic]
fn split_to_oob() { fn split_to_oob() {
let mut hello = Bytes::from(&b"helloworld"[..]); let mut hello = Bytes::from(&b"helloworld"[..]);
hello.split_to(33); let _ = hello.split_to(33);
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn split_to_oob_mut() { fn split_to_oob_mut() {
let mut hello = BytesMut::from(&b"helloworld"[..]); let mut hello = BytesMut::from(&b"helloworld"[..]);
hello.split_to(33); let _ = hello.split_to(33);
} }
#[test] #[test]
@@ -300,15 +300,15 @@ fn split_off_to_at_gt_len() {
use std::panic; use std::panic;
make_bytes().split_to(4); let _ = make_bytes().split_to(4);
make_bytes().split_off(4); let _ = make_bytes().split_off(4);
assert!(panic::catch_unwind(move || { assert!(panic::catch_unwind(move || {
make_bytes().split_to(5); let _ = make_bytes().split_to(5);
}).is_err()); }).is_err());
assert!(panic::catch_unwind(move || { assert!(panic::catch_unwind(move || {
make_bytes().split_off(5); let _ = make_bytes().split_off(5);
}).is_err()); }).is_err());
} }
@@ -428,7 +428,7 @@ fn reserve_vec_recycling() {
#[test] #[test]
fn reserve_in_arc_unique_does_not_overallocate() { fn reserve_in_arc_unique_does_not_overallocate() {
let mut bytes = BytesMut::with_capacity(1000); let mut bytes = BytesMut::with_capacity(1000);
bytes.split(); let _ = bytes.split();
// now bytes is Arc and refcount == 1 // now bytes is Arc and refcount == 1
@@ -440,7 +440,7 @@ fn reserve_in_arc_unique_does_not_overallocate() {
#[test] #[test]
fn reserve_in_arc_unique_doubles() { fn reserve_in_arc_unique_doubles() {
let mut bytes = BytesMut::with_capacity(1000); let mut bytes = BytesMut::with_capacity(1000);
bytes.split(); let _ = bytes.split();
// now bytes is Arc and refcount == 1 // now bytes is Arc and refcount == 1
@@ -723,12 +723,12 @@ fn bytes_mut_unsplit_arc_different() {
let mut buf = BytesMut::with_capacity(64); let mut buf = BytesMut::with_capacity(64);
buf.extend_from_slice(b"aaaabbbbeeee"); buf.extend_from_slice(b"aaaabbbbeeee");
buf.split_off(8); //arc let _ = buf.split_off(8); //arc
let mut buf2 = BytesMut::with_capacity(64); let mut buf2 = BytesMut::with_capacity(64);
buf2.extend_from_slice(b"ccccddddeeee"); buf2.extend_from_slice(b"ccccddddeeee");
buf2.split_off(8); //arc let _ = buf2.split_off(8); //arc
buf.unsplit(buf2); buf.unsplit(buf2);
assert_eq!(b"aaaabbbbccccdddd", &buf[..]); assert_eq!(b"aaaabbbbccccdddd", &buf[..]);