aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alice Ryhl <alice@ryhl.io> 2021-08-07 08:22:18 +0200
committerGravatar GitHub <noreply@github.com> 2021-08-07 08:22:18 +0200
commitab8e3c01a8fae128971cea43eca18da03482cb29 (patch)
treeb2a219b30113f9b6d0e00148069d48972fe972f3
parentf34dc5c3f9e6a3a11e315631055194a733ac1d08 (diff)
downloadbytes-ab8e3c01a8fae128971cea43eca18da03482cb29.tar.gz
bytes-ab8e3c01a8fae128971cea43eca18da03482cb29.tar.zst
bytes-ab8e3c01a8fae128971cea43eca18da03482cb29.zip
Clarify BufMut allocation guarantees (#501)
-rw-r--r--src/buf/buf_mut.rs10
-rw-r--r--tests/test_buf_mut.rs4
2 files changed, 11 insertions, 3 deletions
diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs
index 844474f..bf33fe6 100644
--- a/src/buf/buf_mut.rs
+++ b/src/buf/buf_mut.rs
@@ -33,6 +33,10 @@ pub unsafe trait BufMut {
/// This value is greater than or equal to the length of the slice returned
/// by `chunk_mut()`.
///
+ /// Writing to a `BufMut` may involve allocating more memory on the fly.
+ /// Implementations may fail before reaching the number of bytes indicated
+ /// by this method if they encounter an allocation failure.
+ ///
/// # Examples
///
/// ```
@@ -158,6 +162,9 @@ pub unsafe trait BufMut {
/// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
/// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
/// return an empty slice.
+ ///
+ /// This function may trigger an out-of-memory abort if it tries to allocate
+ /// memory and fails to do so.
// The `chunk_mut` method was previously called `bytes_mut`. This alias makes the
// rename more easily discoverable.
#[cfg_attr(docsrs, doc(alias = "bytes_mut"))]
@@ -1025,7 +1032,8 @@ unsafe impl BufMut for &mut [u8] {
unsafe impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
- usize::MAX - self.len()
+ // A vector can never have more than isize::MAX bytes
+ core::isize::MAX as usize - self.len()
}
#[inline]
diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs
index b85ab9c..f631982 100644
--- a/tests/test_buf_mut.rs
+++ b/tests/test_buf_mut.rs
@@ -9,7 +9,7 @@ use core::usize;
fn test_vec_as_mut_buf() {
let mut buf = Vec::with_capacity(64);
- assert_eq!(buf.remaining_mut(), usize::MAX);
+ assert_eq!(buf.remaining_mut(), isize::MAX as usize);
assert!(buf.chunk_mut().len() >= 64);
@@ -17,7 +17,7 @@ fn test_vec_as_mut_buf() {
assert_eq!(&buf, b"zomg");
- assert_eq!(buf.remaining_mut(), usize::MAX - 4);
+ assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4);
assert_eq!(buf.capacity(), 64);
for _ in 0..16 {