Skip to content

Commit ae9991f

Browse files
author
Douman
committed
Make From only for static slices to Bytes
1 parent b6cb346 commit ae9991f

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/bytes.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ impl Bytes {
499499
self.inner.is_inline()
500500
}
501501

502+
///Creates `Bytes` instance from slice, by copying it.
503+
pub fn copy_from_slice(data: &[u8]) -> Self {
504+
BytesMut::from(data).freeze()
505+
}
506+
502507
/// Returns a slice of self for the provided range.
503508
///
504509
/// This will increment the reference count for the underlying memory and
@@ -542,7 +547,7 @@ impl Bytes {
542547
assert!(end <= len);
543548

544549
if end - begin <= INLINE_CAP {
545-
return Bytes::from(&self[begin..end]);
550+
return Bytes::copy_from_slice(&self[begin..end]);
546551
}
547552

548553
let mut ret = self.clone();
@@ -729,7 +734,7 @@ impl Bytes {
729734
/// ```
730735
/// use bytes::Bytes;
731736
///
732-
/// let a = Bytes::from(&b"Mary had a little lamb, little lamb, little lamb..."[..]);
737+
/// let a = Bytes::copy_from_slice(&b"Mary had a little lamb, little lamb, little lamb..."[..]);
733738
///
734739
/// // Create a shallow clone
735740
/// let b = a.clone();
@@ -759,7 +764,7 @@ impl Bytes {
759764
/// Clones the data if it is not already owned.
760765
pub fn to_mut(&mut self) -> &mut BytesMut {
761766
if !self.inner.is_mut_safe() {
762-
let new = Bytes::from(&self[..]);
767+
let new = Self::copy_from_slice(&self[..]);
763768
*self = new;
764769
}
765770
unsafe { &mut *(self as *mut Bytes as *mut BytesMut) }
@@ -922,15 +927,15 @@ impl From<String> for Bytes {
922927
}
923928
}
924929

925-
impl<'a> From<&'a [u8]> for Bytes {
926-
fn from(src: &'a [u8]) -> Bytes {
927-
BytesMut::from(src).freeze()
930+
impl From<&'static [u8]> for Bytes {
931+
fn from(src: &'static [u8]) -> Bytes {
932+
Bytes::from_static(src)
928933
}
929934
}
930935

931-
impl<'a> From<&'a str> for Bytes {
932-
fn from(src: &'a str) -> Bytes {
933-
BytesMut::from(src).freeze()
936+
impl From<&'static str> for Bytes {
937+
fn from(src: &'static str) -> Bytes {
938+
Bytes::from_static(src.as_bytes())
934939
}
935940
}
936941

tests/test_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ fn stress() {
528528

529529
for i in 0..ITERS {
530530
let data = [i as u8; 256];
531-
let buf = Arc::new(Bytes::from(&data[..]));
531+
let buf = Arc::new(Bytes::copy_from_slice(&data[..]));
532532

533533
let barrier = Arc::new(Barrier::new(THREADS));
534534
let mut joins = Vec::with_capacity(THREADS);
@@ -888,7 +888,7 @@ fn slice_ref_catches_not_an_empty_subset() {
888888
#[test]
889889
#[should_panic]
890890
fn empty_slice_ref_catches_not_an_empty_subset() {
891-
let bytes = Bytes::from(&b""[..]);
891+
let bytes = Bytes::copy_from_slice(&b""[..]);
892892
let slice = &b""[0..0];
893893

894894
bytes.slice_ref(slice);

0 commit comments

Comments
 (0)