spinoso_string/enc/utf8/borrowed/
impls.rs1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use core::array::TryFromSliceError;
4use core::fmt;
5use core::hash::{Hash, Hasher};
6use core::ops::{Index, IndexMut};
7use core::slice::SliceIndex;
8use core::str;
9
10use super::Utf8Str;
11
12impl Default for &'_ Utf8Str {
13 #[inline]
14 fn default() -> Self {
15 Utf8Str::empty()
16 }
17}
18
19impl Hash for Utf8Str {
20 fn hash<H: Hasher>(&self, hasher: &mut H) {
21 self.as_bytes().hash(hasher);
22 }
23}
24
25impl fmt::Debug for Utf8Str {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 use bstr::ByteSlice;
28
29 f.debug_struct("Utf8Str")
30 .field("bytes", &self.as_bytes().as_bstr())
31 .finish()
32 }
33}
34
35impl<I: SliceIndex<[u8]>> Index<I> for Utf8Str {
36 type Output = I::Output;
37
38 #[inline]
39 fn index(&self, index: I) -> &Self::Output {
40 Index::index(self.as_bytes(), index)
41 }
42}
43
44impl<I: SliceIndex<[u8]>> IndexMut<I> for Utf8Str {
45 #[inline]
46 fn index_mut(&mut self, index: I) -> &mut Self::Output {
47 IndexMut::index_mut(self.as_bytes_mut(), index)
48 }
49}
50
51impl AsRef<[u8]> for Utf8Str {
52 #[inline]
53 fn as_ref(&self) -> &[u8] {
54 self.as_bytes()
55 }
56}
57
58impl AsMut<[u8]> for Utf8Str {
59 #[inline]
60 fn as_mut(&mut self) -> &mut [u8] {
61 self.as_bytes_mut()
62 }
63}
64
65impl AsRef<Utf8Str> for Utf8Str {
66 #[inline]
67 fn as_ref(&self) -> &Utf8Str {
68 self
69 }
70}
71
72impl AsMut<Utf8Str> for Utf8Str {
73 #[inline]
74 fn as_mut(&mut self) -> &mut Utf8Str {
75 self
76 }
77}
78
79impl<'a, const N: usize> From<&'a [u8; N]> for &'a Utf8Str {
80 #[inline]
81 fn from(content: &'a [u8; N]) -> Self {
82 Utf8Str::new(content)
83 }
84}
85
86impl<'a, const N: usize> From<&'a mut [u8; N]> for &'a mut Utf8Str {
87 #[inline]
88 fn from(content: &'a mut [u8; N]) -> Self {
89 Utf8Str::new_mut(content)
90 }
91}
92
93impl<'a> From<&'a [u8]> for &'a Utf8Str {
94 #[inline]
95 fn from(content: &'a [u8]) -> Self {
96 Utf8Str::new(content)
97 }
98}
99
100impl<'a> From<&'a mut [u8]> for &'a mut Utf8Str {
101 #[inline]
102 fn from(content: &'a mut [u8]) -> Self {
103 Utf8Str::new_mut(content)
104 }
105}
106
107impl<'a> From<&'a str> for &'a Utf8Str {
108 #[inline]
109 fn from(s: &'a str) -> Self {
110 Utf8Str::new(s)
111 }
112}
113
114impl<'a> From<&'a Utf8Str> for &'a [u8] {
115 #[inline]
116 fn from(content: &'a Utf8Str) -> Self {
117 content.as_bytes()
118 }
119}
120
121impl<'a> From<&'a mut Utf8Str> for &'a mut [u8] {
122 #[inline]
123 fn from(content: &'a mut Utf8Str) -> Self {
124 content.as_bytes_mut()
125 }
126}
127
128impl<'a, const N: usize> TryFrom<&'a Utf8Str> for &'a [u8; N] {
129 type Error = &'a Utf8Str;
130
131 #[inline]
132 fn try_from(content: &'a Utf8Str) -> Result<Self, Self::Error> {
133 if let Ok(arr) = content.as_bytes().try_into() {
134 Ok(arr)
135 } else {
136 Err(content)
137 }
138 }
139}
140
141impl<'a, const N: usize> TryFrom<&'a mut Utf8Str> for &'a mut [u8; N] {
142 type Error = TryFromSliceError;
143
144 #[inline]
145 fn try_from(content: &'a mut Utf8Str) -> Result<Self, Self::Error> {
146 content.as_bytes_mut().try_into()
147 }
148}
149
150impl<'a> From<&'a Utf8Str> for Cow<'a, [u8]> {
151 #[inline]
152 fn from(content: &'a Utf8Str) -> Self {
153 Cow::Borrowed(content.as_bytes())
154 }
155}
156
157impl<'a> TryFrom<&'a Utf8Str> for &'a str {
158 type Error = &'a Utf8Str;
159
160 #[inline]
161 fn try_from(s: &'a Utf8Str) -> Result<Self, Self::Error> {
162 if s.is_valid_encoding() {
163 let slice = s.as_bytes();
164 let s = unsafe { str::from_utf8_unchecked(slice) };
167 Ok(s)
168 } else {
169 Err(s)
170 }
171 }
172}
173
174impl<'a> TryFrom<&'a Utf8Str> for Cow<'a, str> {
175 type Error = &'a Utf8Str;
176
177 #[inline]
178 fn try_from(content: &'a Utf8Str) -> Result<Self, Self::Error> {
179 let s = content.try_into()?;
180 Ok(Cow::Borrowed(s))
181 }
182}
183
184impl<'a> From<&'a Utf8Str> for Cow<'a, Utf8Str> {
185 #[inline]
186 fn from(content: &'a Utf8Str) -> Self {
187 Cow::Borrowed(content)
188 }
189}
190
191impl From<Box<[u8]>> for Box<Utf8Str> {
192 #[inline]
193 fn from(s: Box<[u8]>) -> Self {
194 Utf8Str::from_boxed_bytes(s)
195 }
196}
197
198impl From<Box<Utf8Str>> for Box<[u8]> {
199 #[inline]
200 fn from(s: Box<Utf8Str>) -> Self {
201 Utf8Str::into_boxed_bytes(s)
202 }
203}