@@ -499,6 +499,11 @@ impl Bytes {
499
499
self . inner . is_inline ( )
500
500
}
501
501
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
+
502
507
/// Returns a slice of self for the provided range.
503
508
///
504
509
/// This will increment the reference count for the underlying memory and
@@ -542,7 +547,7 @@ impl Bytes {
542
547
assert ! ( end <= len) ;
543
548
544
549
if end - begin <= INLINE_CAP {
545
- return Bytes :: from ( & self [ begin..end] ) ;
550
+ return Bytes :: copy_from_slice ( & self [ begin..end] ) ;
546
551
}
547
552
548
553
let mut ret = self . clone ( ) ;
@@ -729,7 +734,7 @@ impl Bytes {
729
734
/// ```
730
735
/// use bytes::Bytes;
731
736
///
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..."[..]);
733
738
///
734
739
/// // Create a shallow clone
735
740
/// let b = a.clone();
@@ -759,7 +764,7 @@ impl Bytes {
759
764
/// Clones the data if it is not already owned.
760
765
pub fn to_mut ( & mut self ) -> & mut BytesMut {
761
766
if !self . inner . is_mut_safe ( ) {
762
- let new = Bytes :: from ( & self [ ..] ) ;
767
+ let new = Self :: copy_from_slice ( & self [ ..] ) ;
763
768
* self = new;
764
769
}
765
770
unsafe { & mut * ( self as * mut Bytes as * mut BytesMut ) }
@@ -922,15 +927,15 @@ impl From<String> for Bytes {
922
927
}
923
928
}
924
929
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)
928
933
}
929
934
}
930
935
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 ( ) )
934
939
}
935
940
}
936
941
0 commit comments