smol_bytes/bytes_mut/
from.rs1use crate::strategy::ImmutableStorage;
2
3use std::{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 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 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 From<bytes::Bytes> for BytesMut {
53 #[cfg_attr(not(coverage), inline(always))]
54 fn from(bytes: bytes::Bytes) -> Self {
55 Self::from_bytes(bytes)
56 }
57}
58
59impl From<bytes::BytesMut> for BytesMut {
60 #[cfg_attr(not(coverage), inline(always))]
61 fn from(bytes: bytes::BytesMut) -> Self {
62 Self::from_bytes_mut(bytes)
63 }
64}
65
66impl<S> From<RawBytes<S>> for BytesMut
67where
68 RawBytes<S>: ImmutableStorage,
69{
70 #[inline]
71 fn from(v: RawBytes<S>) -> Self {
72 use crate::bytes::Repr;
73 match v.repr {
74 Repr::Inline(inline_storage) => BytesMut::from_inline(inline_storage),
75 Repr::Heap(bytes) => BytesMut::from_bytes(bytes),
76 }
77 }
78}
79
80impl From<BytesMut> for bytes::BytesMut {
81 #[cfg_attr(not(coverage), inline(always))]
82 fn from(smol_bytes_mut: BytesMut) -> Self {
83 match smol_bytes_mut.0 {
84 Repr::Inline(storage) => {
85 let mut bytes_mut = bytes::BytesMut::with_capacity(storage.len());
86 bytes_mut.put_slice(storage.as_slice());
87 bytes_mut
88 }
89 Repr::Heap(b) => b,
90 }
91 }
92}
93
94impl From<BytesMut> for bytes::Bytes {
95 #[cfg_attr(not(coverage), inline(always))]
96 fn from(smol_bytes_mut: BytesMut) -> Self {
97 match smol_bytes_mut.0 {
98 Repr::Inline(storage) => bytes::Bytes::copy_from_slice(&storage),
99 Repr::Heap(b) => b.freeze(),
100 }
101 }
102}
103
104impl From<Vec<u8>> for BytesMut {
105 #[cfg_attr(not(coverage), inline(always))]
106 fn from(vec: Vec<u8>) -> Self {
107 if vec.len() <= INLINE_CAP {
108 Self(Repr::Inline(unsafe { Buffer::copy_from_slice(&vec) }))
110 } else {
111 Self(Repr::Heap(bytes::BytesMut::from(bytes::Bytes::from(vec))))
112 }
113 }
114}
115
116impl From<BytesMut> for Vec<u8> {
117 #[cfg_attr(not(coverage), inline(always))]
118 fn from(smol_bytes_mut: BytesMut) -> Self {
119 match smol_bytes_mut.0 {
120 Repr::Inline(storage) => storage.as_slice().to_vec(),
121 Repr::Heap(b) => b.into(),
122 }
123 }
124}
125
126impl From<BytesMut> for Box<[u8]> {
127 #[cfg_attr(not(coverage), inline(always))]
128 fn from(smol_bytes_mut: BytesMut) -> Self {
129 match smol_bytes_mut.0 {
130 Repr::Inline(storage) => storage.as_slice().into(),
131 Repr::Heap(b) => Vec::from(b).into(),
132 }
133 }
134}
135
136impl From<BytesMut> for Arc<[u8]> {
137 #[cfg_attr(not(coverage), inline(always))]
138 fn from(smol_bytes_mut: BytesMut) -> Self {
139 match smol_bytes_mut.0 {
140 Repr::Inline(storage) => Arc::from(storage.as_slice()),
141 Repr::Heap(b) => Arc::from(Vec::from(b)),
142 }
143 }
144}
145
146impl From<BytesMut> for Rc<[u8]> {
147 #[cfg_attr(not(coverage), inline(always))]
148 fn from(smol_bytes_mut: BytesMut) -> Self {
149 match smol_bytes_mut.0 {
150 Repr::Inline(storage) => Rc::from(storage.as_slice()),
151 Repr::Heap(b) => Rc::from(Vec::from(b)),
152 }
153 }
154}
155
156impl From<String> for BytesMut {
157 #[cfg_attr(not(coverage), inline(always))]
158 fn from(s: String) -> Self {
159 Self::from(s.into_bytes())
160 }
161}
162
163impl From<Box<[u8]>> for BytesMut {
164 #[cfg_attr(not(coverage), inline(always))]
165 fn from(vec: Box<[u8]>) -> Self {
166 if vec.len() <= INLINE_CAP {
167 Self(Repr::Inline(unsafe { Buffer::copy_from_slice(&vec) }))
169 } else {
170 Self(Repr::Heap(bytes::BytesMut::from(bytes::Bytes::from(vec))))
171 }
172 }
173}
174
175impl From<Arc<[u8]>> for BytesMut {
176 #[cfg_attr(not(coverage), inline(always))]
177 fn from(arc: Arc<[u8]>) -> Self {
178 Self::from(arc.as_ref())
179 }
180}
181
182impl From<Rc<[u8]>> for BytesMut {
183 #[cfg_attr(not(coverage), inline(always))]
184 fn from(rc: Rc<[u8]>) -> Self {
185 Self::from(rc.as_ref())
186 }
187}