devela/text/char/
core_impls.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// devela::text::char::core_impls
//
//! implementations of core traits
//

#[allow(unused_imports)]
use crate::{DataOverflow, _core::fmt, paste, text::char::*, unwrap, ConstDefault};

/* Default, Display, Debug */

///
macro_rules! char_core_impls {
    () => {
        char_core_impls![
            char7 | "_char7" + Self(unwrap![some NonExtremeU8::new(0)]),
            char8 | "_char8" + Self(0),
            char16 | "_char16" + Self(unwrap![some NonSurrogateU16::new(0)])
        ];
    };
    ($( $name:ident | $feature:literal + $default:expr ),+ ) => {
        $(
            #[cfg(feature = $feature)]
            char_core_impls![@$name + $default];
        )+
    };
    (@$name:ident + $default:expr) => { paste! {
        impl Default for super::$name {
            /// Returns the default value of `\x00` (nul character).
            #[must_use]
            fn default() -> Self { $default }
        }
        impl ConstDefault for super::$name {
            /// Returns the default value of `\x00` (nul character).
            const DEFAULT: Self = $default;
        }
        impl fmt::Display for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{}", self.to_char())
            }
        }
        impl fmt::Debug for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{:?}", self.to_char())
            }
        }
        impl fmt::Binary for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Binary::fmt(&self.to_u32(), f)
            }
        }
        impl fmt::LowerHex for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::LowerHex::fmt(&self.to_u32(), f)
            }
        }
        impl fmt::UpperHex for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::UpperHex::fmt(&self.to_u32(), f)
            }
        }
        impl fmt::Octal for super::$name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Octal::fmt(&self.to_u32(), f)
            }
        }
    }};
}
#[rustfmt::skip]
char_core_impls!();

/* From char7 */

#[cfg(feature = "_char7")]
mod c7 {
    use super::super::*;

    impl From<char7> for char {
        #[must_use]
        fn from(c: char7) -> char {
            c.to_char()
        }
    }
    #[cfg(feature = "_char8")]
    impl From<char7> for char8 {
        #[must_use]
        fn from(c: char7) -> char8 {
            c.to_char8()
        }
    }
    #[cfg(feature = "_char16")]
    impl From<char7> for char16 {
        #[must_use]
        fn from(c: char7) -> char16 {
            c.to_char16()
        }
    }
}

/* From char8 */

#[cfg(feature = "_char8")]
mod c8 {
    use super::*;

    impl From<char8> for char {
        #[must_use]
        fn from(c: char8) -> char {
            c.to_char()
        }
    }
    #[cfg(feature = "_char7")]
    impl TryFrom<char8> for char7 {
        type Error = DataOverflow;
        fn try_from(c: char8) -> Result<char7, DataOverflow> {
            c.try_to_char7()
        }
    }
    #[cfg(feature = "_char16")]
    impl From<char8> for char16 {
        #[must_use]
        fn from(c: char8) -> char16 {
            c.to_char16()
        }
    }
}

/* From char16 */

#[cfg(feature = "_char16")]
mod c16 {
    use super::*;

    impl From<char16> for char {
        #[must_use]
        fn from(c: char16) -> char {
            c.to_char()
        }
    }
    #[cfg(feature = "_char7")]
    impl TryFrom<char16> for char7 {
        type Error = DataOverflow;
        fn try_from(c: char16) -> Result<char7, DataOverflow> {
            c.try_to_char7()
        }
    }
    #[cfg(feature = "_char8")]
    impl TryFrom<char16> for char8 {
        type Error = DataOverflow;
        fn try_from(c: char16) -> Result<char8, DataOverflow> {
            c.try_to_char8()
        }
    }
}

/* From char */

#[cfg(feature = "_char7")]
impl TryFrom<char> for char7 {
    type Error = DataOverflow;
    fn try_from(c: char) -> Result<char7, DataOverflow> {
        char7::try_from_char(c)
    }
}
#[cfg(feature = "_char8")]
impl TryFrom<char> for char8 {
    type Error = DataOverflow;
    fn try_from(c: char) -> Result<char8, DataOverflow> {
        char8::try_from_char(c)
    }
}
#[cfg(feature = "_char16")]
impl TryFrom<char> for char16 {
    type Error = DataOverflow;
    fn try_from(c: char) -> Result<char16, DataOverflow> {
        char16::try_from_char(c)
    }
}