aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Anthony Ramine <123095+nox@users.noreply.github.com> 2024-05-28 10:14:02 +0200
committerGravatar GitHub <noreply@github.com> 2024-05-28 10:14:02 +0200
commitfa1daac3ae1dcb07dffe3a41a041dffd6edf177b (patch)
tree5f35204d0b1e5d89e3c8fa53c59c5c363694b58f /src
parentcaf520ac7f2c466d26bd88eca33ddc53c408e17e (diff)
downloadbytes-fa1daac3ae1dcb07dffe3a41a041dffd6edf177b.tar.gz
bytes-fa1daac3ae1dcb07dffe3a41a041dffd6edf177b.tar.zst
bytes-fa1daac3ae1dcb07dffe3a41a041dffd6edf177b.zip
Change Bytes::make_mut to impl From<Bytes> for BytesMut (closes #709) (#710)HEADmaster
<Arc<T>>::make_mut returns a &mut T, such an API is doable for Bytes too and thus we should reserve Bytes::make_mut for that. Furthermore, it would be helpful to use From<Bytes> as a trait bound in some cases with other traits such as Hyper's body trait, where Hyper gives you Bytes values. Finally, making it impl From<Bytes> for BytesMut means the API is more easily discoverable as it appears on both Bytes and BytesMut.
Diffstat (limited to 'src')
-rw-r--r--src/bytes.rs44
-rw-r--r--src/bytes/promotable.rs0
2 files changed, 23 insertions, 21 deletions
diff --git a/src/bytes.rs b/src/bytes.rs
index e23d9a8..e0c33b3 100644
--- a/src/bytes.rs
+++ b/src/bytes.rs
@@ -525,32 +525,12 @@ impl Bytes {
/// ```
pub fn try_into_mut(self) -> Result<BytesMut, Bytes> {
if self.is_unique() {
- Ok(self.make_mut())
+ Ok(self.into())
} else {
Err(self)
}
}
- /// Convert self into `BytesMut`.
- ///
- /// If `self` is unique for the entire original buffer, this will return a
- /// `BytesMut` with the contents of `self` without copying.
- /// If `self` is not unique for the entire original buffer, this will make
- /// a copy of `self` subset of the original buffer in a new `BytesMut`.
- ///
- /// # Examples
- ///
- /// ```
- /// use bytes::{Bytes, BytesMut};
- ///
- /// let bytes = Bytes::from(b"hello".to_vec());
- /// assert_eq!(bytes.make_mut(), BytesMut::from(&b"hello"[..]));
- /// ```
- pub fn make_mut(self) -> BytesMut {
- let bytes = ManuallyDrop::new(self);
- unsafe { (bytes.vtable.to_mut)(&bytes.data, bytes.ptr, bytes.len) }
- }
-
#[inline]
pub(crate) unsafe fn with_vtable(
ptr: *const u8,
@@ -932,6 +912,28 @@ impl From<Box<[u8]>> for Bytes {
}
}
+impl From<Bytes> for BytesMut {
+ /// Convert self into `BytesMut`.
+ ///
+ /// If `bytes` is unique for the entire original buffer, this will return a
+ /// `BytesMut` with the contents of `bytes` without copying.
+ /// If `bytes` is not unique for the entire original buffer, this will make
+ /// a copy of `bytes` subset of the original buffer in a new `BytesMut`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, BytesMut};
+ ///
+ /// let bytes = Bytes::from(b"hello".to_vec());
+ /// assert_eq!(BytesMut::from(bytes), BytesMut::from(&b"hello"[..]));
+ /// ```
+ fn from(bytes: Bytes) -> Self {
+ let bytes = ManuallyDrop::new(bytes);
+ unsafe { (bytes.vtable.to_mut)(&bytes.data, bytes.ptr, bytes.len) }
+ }
+}
+
impl From<String> for Bytes {
fn from(s: String) -> Bytes {
Bytes::from(s.into_bytes())
diff --git a/src/bytes/promotable.rs b/src/bytes/promotable.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/bytes/promotable.rs