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
use core::{
cmp::PartialEq,
fmt::{self, Debug, Display},
};
pub use arr::ArrayStr;
mod arr {
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct ArrayStr<const N: usize> {
arr: [u8; N],
}
impl<const N: usize> ArrayStr<N> {
pub const fn from_utf8(arr: [u8; N]) -> Result<Self, core::str::Utf8Error> {
match core::str::from_utf8(&arr) {
Ok(_) => Ok(Self { arr }),
Err(e) => Err(e),
}
}
pub const fn from_utf8_unwrap(arr: [u8; N]) -> Self {
const_panic::unwrap_ok!(Self::from_utf8(arr))
}
pub const unsafe fn from_utf8_unchecked(arr: [u8; N]) -> Self {
Self { arr }
}
pub const fn as_array(&self) -> &[u8; N] {
&self.arr
}
pub const fn as_slice(&self) -> &[u8] {
&self.arr
}
pub const fn into_array(self) -> [u8; N] {
self.arr
}
}
}
impl<const N: usize> ArrayStr<N> {
pub const fn len(&self) -> usize {
N
}
pub const fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.as_array()) }
}
}
impl<const N: usize> PartialEq<str> for ArrayStr<N> {
fn eq(&self, rhs: &str) -> bool {
self.as_str() == rhs
}
}
impl<const N: usize> PartialEq<&str> for ArrayStr<N> {
fn eq(&self, rhs: &&str) -> bool {
self.as_str() == *rhs
}
}
impl<const N: usize> PartialEq<ArrayStr<N>> for str {
fn eq(&self, rhs: &ArrayStr<N>) -> bool {
self == rhs.as_str()
}
}
impl<const N: usize> PartialEq<ArrayStr<N>> for &str {
fn eq(&self, rhs: &ArrayStr<N>) -> bool {
*self == rhs.as_str()
}
}
impl<const N: usize> Display for ArrayStr<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(self.as_str(), f)
}
}
impl<const N: usize> Debug for ArrayStr<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(self.as_str(), f)
}
}