aboutsummaryrefslogtreecommitdiff
path: root/src/buf/buf_mut.rs
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 /src/buf/buf_mut.rs
parentf34dc5c3f9e6a3a11e315631055194a733ac1d08 (diff)
downloadbytes-ab8e3c01a8fae128971cea43eca18da03482cb29.tar.gz
bytes-ab8e3c01a8fae128971cea43eca18da03482cb29.tar.zst
bytes-ab8e3c01a8fae128971cea43eca18da03482cb29.zip
Clarify BufMut allocation guarantees (#501)
Diffstat (limited to 'src/buf/buf_mut.rs')
-rw-r--r--src/buf/buf_mut.rs10
1 files changed, 9 insertions, 1 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]