diff options
author | Haibo Huang <hhb@google.com> | 2020-07-20 22:11:12 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-07-20 22:11:12 +0000 |
commit | 5c3f69fe082eb884a5c0970fe36bc28216ca592d (patch) | |
tree | 50b5094b1c55ce2b1bdc01c11e9a741031561006 /src/bytes_mut.rs | |
parent | 192f5ef0d8170589b14b94f3bd16376782b9f827 (diff) | |
parent | 405dc7ae39aced6e95686372f79e1db33a8727d3 (diff) | |
download | bytes-5c3f69fe082eb884a5c0970fe36bc28216ca592d.tar.gz |
Upgrade rust/crates/bytes to 0.5.6 am: 293e2aa580 am: a0a16c71fa am: 405dc7ae39
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/bytes/+/1361690
Change-Id: Idfd4a46aedef3effa25be46db6c368f4b3247e5f
Diffstat (limited to 'src/bytes_mut.rs')
-rw-r--r-- | src/bytes_mut.rs | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 4d0585e..a7a8e57 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -22,8 +22,12 @@ use crate::{Buf, BufMut, Bytes}; /// /// `BytesMut` represents a unique view into a potentially shared memory region. /// Given the uniqueness guarantee, owners of `BytesMut` handles are able to -/// mutate the memory. It is similar to a `Vec<u8>` but with less copies and -/// allocations. +/// mutate the memory. +/// +/// `BytesMut` can be thought of as containing a `buf: Arc<Vec<u8>>`, an offset +/// into `buf`, a slice length, and a guarantee that no other `BytesMut` for the +/// same `buf` overlaps with its slice. That guarantee means that a write lock +/// is not required. /// /// # Growth /// @@ -475,6 +479,7 @@ impl BytesMut { /// /// assert_eq!(&b[..], b"hello world"); /// ``` + #[inline] pub unsafe fn set_len(&mut self, len: usize) { debug_assert!(len <= self.cap, "set_len out of bounds"); self.len = len; @@ -558,9 +563,8 @@ impl BytesMut { unsafe { let (off, prev) = self.get_vec_pos(); - // Only reuse space if we stand to gain at least capacity/2 - // bytes of space back - if off >= additional && off >= (self.cap / 2) { + // Only reuse space if we can satisfy the requested additional space. + if self.capacity() - self.len() + off >= additional { // There's space - reuse it // // Just move the pointer back to the start after copying @@ -1025,6 +1029,7 @@ impl Deref for BytesMut { } impl AsMut<[u8]> for BytesMut { + #[inline] fn as_mut(&mut self) -> &mut [u8] { self.as_slice_mut() } @@ -1495,7 +1500,7 @@ static SHARED_VTABLE: Vtable = Vtable { }; unsafe fn shared_v_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { - let shared = data.load(Ordering::Acquire) as *mut Shared; + let shared = data.load(Ordering::Relaxed) as *mut Shared; increment_shared(shared); let data = AtomicPtr::new(shared as _); |