Skip to content

Commit fa1daac

Browse files
authored
Change Bytes::make_mut to impl From<Bytes> for BytesMut (closes #709) (#710)
<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.
1 parent caf520a commit fa1daac

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

src/bytes.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -525,32 +525,12 @@ impl Bytes {
525525
/// ```
526526
pub fn try_into_mut(self) -> Result<BytesMut, Bytes> {
527527
if self.is_unique() {
528-
Ok(self.make_mut())
528+
Ok(self.into())
529529
} else {
530530
Err(self)
531531
}
532532
}
533533

534-
/// Convert self into `BytesMut`.
535-
///
536-
/// If `self` is unique for the entire original buffer, this will return a
537-
/// `BytesMut` with the contents of `self` without copying.
538-
/// If `self` is not unique for the entire original buffer, this will make
539-
/// a copy of `self` subset of the original buffer in a new `BytesMut`.
540-
///
541-
/// # Examples
542-
///
543-
/// ```
544-
/// use bytes::{Bytes, BytesMut};
545-
///
546-
/// let bytes = Bytes::from(b"hello".to_vec());
547-
/// assert_eq!(bytes.make_mut(), BytesMut::from(&b"hello"[..]));
548-
/// ```
549-
pub fn make_mut(self) -> BytesMut {
550-
let bytes = ManuallyDrop::new(self);
551-
unsafe { (bytes.vtable.to_mut)(&bytes.data, bytes.ptr, bytes.len) }
552-
}
553-
554534
#[inline]
555535
pub(crate) unsafe fn with_vtable(
556536
ptr: *const u8,
@@ -932,6 +912,28 @@ impl From<Box<[u8]>> for Bytes {
932912
}
933913
}
934914

915+
impl From<Bytes> for BytesMut {
916+
/// Convert self into `BytesMut`.
917+
///
918+
/// If `bytes` is unique for the entire original buffer, this will return a
919+
/// `BytesMut` with the contents of `bytes` without copying.
920+
/// If `bytes` is not unique for the entire original buffer, this will make
921+
/// a copy of `bytes` subset of the original buffer in a new `BytesMut`.
922+
///
923+
/// # Examples
924+
///
925+
/// ```
926+
/// use bytes::{Bytes, BytesMut};
927+
///
928+
/// let bytes = Bytes::from(b"hello".to_vec());
929+
/// assert_eq!(BytesMut::from(bytes), BytesMut::from(&b"hello"[..]));
930+
/// ```
931+
fn from(bytes: Bytes) -> Self {
932+
let bytes = ManuallyDrop::new(bytes);
933+
unsafe { (bytes.vtable.to_mut)(&bytes.data, bytes.ptr, bytes.len) }
934+
}
935+
}
936+
935937
impl From<String> for Bytes {
936938
fn from(s: String) -> Bytes {
937939
Bytes::from(s.into_bytes())

src/bytes/promotable.rs

Whitespace-only changes.

tests/test_bytes.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1174,29 +1174,29 @@ fn shared_is_unique() {
11741174
}
11751175

11761176
#[test]
1177-
fn test_bytes_make_mut_static() {
1177+
fn test_bytesmut_from_bytes_static() {
11781178
let bs = b"1b23exfcz3r";
11791179

11801180
// Test STATIC_VTABLE.to_mut
1181-
let bytes_mut = Bytes::from_static(bs).make_mut();
1181+
let bytes_mut = BytesMut::from(Bytes::from_static(bs));
11821182
assert_eq!(bytes_mut, bs[..]);
11831183
}
11841184

11851185
#[test]
1186-
fn test_bytes_make_mut_bytes_mut_vec() {
1186+
fn test_bytesmut_from_bytes_bytes_mut_vec() {
11871187
let bs = b"1b23exfcz3r";
11881188
let bs_long = b"1b23exfcz3r1b23exfcz3r";
11891189

11901190
// Test case where kind == KIND_VEC
11911191
let mut bytes_mut: BytesMut = bs[..].into();
1192-
bytes_mut = bytes_mut.freeze().make_mut();
1192+
bytes_mut = BytesMut::from(bytes_mut.freeze());
11931193
assert_eq!(bytes_mut, bs[..]);
11941194
bytes_mut.extend_from_slice(&bs[..]);
11951195
assert_eq!(bytes_mut, bs_long[..]);
11961196
}
11971197

11981198
#[test]
1199-
fn test_bytes_make_mut_bytes_mut_shared() {
1199+
fn test_bytesmut_from_bytes_bytes_mut_shared() {
12001200
let bs = b"1b23exfcz3r";
12011201

12021202
// Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE
@@ -1207,17 +1207,17 @@ fn test_bytes_make_mut_bytes_mut_shared() {
12071207
let b2 = b1.clone();
12081208

12091209
// shared.is_unique() = False
1210-
let mut b1m = b1.make_mut();
1210+
let mut b1m = BytesMut::from(b1);
12111211
assert_eq!(b1m, bs[..]);
12121212
b1m[0] = b'9';
12131213

12141214
// shared.is_unique() = True
1215-
let b2m = b2.make_mut();
1215+
let b2m = BytesMut::from(b2);
12161216
assert_eq!(b2m, bs[..]);
12171217
}
12181218

12191219
#[test]
1220-
fn test_bytes_make_mut_bytes_mut_offset() {
1220+
fn test_bytesmut_from_bytes_bytes_mut_offset() {
12211221
let bs = b"1b23exfcz3r";
12221222

12231223
// Test bytes_mut.SHARED_VTABLE.to_mut impl where offset != 0
@@ -1227,58 +1227,58 @@ fn test_bytes_make_mut_bytes_mut_offset() {
12271227
let b1 = bytes_mut1.freeze();
12281228
let b2 = bytes_mut2.freeze();
12291229

1230-
let b1m = b1.make_mut();
1231-
let b2m = b2.make_mut();
1230+
let b1m = BytesMut::from(b1);
1231+
let b2m = BytesMut::from(b2);
12321232

12331233
assert_eq!(b2m, bs[9..]);
12341234
assert_eq!(b1m, bs[..9]);
12351235
}
12361236

12371237
#[test]
1238-
fn test_bytes_make_mut_promotable_even_vec() {
1238+
fn test_bytesmut_from_bytes_promotable_even_vec() {
12391239
let vec = vec![33u8; 1024];
12401240

12411241
// Test case where kind == KIND_VEC
12421242
let b1 = Bytes::from(vec.clone());
1243-
let b1m = b1.make_mut();
1243+
let b1m = BytesMut::from(b1);
12441244
assert_eq!(b1m, vec);
12451245
}
12461246

12471247
#[test]
1248-
fn test_bytes_make_mut_promotable_even_arc_1() {
1248+
fn test_bytesmut_from_bytes_promotable_even_arc_1() {
12491249
let vec = vec![33u8; 1024];
12501250

12511251
// Test case where kind == KIND_ARC, ref_cnt == 1
12521252
let b1 = Bytes::from(vec.clone());
12531253
drop(b1.clone());
1254-
let b1m = b1.make_mut();
1254+
let b1m = BytesMut::from(b1);
12551255
assert_eq!(b1m, vec);
12561256
}
12571257

12581258
#[test]
1259-
fn test_bytes_make_mut_promotable_even_arc_2() {
1259+
fn test_bytesmut_from_bytes_promotable_even_arc_2() {
12601260
let vec = vec![33u8; 1024];
12611261

12621262
// Test case where kind == KIND_ARC, ref_cnt == 2
12631263
let b1 = Bytes::from(vec.clone());
12641264
let b2 = b1.clone();
1265-
let b1m = b1.make_mut();
1265+
let b1m = BytesMut::from(b1);
12661266
assert_eq!(b1m, vec);
12671267

12681268
// Test case where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
1269-
let b2m = b2.make_mut();
1269+
let b2m = BytesMut::from(b2);
12701270
assert_eq!(b2m, vec);
12711271
}
12721272

12731273
#[test]
1274-
fn test_bytes_make_mut_promotable_even_arc_offset() {
1274+
fn test_bytesmut_from_bytes_promotable_even_arc_offset() {
12751275
let vec = vec![33u8; 1024];
12761276

12771277
// Test case where offset != 0
12781278
let mut b1 = Bytes::from(vec.clone());
12791279
let b2 = b1.split_off(20);
1280-
let b1m = b1.make_mut();
1281-
let b2m = b2.make_mut();
1280+
let b1m = BytesMut::from(b1);
1281+
let b2m = BytesMut::from(b2);
12821282

12831283
assert_eq!(b2m, vec[20..]);
12841284
assert_eq!(b1m, vec[..20]);

tests/test_bytes_odd_alloc.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::alloc::{GlobalAlloc, Layout, System};
77
use std::ptr;
88

9-
use bytes::Bytes;
9+
use bytes::{Bytes, BytesMut};
1010

1111
#[global_allocator]
1212
static ODD: Odd = Odd;
@@ -97,50 +97,50 @@ fn test_bytes_into_vec() {
9797
}
9898

9999
#[test]
100-
fn test_bytes_make_mut_vec() {
100+
fn test_bytesmut_from_bytes_vec() {
101101
let vec = vec![33u8; 1024];
102102

103103
// Test case where kind == KIND_VEC
104104
let b1 = Bytes::from(vec.clone());
105-
let b1m = b1.make_mut();
105+
let b1m = BytesMut::from(b1);
106106
assert_eq!(b1m, vec);
107107
}
108108

109109
#[test]
110-
fn test_bytes_make_mut_arc_1() {
110+
fn test_bytesmut_from_bytes_arc_1() {
111111
let vec = vec![33u8; 1024];
112112

113113
// Test case where kind == KIND_ARC, ref_cnt == 1
114114
let b1 = Bytes::from(vec.clone());
115115
drop(b1.clone());
116-
let b1m = b1.make_mut();
116+
let b1m = BytesMut::from(b1);
117117
assert_eq!(b1m, vec);
118118
}
119119

120120
#[test]
121-
fn test_bytes_make_mut_arc_2() {
121+
fn test_bytesmut_from_bytes_arc_2() {
122122
let vec = vec![33u8; 1024];
123123

124124
// Test case where kind == KIND_ARC, ref_cnt == 2
125125
let b1 = Bytes::from(vec.clone());
126126
let b2 = b1.clone();
127-
let b1m = b1.make_mut();
127+
let b1m = BytesMut::from(b1);
128128
assert_eq!(b1m, vec);
129129

130130
// Test case where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
131-
let b2m = b2.make_mut();
131+
let b2m = BytesMut::from(b2);
132132
assert_eq!(b2m, vec);
133133
}
134134

135135
#[test]
136-
fn test_bytes_make_mut_arc_offset() {
136+
fn test_bytesmut_from_bytes_arc_offset() {
137137
let vec = vec![33u8; 1024];
138138

139139
// Test case where offset != 0
140140
let mut b1 = Bytes::from(vec.clone());
141141
let b2 = b1.split_off(20);
142-
let b1m = b1.make_mut();
143-
let b2m = b2.make_mut();
142+
let b1m = BytesMut::from(b1);
143+
let b2m = BytesMut::from(b2);
144144

145145
assert_eq!(b2m, vec[20..]);
146146
assert_eq!(b1m, vec[..20]);

0 commit comments

Comments
 (0)