Skip to main content

smol_bytes/utf8_buffer/
from.rs

1use core::str::FromStr;
2#[cfg(any(feature = "std", feature = "alloc"))]
3use std::string::ToString;
4
5use super::*;
6
7impl Utf8Buffer {
8  /// Validates a fixed buffer as UTF-8 without discarding it on failure.
9  pub fn from_utf8(buffer: Buffer) -> Result<Self, FromUtf8Error<Buffer>> {
10    match core::str::from_utf8(buffer.as_slice()) {
11      Ok(_) => Ok(Self { inner: buffer }),
12      Err(error) => Err(FromUtf8Error::new(buffer, error)),
13    }
14  }
15
16  /// Tries to create a `Utf8Buffer` from a string slice.
17  pub fn try_from_str(s: &str) -> Result<Self, TryPutError> {
18    let inner = Buffer::try_from(s.as_bytes())?;
19    Ok(Self { inner })
20  }
21}
22
23#[cfg(any(feature = "std", feature = "alloc"))]
24impl<S> crate::utf8_bytes::Utf8Bytes<S> {
25  /// Tries to move this validated UTF-8 view into a fixed [`Utf8Buffer`].
26  ///
27  /// Inline values move their inline storage while preserving the cursor.
28  /// Short heap views are copied into inline storage, while longer values are
29  /// returned unchanged.
30  pub fn try_into_buffer(self) -> Result<Utf8Buffer, Self> {
31    match self.inner.try_into_buffer() {
32      Ok(inner) => Ok(Utf8Buffer { inner }),
33      Err(inner) => Err(Self { inner }),
34    }
35  }
36}
37
38#[cfg(any(feature = "std", feature = "alloc"))]
39impl crate::Utf8BytesMut {
40  /// Tries to move this validated mutable UTF-8 view into a fixed
41  /// [`Utf8Buffer`].
42  ///
43  /// Inline values move their inline storage while preserving the cursor.
44  /// Short heap views are copied into inline storage, while longer values are
45  /// returned unchanged.
46  pub fn try_into_buffer(self) -> Result<Utf8Buffer, Self> {
47    match self.inner.try_into_buffer() {
48      Ok(inner) => Ok(Utf8Buffer { inner }),
49      Err(inner) => Err(Self { inner }),
50    }
51  }
52}
53
54impl TryFrom<&str> for Utf8Buffer {
55  type Error = TryPutError;
56
57  fn try_from(s: &str) -> Result<Self, Self::Error> {
58    Self::try_from_str(s)
59  }
60}
61
62impl FromStr for Utf8Buffer {
63  type Err = TryPutError;
64
65  fn from_str(s: &str) -> Result<Self, Self::Err> {
66    Self::try_from_str(s)
67  }
68}
69
70impl From<Utf8Buffer> for Buffer {
71  #[cfg_attr(not(coverage), inline(always))]
72  fn from(value: Utf8Buffer) -> Self {
73    value.inner
74  }
75}
76
77impl TryFrom<Buffer> for Utf8Buffer {
78  type Error = FromUtf8Error<Buffer>;
79
80  fn try_from(buffer: Buffer) -> Result<Self, Self::Error> {
81    Self::from_utf8(buffer)
82  }
83}
84
85impl TryFrom<&[u8]> for Utf8Buffer {
86  type Error = FromBytesError;
87
88  /// Creates a `Utf8Buffer` from a byte slice, validating UTF-8 and
89  /// checking inline capacity.
90  fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
91    let s = core::str::from_utf8(bytes)?;
92    Ok(Self::try_from_str(s)?)
93  }
94}
95
96#[cfg(any(feature = "std", feature = "alloc"))]
97const _: () = {
98  use std::{boxed::Box, rc::Rc, string::String, sync::Arc, vec::Vec};
99
100  impl From<Utf8Buffer> for String {
101    fn from(value: Utf8Buffer) -> Self {
102      value.as_str().to_string()
103    }
104  }
105
106  impl From<Utf8Buffer> for Vec<u8> {
107    fn from(value: Utf8Buffer) -> Self {
108      value.into_inner().as_slice().to_vec()
109    }
110  }
111
112  impl From<Utf8Buffer> for ::bytes::Bytes {
113    fn from(value: Utf8Buffer) -> Self {
114      ::bytes::Bytes::copy_from_slice(value.as_str().as_bytes())
115    }
116  }
117
118  impl From<Utf8Buffer> for ::bytes::BytesMut {
119    fn from(value: Utf8Buffer) -> Self {
120      ::bytes::BytesMut::from(value.as_str().as_bytes())
121    }
122  }
123
124  impl From<Utf8Buffer> for Box<str> {
125    fn from(value: Utf8Buffer) -> Self {
126      value.as_str().into()
127    }
128  }
129
130  impl From<Utf8Buffer> for Arc<str> {
131    fn from(value: Utf8Buffer) -> Self {
132      value.as_str().into()
133    }
134  }
135
136  impl From<Utf8Buffer> for Rc<str> {
137    fn from(value: Utf8Buffer) -> Self {
138      value.as_str().into()
139    }
140  }
141
142  impl TryFrom<String> for Utf8Buffer {
143    type Error = TryPutError;
144
145    fn try_from(s: String) -> Result<Self, Self::Error> {
146      Self::try_from_str(s.as_str())
147    }
148  }
149
150  impl TryFrom<&String> for Utf8Buffer {
151    type Error = TryPutError;
152
153    fn try_from(s: &String) -> Result<Self, Self::Error> {
154      Self::try_from_str(s.as_str())
155    }
156  }
157
158  impl TryFrom<Box<str>> for Utf8Buffer {
159    type Error = TryPutError;
160
161    fn try_from(s: Box<str>) -> Result<Self, Self::Error> {
162      Self::try_from_str(s.as_ref())
163    }
164  }
165
166  impl TryFrom<Arc<str>> for Utf8Buffer {
167    type Error = TryPutError;
168
169    fn try_from(s: Arc<str>) -> Result<Self, Self::Error> {
170      Self::try_from_str(s.as_ref())
171    }
172  }
173
174  impl TryFrom<Rc<str>> for Utf8Buffer {
175    type Error = TryPutError;
176
177    fn try_from(s: Rc<str>) -> Result<Self, Self::Error> {
178      Self::try_from_str(s.as_ref())
179    }
180  }
181};