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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::translate::FromGlib;
use crate::translate::IntoGlib;
use libc::{c_char, c_uchar};
use std::convert::TryFrom;
use std::num::TryFromIntError;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Char(pub c_char);
impl TryFrom<char> for Char {
type Error = TryFromIntError;
fn try_from(c: char) -> Result<Char, Self::Error> {
Ok(Self(u8::try_from(u32::from(c))? as c_char))
}
}
impl From<Char> for char {
fn from(c: Char) -> char {
c.0 as u8 as char
}
}
impl From<u8> for Char {
fn from(c: u8) -> Char {
Char(c as c_char)
}
}
impl From<Char> for u8 {
fn from(c: Char) -> u8 {
c.0 as u8
}
}
#[doc(hidden)]
impl FromGlib<c_char> for Char {
unsafe fn from_glib(value: c_char) -> Self {
Self(value)
}
}
#[doc(hidden)]
impl IntoGlib for Char {
type GlibType = c_char;
fn into_glib(self) -> c_char {
self.0
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct UChar(pub c_uchar);
impl TryFrom<char> for UChar {
type Error = TryFromIntError;
fn try_from(c: char) -> Result<UChar, Self::Error> {
Ok(Self(u8::try_from(u32::from(c))? as c_uchar))
}
}
impl From<UChar> for char {
fn from(c: UChar) -> char {
c.0 as char
}
}
impl From<u8> for UChar {
fn from(c: u8) -> UChar {
UChar(c as c_uchar)
}
}
impl From<UChar> for u8 {
fn from(c: UChar) -> u8 {
c.0 as u8
}
}
#[doc(hidden)]
impl FromGlib<c_uchar> for UChar {
unsafe fn from_glib(value: c_uchar) -> Self {
Self(value)
}
}
#[doc(hidden)]
impl IntoGlib for UChar {
type GlibType = c_uchar;
fn into_glib(self) -> c_uchar {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::translate::from_glib;
#[test]
fn converts_single_byte_chars() {
assert_eq!(Char::try_from(0 as char), Ok(Char(0_i8)));
assert_eq!(UChar::try_from(0 as char), Ok(UChar(0_u8)));
assert_eq!(UChar::try_from(255 as char), Ok(UChar(255_u8)));
assert_eq!(UChar::try_from('ñ'), Ok(UChar(241_u8)));
}
#[test]
fn refuses_multibyte_chars() {
assert!(Char::try_from('☔').is_err());
assert!(UChar::try_from('☔').is_err());
}
#[test]
fn into_i8() {
assert_eq!(Char::from(b'A').into_glib(), 65_i8);
}
#[test]
fn into_u8() {
assert_eq!(UChar::from(b'A').into_glib(), 65_u8);
}
#[test]
fn into_char() {
assert_eq!(char::from(Char(65_i8)), 'A');
assert_eq!('ñ', UChar(241_u8).into());
}
#[test]
fn convert_from_glib() {
assert_eq!(Char(65_i8), unsafe { from_glib(65_i8) });
assert_eq!(UChar(241_u8), unsafe { from_glib(241_u8) });
}
}