Skip to main content

smol_bytes/bytes_mut/
from.rs

1use crate::strategy::ImmutableStorage;
2
3use std::{borrow::Cow, boxed::Box, rc::Rc, string::String, sync::Arc, vec::Vec};
4
5use super::*;
6
7impl<const N: usize> From<[u8; N]> for BytesMut {
8  #[cfg_attr(not(coverage), inline(always))]
9  fn from(array: [u8; N]) -> Self {
10    Self::from(&array)
11  }
12}
13
14impl<const N: usize> From<&[u8; N]> for BytesMut {
15  #[cfg_attr(not(coverage), inline(always))]
16  fn from(array: &[u8; N]) -> Self {
17    if N <= INLINE_CAP {
18      // SAFETY: len is guaranteed to be less than or equal to INLINE_CAP
19      Self(Repr::Inline(unsafe { Buffer::copy_from_slice(array) }))
20    } else {
21      Self(Repr::Heap(bytes::BytesMut::from(array.as_slice())))
22    }
23  }
24}
25
26impl From<Buffer> for BytesMut {
27  #[cfg_attr(not(coverage), inline(always))]
28  fn from(buffer: Buffer) -> Self {
29    Self(Repr::Inline(buffer))
30  }
31}
32
33impl From<&[u8]> for BytesMut {
34  #[cfg_attr(not(coverage), inline(always))]
35  fn from(slice: &[u8]) -> Self {
36    if slice.len() <= INLINE_CAP {
37      // SAFETY: len is guaranteed to be less than or equal to INLINE_CAP
38      Self(Repr::Inline(unsafe { Buffer::copy_from_slice(slice) }))
39    } else {
40      Self(Repr::Heap(bytes::BytesMut::from(slice)))
41    }
42  }
43}
44
45impl From<&str> for BytesMut {
46  #[cfg_attr(not(coverage), inline(always))]
47  fn from(s: &str) -> Self {
48    Self::from(s.as_bytes())
49  }
50}
51
52impl<'a> From<Cow<'a, [u8]>> for BytesMut {
53  fn from(value: Cow<'a, [u8]>) -> Self {
54    match value {
55      Cow::Borrowed(bytes) => Self::from(bytes),
56      Cow::Owned(bytes) => Self::from(bytes),
57    }
58  }
59}
60
61impl From<bytes::Bytes> for BytesMut {
62  #[cfg_attr(not(coverage), inline(always))]
63  fn from(bytes: bytes::Bytes) -> Self {
64    Self::from_bytes(bytes)
65  }
66}
67
68impl From<bytes::BytesMut> for BytesMut {
69  #[cfg_attr(not(coverage), inline(always))]
70  fn from(bytes: bytes::BytesMut) -> Self {
71    Self::from_bytes_mut(bytes)
72  }
73}
74
75impl<S> From<RawBytes<S>> for BytesMut
76where
77  RawBytes<S>: ImmutableStorage,
78{
79  #[inline]
80  fn from(v: RawBytes<S>) -> Self {
81    use crate::bytes::Repr;
82    match v.repr {
83      Repr::Inline(inline_storage) => BytesMut::from_inline(inline_storage),
84      Repr::Heap(bytes) => BytesMut::from_bytes(bytes),
85    }
86  }
87}
88
89impl From<BytesMut> for bytes::BytesMut {
90  #[cfg_attr(not(coverage), inline(always))]
91  fn from(smol_bytes_mut: BytesMut) -> Self {
92    match smol_bytes_mut.0 {
93      Repr::Inline(storage) => {
94        let mut bytes_mut = bytes::BytesMut::with_capacity(storage.len());
95        bytes_mut.put_slice(storage.as_slice());
96        bytes_mut
97      }
98      Repr::Heap(b) => b,
99    }
100  }
101}
102
103impl From<BytesMut> for bytes::Bytes {
104  #[cfg_attr(not(coverage), inline(always))]
105  fn from(smol_bytes_mut: BytesMut) -> Self {
106    match smol_bytes_mut.0 {
107      Repr::Inline(storage) => bytes::Bytes::copy_from_slice(&storage),
108      Repr::Heap(b) => b.freeze(),
109    }
110  }
111}
112
113impl From<Vec<u8>> for BytesMut {
114  #[cfg_attr(not(coverage), inline(always))]
115  fn from(vec: Vec<u8>) -> Self {
116    if vec.len() <= INLINE_CAP {
117      // SAFETY: len is guaranteed to be less than or equal to INLINE_CAP
118      Self(Repr::Inline(unsafe { Buffer::copy_from_slice(&vec) }))
119    } else {
120      Self(Repr::Heap(bytes::BytesMut::from(bytes::Bytes::from(vec))))
121    }
122  }
123}
124
125impl From<BytesMut> for Vec<u8> {
126  #[cfg_attr(not(coverage), inline(always))]
127  fn from(smol_bytes_mut: BytesMut) -> Self {
128    match smol_bytes_mut.0 {
129      Repr::Inline(storage) => storage.as_slice().to_vec(),
130      Repr::Heap(b) => b.into(),
131    }
132  }
133}
134
135impl From<BytesMut> for Box<[u8]> {
136  #[cfg_attr(not(coverage), inline(always))]
137  fn from(smol_bytes_mut: BytesMut) -> Self {
138    match smol_bytes_mut.0 {
139      Repr::Inline(storage) => storage.as_slice().into(),
140      Repr::Heap(b) => Vec::from(b).into(),
141    }
142  }
143}
144
145impl From<BytesMut> for Arc<[u8]> {
146  #[cfg_attr(not(coverage), inline(always))]
147  fn from(smol_bytes_mut: BytesMut) -> Self {
148    match smol_bytes_mut.0 {
149      Repr::Inline(storage) => Arc::from(storage.as_slice()),
150      Repr::Heap(b) => Arc::from(Vec::from(b)),
151    }
152  }
153}
154
155impl From<BytesMut> for Rc<[u8]> {
156  #[cfg_attr(not(coverage), inline(always))]
157  fn from(smol_bytes_mut: BytesMut) -> Self {
158    match smol_bytes_mut.0 {
159      Repr::Inline(storage) => Rc::from(storage.as_slice()),
160      Repr::Heap(b) => Rc::from(Vec::from(b)),
161    }
162  }
163}
164
165impl From<String> for BytesMut {
166  #[cfg_attr(not(coverage), inline(always))]
167  fn from(s: String) -> Self {
168    Self::from(s.into_bytes())
169  }
170}
171
172impl From<Box<[u8]>> for BytesMut {
173  #[cfg_attr(not(coverage), inline(always))]
174  fn from(vec: Box<[u8]>) -> Self {
175    if vec.len() <= INLINE_CAP {
176      // SAFETY: len is guaranteed to be less than or equal to INLINE_CAP
177      Self(Repr::Inline(unsafe { Buffer::copy_from_slice(&vec) }))
178    } else {
179      Self(Repr::Heap(bytes::BytesMut::from(bytes::Bytes::from(vec))))
180    }
181  }
182}
183
184impl From<Arc<[u8]>> for BytesMut {
185  #[cfg_attr(not(coverage), inline(always))]
186  fn from(arc: Arc<[u8]>) -> Self {
187    Self::from(arc.as_ref())
188  }
189}
190
191impl From<Rc<[u8]>> for BytesMut {
192  #[cfg_attr(not(coverage), inline(always))]
193  fn from(rc: Rc<[u8]>) -> Self {
194    Self::from(rc.as_ref())
195  }
196}