nonzero_char/
convert.rs

1extern crate alloc;
2
3use core::{error::Error, fmt::Display};
4
5use crate::NonZeroChar;
6use alloc::{borrow::Cow, boxed::Box, string::String};
7
8macro_rules! impl_froms {
9    ($($ty:ty),+ $(,)?) => {
10        $(
11            impl From<NonZeroChar> for $ty {
12                fn from(value: NonZeroChar) -> Self {
13                    value.get().into()
14                }
15            }
16        )+
17    };
18}
19macro_rules! impl_try_froms {
20    ($($ty:ty),+ $(,)?) => {
21        $(
22            impl TryFrom<NonZeroChar> for $ty {
23                type Error = <$ty as TryFrom<char>>::Error;
24
25                fn try_from(value: NonZeroChar) -> Result<Self, Self::Error> {
26                    value.get().try_into()
27                }
28            }
29        )+
30    };
31}
32macro_rules! impl_fromiters {
33    ($($ty:ty),+ $(,)?) => {
34        $(
35            impl FromIterator<NonZeroChar> for $ty {
36                fn from_iter<T: IntoIterator<Item = NonZeroChar>>(iter: T) -> Self {
37                    iter.into_iter()
38                        .map(NonZeroChar::get)
39                        .collect()
40                }
41            }
42            impl<'a> FromIterator<&'a NonZeroChar> for $ty {
43                fn from_iter<T: IntoIterator<Item = &'a NonZeroChar>>(iter: T) -> Self {
44                    iter.into_iter()
45                        .copied()
46                        .map(NonZeroChar::get)
47                        .collect()
48                }
49            }
50        )+
51    };
52}
53
54/// Try from byte errors
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct TryFromByteError(());
57#[allow(deprecated)]
58impl Display for TryFromByteError {
59    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60        self.description().fmt(f)
61    }
62}
63impl Error for TryFromByteError {
64    fn description(&self) -> &str {
65        "converted integer out of range for `NonZeroChar`"
66    }
67}
68
69impl_froms! {
70    u32,
71    u64,
72    u128,
73    String,
74}
75impl_try_froms! {
76    u8,
77    u16,
78}
79impl_fromiters! {
80    String,
81    Box<str>,
82    Cow<'_, str>,
83}
84impl TryFrom<u8> for NonZeroChar {
85    type Error = TryFromByteError;
86
87    fn try_from(value: u8) -> Result<Self, Self::Error> {
88        NonZeroChar::new(value.into())
89            .ok_or(TryFromByteError(()))
90    }
91}
92impl TryFrom<u32> for NonZeroChar {
93    type Error = TryFromByteError;
94
95    fn try_from(value: u32) -> Result<Self, Self::Error> {
96        match value.try_into() {
97            Ok(ch) => {
98                NonZeroChar::new(ch)
99                    .ok_or(TryFromByteError(()))
100            },
101            Err(_) => Err(TryFromByteError(())),
102        }
103    }
104}
105impl Extend<NonZeroChar> for String {
106    fn extend<T: IntoIterator<Item = NonZeroChar>>(&mut self, iter: T) {
107        self.extend(iter.into_iter().map(NonZeroChar::get));
108    }
109}
110impl<'a> Extend<&'a NonZeroChar> for String {
111    fn extend<T: IntoIterator<Item = &'a NonZeroChar>>(&mut self, iter: T) {
112        self.extend(iter.into_iter().copied().map(NonZeroChar::get));
113    }
114}