const_str/__ctfe/
equal.rs1pub struct Equal<T1, T2>(pub T1, pub T2);
2
3impl Equal<&[u8], &[u8]> {
4 pub const fn const_eval(&self) -> bool {
5 crate::bytes::equal(self.0, self.1)
6 }
7}
8
9impl<const L1: usize, const L2: usize> Equal<&[u8; L1], &[u8; L2]> {
10 pub const fn const_eval(&self) -> bool {
11 crate::bytes::equal(self.0, self.1)
12 }
13}
14
15impl Equal<&str, &str> {
16 pub const fn const_eval(&self) -> bool {
17 crate::str::equal(self.0, self.1)
18 }
19}
20
21#[macro_export]
36macro_rules! equal {
37 ($lhs: expr, $rhs: expr) => {
38 $crate::__ctfe::Equal($lhs, $rhs).const_eval()
39 };
40}
41
42#[cfg(test)]
43mod tests {
44 #[test]
45 fn test_equal_str() {
46 const A: &str = "hello";
47 const B: &str = "world";
48 const C: &str = "hello";
49 const D: &str = "";
50 const E: &str = "";
51
52 let eq1 = equal!(A, B);
53 let eq2 = equal!(A, C);
54 let eq3 = equal!(D, E);
55
56 assert!(!eq1);
57 assert!(eq2);
58 assert!(eq3);
59 }
60
61 #[test]
62 fn test_equal_bytes() {
63 const A: &[u8] = b"hello";
64 const B: &[u8] = b"world";
65 const C: &[u8] = b"hello";
66
67 let eq1 = equal!(A, B);
68 let eq2 = equal!(A, C);
69
70 assert!(!eq1);
71 assert!(eq2);
72 }
73
74 #[test]
75 fn test_equal_byte_arrays() {
76 const A: &[u8; 5] = b"hello";
77 const B: &[u8; 5] = b"world";
78 const C: &[u8; 5] = b"hello";
79
80 let eq1 = equal!(A, B);
81 let eq2 = equal!(A, C);
82
83 assert!(!eq1);
84 assert!(eq2);
85 }
86}