str_array/
convert.rs

1use super::*;
2
3impl<const N: usize> FromStr for StrArray<N> {
4    type Err = StrLenError<N>;
5
6    fn from_str(s: &str) -> Result<Self, Self::Err> {
7        Self::new(s)
8    }
9}
10
11impl<const N: usize> TryFrom<&str> for StrArray<N> {
12    type Error = StrLenError<N>;
13
14    fn try_from(val: &str) -> Result<Self, Self::Error> {
15        Self::new(val)
16    }
17}
18
19impl<const N: usize> TryFrom<[u8; N]> for StrArray<N> {
20    type Error = Utf8Error;
21
22    fn try_from(val: [u8; N]) -> Result<Self, Self::Error> {
23        Self::from_utf8(&val)
24    }
25}
26
27impl<const N: usize> TryFrom<&[u8; N]> for StrArray<N> {
28    type Error = Utf8Error;
29
30    fn try_from(val: &[u8; N]) -> Result<Self, Self::Error> {
31        Self::from_utf8(val)
32    }
33}
34
35impl<'a, const N: usize> TryFrom<&'a [u8; N]> for &'a StrArray<N> {
36    type Error = Utf8Error;
37
38    fn try_from(val: &'a [u8; N]) -> Result<Self, Self::Error> {
39        StrArray::ref_from_utf8(val)
40    }
41}
42
43impl<'a, const N: usize> TryFrom<&'a mut [u8; N]> for &'a mut StrArray<N> {
44    type Error = Utf8Error;
45
46    fn try_from(val: &'a mut [u8; N]) -> Result<Self, Self::Error> {
47        StrArray::mut_from_utf8(val)
48    }
49}
50
51impl<'a, const N: usize> TryFrom<&'a str> for &'a StrArray<N> {
52    type Error = StrLenError<N>;
53
54    fn try_from(val: &'a str) -> Result<Self, Self::Error> {
55        StrArray::ref_from_str(val)
56    }
57}
58
59impl<'a, const N: usize> TryFrom<&'a mut str> for &'a mut StrArray<N> {
60    type Error = StrLenError<N>;
61
62    fn try_from(val: &'a mut str) -> Result<Self, Self::Error> {
63        StrArray::mut_from_str(val)
64    }
65}
66
67impl<'a, const N: usize> From<&'a StrArray<N>> for &'a str {
68    fn from(val: &'a StrArray<N>) -> Self {
69        val
70    }
71}
72
73impl<'a, const N: usize> From<&'a mut StrArray<N>> for &'a mut str {
74    fn from(val: &'a mut StrArray<N>) -> Self {
75        val
76    }
77}
78
79impl<'a, const N: usize> From<&'a StrArray<N>> for &'a [u8; N] {
80    fn from(val: &'a StrArray<N>) -> Self {
81        val.as_bytes()
82    }
83}
84
85impl<'a, const N: usize> From<&'a StrArray<N>> for &'a [u8] {
86    fn from(val: &'a StrArray<N>) -> Self {
87        val.as_bytes()
88    }
89}
90
91impl<const N: usize> From<StrArray<N>> for [u8; N] {
92    fn from(s: StrArray<N>) -> Self {
93        s.into_bytes()
94    }
95}
96
97impl<const N: usize> AsRef<str> for StrArray<N> {
98    fn as_ref(&self) -> &str {
99        self.as_str()
100    }
101}
102
103impl<const N: usize> AsRef<[u8; N]> for StrArray<N> {
104    fn as_ref(&self) -> &[u8; N] {
105        self.as_bytes()
106    }
107}
108
109impl<const N: usize> AsRef<[u8]> for StrArray<N> {
110    fn as_ref(&self) -> &[u8] {
111        self.as_bytes()
112    }
113}
114
115impl<const N: usize> AsMut<str> for StrArray<N> {
116    fn as_mut(&mut self) -> &mut str {
117        self.as_mut_str()
118    }
119}
120
121#[cfg(feature = "alloc")]
122mod alloc {
123    use super::*;
124
125    impl<const N: usize> Borrow<str> for StrArray<N> {
126        fn borrow(&self) -> &str {
127            self.as_str()
128        }
129    }
130
131    impl<const N: usize> BorrowMut<str> for StrArray<N> {
132        fn borrow_mut(&mut self) -> &mut str {
133            self.as_mut_str()
134        }
135    }
136
137    impl<const N: usize> TryFrom<String> for StrArray<N> {
138        type Error = StrLenError<N>;
139
140        fn try_from(val: String) -> Result<Self, Self::Error> {
141            Self::try_from(val.as_str())
142        }
143    }
144
145    impl<const N: usize> TryFrom<Box<str>> for StrArray<N> {
146        type Error = StrLenError<N>;
147
148        fn try_from(val: Box<str>) -> Result<Self, Self::Error> {
149            Self::try_from(val.as_ref())
150        }
151    }
152
153    impl<const N: usize> TryFrom<Rc<str>> for StrArray<N> {
154        type Error = StrLenError<N>;
155
156        fn try_from(val: Rc<str>) -> Result<Self, Self::Error> {
157            Self::try_from(val.as_ref())
158        }
159    }
160
161    impl<const N: usize> TryFrom<Arc<str>> for StrArray<N> {
162        type Error = StrLenError<N>;
163
164        fn try_from(val: Arc<str>) -> Result<Self, Self::Error> {
165            Self::try_from(val.as_ref())
166        }
167    }
168
169    impl<const N: usize> From<StrArray<N>> for String {
170        fn from(s: StrArray<N>) -> Self {
171            s.as_str().into()
172        }
173    }
174
175    impl<const N: usize> From<StrArray<N>> for Box<str> {
176        fn from(s: StrArray<N>) -> Self {
177            Box::from(s.as_str())
178        }
179    }
180
181    impl<const N: usize> From<StrArray<N>> for Rc<str> {
182        fn from(s: StrArray<N>) -> Self {
183            Rc::from(s.as_str())
184        }
185    }
186
187    impl<const N: usize> From<StrArray<N>> for Arc<str> {
188        fn from(s: StrArray<N>) -> Self {
189            Arc::from(s.as_str())
190        }
191    }
192}
193
194#[cfg(test)]
195mod tests {
196    use super::*;
197    use core::borrow::{Borrow, BorrowMut};
198
199    #[test]
200    fn test_borrow() {
201        let s = str_array!("hello");
202        let borrowed: &str = s.borrow();
203        assert_eq!(borrowed, "hello");
204    }
205
206    #[test]
207    fn test_borrow_mut() {
208        let mut s = str_array!("hello");
209        let borrowed: &mut str = s.borrow_mut();
210        borrowed.make_ascii_uppercase();
211        assert_eq!(borrowed, "HELLO");
212    }
213
214    #[test]
215    fn test_from_str() {
216        let s: StrArray<5> = "hello".parse().unwrap();
217        assert_eq!(s, "hello");
218    }
219
220    #[test]
221    fn test_try_from_str() {
222        let s: StrArray<5> = "hello".try_into().unwrap();
223        assert_eq!(s, "hello");
224    }
225
226    #[test]
227    fn test_try_from_str_err() {
228        let s: Result<StrArray<3>, _> = "hello".try_into();
229        assert!(s.is_err());
230    }
231
232    #[test]
233    fn test_try_from_bytes() {
234        let s: StrArray<5> = StrArray::try_from(*b"hello").unwrap();
235        assert_eq!(s, "hello");
236    }
237
238    #[test]
239    fn test_try_from_bytes_slice() {
240        let s: StrArray<5> = b"hello".try_into().unwrap();
241        assert_eq!(s, "hello");
242    }
243
244    #[test]
245    fn test_into_bytes() {
246        let s: StrArray<5> = "hello".try_into().unwrap();
247        let bytes: [u8; 5] = s.into();
248        assert_eq!(&bytes, b"hello");
249    }
250
251    #[test]
252    fn test_as_ref_str() {
253        let s: StrArray<5> = "hello".try_into().unwrap();
254        let r: &str = s.as_ref();
255        assert_eq!(r, "hello");
256    }
257
258    #[test]
259    fn test_as_ref_bytes() {
260        let s: StrArray<5> = "hello".try_into().unwrap();
261        let r: &[u8; 5] = s.as_ref();
262        assert_eq!(r, b"hello");
263    }
264
265    #[test]
266    fn test_as_ref_byte_slice() {
267        let s: StrArray<5> = "hello".try_into().unwrap();
268        let r: &[u8] = s.as_ref();
269        assert_eq!(r, b"hello");
270    }
271
272    #[test]
273    fn test_as_mut_str() {
274        let mut s: StrArray<5> = "hello".try_into().unwrap();
275        let mut_s: &mut str = s.as_mut();
276        mut_s.make_ascii_uppercase();
277        assert_eq!(mut_s, "HELLO");
278    }
279
280    #[test]
281    fn test_try_from_mut_str() {
282        let mut s = *b"hello";
283        let s = str::from_utf8_mut(&mut s).unwrap();
284        let sa: &mut StrArray<5> = <&mut StrArray<5>>::try_from(&mut *s).unwrap();
285        sa.make_ascii_uppercase();
286        assert_eq!(s, "HELLO");
287    }
288
289    #[test]
290    fn test_try_from_mut_bytes() {
291        let mut b = *b"hello";
292        let sa: &mut StrArray<5> = <&mut StrArray<5>>::try_from(&mut b).unwrap();
293        sa.make_ascii_uppercase();
294        assert_eq!(sa, "HELLO");
295    }
296
297    #[test]
298    fn test_from_strarray_for_str() {
299        let s = str_array!("hello");
300        let r: &str = (&s).into();
301        assert_eq!(r, "hello");
302    }
303
304    #[test]
305    fn test_from_mut_strarray_for_mut_str() {
306        let mut s = str_array!("hello");
307        let r: &mut str = (&mut s).into();
308        r.make_ascii_uppercase();
309        assert_eq!(r, "HELLO");
310    }
311
312    #[test]
313    fn test_from_strarray_for_bytes() {
314        let s = str_array!("hello");
315        let r: &[u8; 5] = (&s).into();
316        assert_eq!(r, b"hello");
317    }
318
319    #[test]
320    fn test_from_strarray_for_byte_slice() {
321        let s = str_array!("hello");
322        let r: &[u8] = (&s).into();
323        assert_eq!(r, b"hello");
324    }
325}
326
327#[cfg(all(test, feature = "alloc"))]
328mod alloc_tests {
329    use super::*;
330    use ::alloc::{boxed::Box, rc::Rc, string::String, sync::Arc};
331
332    #[test]
333    fn test_try_from_string() {
334        let s = String::from("hello");
335        let sa: StrArray<5> = s.try_into().unwrap();
336        assert_eq!(sa, "hello");
337    }
338
339    #[test]
340    fn test_try_from_box_str() {
341        let s = String::from("hello").into_boxed_str();
342        let sa: StrArray<5> = s.try_into().unwrap();
343        assert_eq!(sa, "hello");
344    }
345
346    #[test]
347    fn test_try_from_rc_str() {
348        let s: Rc<str> = "hello".into();
349        let sa: StrArray<5> = s.try_into().unwrap();
350        assert_eq!(sa, "hello");
351    }
352
353    #[test]
354    fn test_try_from_arc_str() {
355        let s: Arc<str> = "hello".into();
356        let sa: StrArray<5> = s.try_into().unwrap();
357        assert_eq!(sa, "hello");
358    }
359
360    #[test]
361    fn test_into_string() {
362        let s = str_array!("hello");
363        let string: String = s.into();
364        assert_eq!(string, "hello");
365    }
366
367    #[test]
368    fn test_into_box_str() {
369        let s = str_array!("hello");
370        let b: Box<str> = s.into();
371        assert_eq!(&*b, "hello");
372    }
373
374    #[test]
375    fn test_into_rc_str() {
376        let s = str_array!("hello");
377        let r: Rc<str> = s.into();
378        assert_eq!(&*r, "hello");
379    }
380
381    #[test]
382    fn test_into_arc_str() {
383        let s = str_array!("hello");
384        let a: Arc<str> = s.into();
385        assert_eq!(&*a, "hello");
386    }
387}