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
use core::fmt::Write;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(transparent)]
pub struct AsciiArray<const N: usize>(pub [u8; N]);
impl<const N: usize> Default for AsciiArray<N> {
#[inline]
#[must_use]
fn default() -> Self {
Self([0_u8; N])
}
}
impl<const N: usize> core::fmt::Debug for AsciiArray<N> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_char('\"')?;
for ch in self.0.iter().copied().map(|u| u as char) {
f.write_char(ch)?;
}
f.write_char('\"')?;
Ok(())
}
}
impl<const N: usize> core::fmt::Display for AsciiArray<N> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for ch in self.0.iter().copied().map(|u| u as char) {
f.write_char(ch)?;
}
Ok(())
}
}
impl<const N: usize> From<[u8; N]> for AsciiArray<N> {
#[inline]
#[must_use]
fn from(array: [u8; N]) -> Self {
Self(array)
}
}
impl<const N: usize> PartialEq<&str> for AsciiArray<N> {
#[inline]
#[must_use]
fn eq(&self, other: &&str) -> bool {
self.0.as_slice() == other.as_bytes()
}
}