aboutsummaryrefslogtreecommitdiff
path: root/src/bytes.rs
diff options
context:
space:
mode:
authorGravatar Douman <douman@gmx.se> 2019-08-16 08:50:24 +0200
committerGravatar Douman <douman@gmx.se> 2019-08-27 22:17:26 +0200
commitae9991f3ba4f08d9c3d57b240a3c018082a38012 (patch)
tree9597dfe493c16922d95afdc364c543c626fbf469 /src/bytes.rs
parentb6cb346adfaae89bce44bfa337652e6d218d38c4 (diff)
downloadbytes-ae9991f3ba4f08d9c3d57b240a3c018082a38012.tar.gz
bytes-ae9991f3ba4f08d9c3d57b240a3c018082a38012.tar.zst
bytes-ae9991f3ba4f08d9c3d57b240a3c018082a38012.zip
Make From only for static slices to Bytes
Diffstat (limited to 'src/bytes.rs')
-rw-r--r--src/bytes.rs23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/bytes.rs b/src/bytes.rs
index 99022f0..6a9c959 100644
--- a/src/bytes.rs
+++ b/src/bytes.rs
@@ -499,6 +499,11 @@ impl Bytes {
self.inner.is_inline()
}
+ ///Creates `Bytes` instance from slice, by copying it.
+ pub fn copy_from_slice(data: &[u8]) -> Self {
+ BytesMut::from(data).freeze()
+ }
+
/// Returns a slice of self for the provided range.
///
/// This will increment the reference count for the underlying memory and
@@ -542,7 +547,7 @@ impl Bytes {
assert!(end <= len);
if end - begin <= INLINE_CAP {
- return Bytes::from(&self[begin..end]);
+ return Bytes::copy_from_slice(&self[begin..end]);
}
let mut ret = self.clone();
@@ -729,7 +734,7 @@ impl Bytes {
/// ```
/// use bytes::Bytes;
///
- /// let a = Bytes::from(&b"Mary had a little lamb, little lamb, little lamb..."[..]);
+ /// let a = Bytes::copy_from_slice(&b"Mary had a little lamb, little lamb, little lamb..."[..]);
///
/// // Create a shallow clone
/// let b = a.clone();
@@ -759,7 +764,7 @@ impl Bytes {
/// Clones the data if it is not already owned.
pub fn to_mut(&mut self) -> &mut BytesMut {
if !self.inner.is_mut_safe() {
- let new = Bytes::from(&self[..]);
+ let new = Self::copy_from_slice(&self[..]);
*self = new;
}
unsafe { &mut *(self as *mut Bytes as *mut BytesMut) }
@@ -922,15 +927,15 @@ impl From<String> for Bytes {
}
}
-impl<'a> From<&'a [u8]> for Bytes {
- fn from(src: &'a [u8]) -> Bytes {
- BytesMut::from(src).freeze()
+impl From<&'static [u8]> for Bytes {
+ fn from(src: &'static [u8]) -> Bytes {
+ Bytes::from_static(src)
}
}
-impl<'a> From<&'a str> for Bytes {
- fn from(src: &'a str) -> Bytes {
- BytesMut::from(src).freeze()
+impl From<&'static str> for Bytes {
+ fn from(src: &'static str) -> Bytes {
+ Bytes::from_static(src.as_bytes())
}
}