1use super::{
6 nice_arr,
7 nice_str,
8 nice_uint,
9 NiceChar,
10};
11use std::num::NonZeroU8;
12
13
14
15#[repr(u8)]
16#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
17enum NiceU8Idx {
19 From00 = 0_u8, From01 = 1_u8, From02 = 2_u8, }
23
24#[cfg_attr(
25 not(test),
26 expect(dead_code, reason = "For consistency with bigger index types.")
27)]
28impl NiceU8Idx {
29 const DIGITS: [Self; 3] = [
31 Self::From02, Self::From01, Self::From00,
32 ];
33
34 const LAST: Self = Self::From02;
36
37 const LEN: usize = 3;
39}
40
41
42
43nice_uint! {
44 NiceU8, NiceU8Idx,
45 u8, NonZeroU8,
46 [ 0 0 0 ],
47 [ 2 5 5 ]
48}
49
50impl From<u8> for NiceU8 {
51 #[inline]
52 fn from(src: u8) -> Self {
53 let (data, from) = data_from(src);
54 Self { data, from }
55 }
56}
57
58impl NiceU8 {
59 #[must_use]
60 #[inline]
61 pub const fn as_bytes2(&self) -> &[u8] {
74 NiceChar::as_bytes(
75 if matches!(self.from, NiceU8Idx::From00) { self.data.as_slice() }
76 else {
77 let (_, rest) = self.data.split_at(1);
78 rest
79 }
80 )
81 }
82
83 #[must_use]
84 #[inline]
85 pub const fn as_bytes3(&self) -> &[u8] {
98 NiceChar::as_bytes(self.data.as_slice())
99 }
100
101 #[must_use]
102 #[inline]
103 pub const fn as_str2(&self) -> &str {
116 NiceChar::as_str(
117 if matches!(self.from, NiceU8Idx::From00) { self.data.as_slice() }
118 else {
119 let (_, rest) = self.data.split_at(1);
120 rest
121 }
122 )
123 }
124
125 #[must_use]
126 #[inline]
127 pub const fn as_str3(&self) -> &str {
140 NiceChar::as_str(self.data.as_slice())
141 }
142}
143
144impl NiceU8 {
145 pub const fn replace(&mut self, src: u8) {
161 (self.data, self.from) = data_from(src);
162 }
163}
164
165
166
167#[inline]
168const fn data_from(num: u8) -> ([NiceChar; 3], NiceU8Idx) {
173 if 99 < num {
175 let a = if 199 < num { NiceChar::Digit2 } else { NiceChar::Digit1 };
176 let b = NiceChar::from_digit_u8(num / 10);
177 let c = NiceChar::from_digit_u8(num);
178 ([a, b, c], NiceU8Idx::From00)
179 }
180 else if 9 < num {
182 let b = NiceChar::from_digit_u8(num / 10);
183 let c = NiceChar::from_digit_u8(num);
184 ([NiceChar::Digit0, b, c], NiceU8Idx::From01)
185 }
186 else {
188 (
189 [NiceChar::Digit0, NiceChar::Digit0, NiceChar::from_digit_u8(num)],
190 NiceU8Idx::From02
191 )
192 }
193}
194
195
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[test]
202 fn t_nice() {
203 assert_eq!(NiceU8::default(), NiceU8::from(0));
205 assert_eq!(NiceU8::MIN, NiceU8::from(0));
206 assert_eq!(NiceU8::MAX, NiceU8::from(u8::MAX));
207
208 let mut last = NiceU8::MAX;
209 for i in u8::MIN..=u8::MAX {
210 let istr = i.to_string();
211 let nice = NiceU8::from(i);
212
213 assert_eq!(istr, nice.as_str());
214 assert_eq!(istr.as_bytes(), nice.as_bytes());
215 assert_eq!(istr.len(), nice.len());
216
217 assert_ne!(nice, last);
219
220 last.replace(i);
222 assert_eq!(nice, last);
223 }
224 }
225
226 super::super::nice_test!(NiceU8Idx);
227}