smol_bytes/utf8_bytes_mut/
from.rs1use core::{convert::Infallible, str::FromStr};
2use std::{borrow::Cow, string::String, vec::Vec};
3
4use super::*;
5
6impl From<&str> for Utf8BytesMut {
7 fn from(s: &str) -> Self {
8 Self {
9 inner: BytesMut::from(s.as_bytes()),
10 }
11 }
12}
13
14impl FromStr for Utf8BytesMut {
15 type Err = Infallible;
16
17 fn from_str(s: &str) -> Result<Self, Self::Err> {
18 Ok(Self::from(s))
19 }
20}
21
22impl From<String> for Utf8BytesMut {
23 fn from(s: String) -> Self {
24 Self {
25 inner: BytesMut::from(s.into_bytes()),
26 }
27 }
28}
29
30impl From<crate::Utf8Buffer> for Utf8BytesMut {
31 fn from(value: crate::Utf8Buffer) -> Self {
45 Self {
46 inner: value.into_inner().into(),
47 }
48 }
49}
50
51impl<'a> From<Cow<'a, str>> for Utf8BytesMut {
52 fn from(value: Cow<'a, str>) -> Self {
53 match value {
54 Cow::Borrowed(value) => Self::from(value),
55 Cow::Owned(value) => Self::from(value),
56 }
57 }
58}
59
60impl TryFrom<BytesMut> for Utf8BytesMut {
61 type Error = FromUtf8Error<BytesMut>;
62
63 fn try_from(bytes: BytesMut) -> Result<Self, Self::Error> {
64 Self::from_utf8(bytes)
65 }
66}
67
68impl TryFrom<&[u8]> for Utf8BytesMut {
69 type Error = core::str::Utf8Error;
70
71 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
73 let s = core::str::from_utf8(bytes)?;
74 Ok(Self::from(s))
75 }
76}
77
78impl TryFrom<Vec<u8>> for Utf8BytesMut {
79 type Error = FromUtf8Error<Vec<u8>>;
80
81 fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
82 match core::str::from_utf8(&bytes) {
83 Ok(_) => Ok(Self {
84 inner: BytesMut::from(bytes),
85 }),
86 Err(error) => Err(FromUtf8Error::new(bytes, error)),
87 }
88 }
89}
90
91impl TryFrom<::bytes::Bytes> for Utf8BytesMut {
92 type Error = FromUtf8Error<::bytes::Bytes>;
93
94 fn try_from(bytes: ::bytes::Bytes) -> Result<Self, Self::Error> {
95 match core::str::from_utf8(bytes.as_ref()) {
96 Ok(_) => Ok(Self {
97 inner: BytesMut::from(bytes),
98 }),
99 Err(error) => Err(FromUtf8Error::new(bytes, error)),
100 }
101 }
102}
103
104impl TryFrom<::bytes::BytesMut> for Utf8BytesMut {
105 type Error = FromUtf8Error<::bytes::BytesMut>;
106
107 fn try_from(bytes: ::bytes::BytesMut) -> Result<Self, Self::Error> {
108 match core::str::from_utf8(bytes.as_ref()) {
109 Ok(_) => Ok(Self {
110 inner: BytesMut::from(bytes),
111 }),
112 Err(error) => Err(FromUtf8Error::new(bytes, error)),
113 }
114 }
115}
116
117impl From<Utf8BytesMut> for BytesMut {
118 fn from(value: Utf8BytesMut) -> Self {
119 value.inner
120 }
121}
122
123impl From<Utf8BytesMut> for String {
124 fn from(value: Utf8BytesMut) -> Self {
125 String::from_utf8(value.inner.into()).expect("Utf8Bytes invariant")
126 }
127}
128
129impl From<Utf8BytesMut> for Vec<u8> {
130 fn from(value: Utf8BytesMut) -> Self {
131 value.into_inner().into()
132 }
133}
134
135impl From<Utf8BytesMut> for ::bytes::Bytes {
136 fn from(value: Utf8BytesMut) -> Self {
137 value.into_inner().into()
138 }
139}
140
141impl From<Utf8BytesMut> for ::bytes::BytesMut {
142 fn from(value: Utf8BytesMut) -> Self {
143 value.into_inner().into()
144 }
145}
146
147#[cfg(any(feature = "std", feature = "alloc"))]
148const _: () = {
149 use std::{boxed::Box, rc::Rc, sync::Arc};
150
151 impl From<Utf8BytesMut> for Box<str> {
152 fn from(value: Utf8BytesMut) -> Self {
153 String::from(value).into_boxed_str()
154 }
155 }
156
157 impl From<Utf8BytesMut> for Arc<str> {
158 fn from(value: Utf8BytesMut) -> Self {
159 value.as_str().into()
160 }
161 }
162
163 impl From<Utf8BytesMut> for Rc<str> {
164 fn from(value: Utf8BytesMut) -> Self {
165 value.as_str().into()
166 }
167 }
168
169 impl From<&String> for Utf8BytesMut {
170 fn from(s: &String) -> Self {
171 Self::from(s.as_str())
172 }
173 }
174
175 impl From<Box<str>> for Utf8BytesMut {
176 fn from(s: Box<str>) -> Self {
177 Self::from(String::from(s))
178 }
179 }
180
181 impl From<Arc<str>> for Utf8BytesMut {
182 fn from(s: Arc<str>) -> Self {
183 Self::from(s.as_ref())
184 }
185 }
186
187 impl From<Rc<str>> for Utf8BytesMut {
188 fn from(s: Rc<str>) -> Self {
189 Self::from(s.as_ref())
190 }
191 }
192};