Skip to content

Commit dc925c8

Browse files
committed
Merge remote-tracking branch 'upstream/master' into cfg_target_has_atomic
2 parents 77f3082 + ab8e3c0 commit dc925c8

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
name = "bytes"
44
# When releasing to crates.io:
5-
# - Update html_root_url.
65
# - Update CHANGELOG.md.
7-
# - Update doc URL.
86
# - Create "v1.0.x" git tag.
97
version = "1.0.1"
108
license = "MIT"
@@ -13,7 +11,6 @@ authors = [
1311
"Sean McArthur <sean@seanmonstar.com>",
1412
]
1513
description = "Types and traits for working with bytes"
16-
documentation = "https://docs.rs/bytes/1.0.1/bytes/"
1714
repository = "https://github.com/tokio-rs/bytes"
1815
readme = "README.md"
1916
keywords = ["buffers", "zero-copy", "io"]

src/buf/buf_mut.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pub unsafe trait BufMut {
3333
/// This value is greater than or equal to the length of the slice returned
3434
/// by `chunk_mut()`.
3535
///
36+
/// Writing to a `BufMut` may involve allocating more memory on the fly.
37+
/// Implementations may fail before reaching the number of bytes indicated
38+
/// by this method if they encounter an allocation failure.
39+
///
3640
/// # Examples
3741
///
3842
/// ```
@@ -158,6 +162,9 @@ pub unsafe trait BufMut {
158162
/// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
159163
/// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
160164
/// return an empty slice.
165+
///
166+
/// This function may trigger an out-of-memory abort if it tries to allocate
167+
/// memory and fails to do so.
161168
// The `chunk_mut` method was previously called `bytes_mut`. This alias makes the
162169
// rename more easily discoverable.
163170
#[cfg_attr(docsrs, doc(alias = "bytes_mut"))]
@@ -1025,7 +1032,8 @@ unsafe impl BufMut for &mut [u8] {
10251032
unsafe impl BufMut for Vec<u8> {
10261033
#[inline]
10271034
fn remaining_mut(&self) -> usize {
1028-
usize::MAX - self.len()
1035+
// A vector can never have more than isize::MAX bytes
1036+
core::isize::MAX as usize - self.len()
10291037
}
10301038

10311039
#[inline]

src/bytes_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl BytesMut {
819819
}
820820

821821
fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
822-
if other.is_empty() {
822+
if other.capacity() == 0 {
823823
return Ok(());
824824
}
825825

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
no_crate_inject,
44
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
55
))]
6-
#![doc(html_root_url = "https://docs.rs/bytes/1.0.1")]
76
#![no_std]
87

98
//! Provides abstractions for working with bytes.

tests/test_buf_mut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use core::usize;
99
fn test_vec_as_mut_buf() {
1010
let mut buf = Vec::with_capacity(64);
1111

12-
assert_eq!(buf.remaining_mut(), usize::MAX);
12+
assert_eq!(buf.remaining_mut(), isize::MAX as usize);
1313

1414
assert!(buf.chunk_mut().len() >= 64);
1515

1616
buf.put(&b"zomg"[..]);
1717

1818
assert_eq!(&buf, b"zomg");
1919

20-
assert_eq!(buf.remaining_mut(), usize::MAX - 4);
20+
assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4);
2121
assert_eq!(buf.capacity(), 64);
2222

2323
for _ in 0..16 {

tests/test_bytes.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,31 @@ fn bytes_mut_unsplit_empty_self() {
784784
assert_eq!(b"aaabbbcccddd", &buf[..]);
785785
}
786786

787+
#[test]
788+
fn bytes_mut_unsplit_other_keeps_capacity() {
789+
let mut buf = BytesMut::with_capacity(64);
790+
buf.extend_from_slice(b"aabb");
791+
792+
// non empty other created "from" buf
793+
let mut other = buf.split_off(buf.len());
794+
other.extend_from_slice(b"ccddee");
795+
buf.unsplit(other);
796+
797+
assert_eq!(buf.capacity(), 64);
798+
}
799+
800+
#[test]
801+
fn bytes_mut_unsplit_empty_other_keeps_capacity() {
802+
let mut buf = BytesMut::with_capacity(64);
803+
buf.extend_from_slice(b"aabbccddee");
804+
805+
// empty other created "from" buf
806+
let other = buf.split_off(buf.len());
807+
buf.unsplit(other);
808+
809+
assert_eq!(buf.capacity(), 64);
810+
}
811+
787812
#[test]
788813
fn bytes_mut_unsplit_arc_different() {
789814
let mut buf = BytesMut::with_capacity(64);

0 commit comments

Comments
 (0)