Skip to main content

devela/sys/mem/view/slice/namespace/
eq.rs

1// devela::sys::mem::view::slice::namespace::eq
2
3use crate::{Slice, is, whilst};
4
5/// Helper for implementing slice operations for primitives.
6macro_rules! impl_prim {
7    () => {
8        impl_prim![
9            u8, u16, u32, u64, u128, usize,
10            i8, i16, i32, i64, i128, isize,
11            f32, f64,
12            bool, char
13        ];
14        #[cfg(nightly_float)]
15        impl_prim![f16, f128];
16    };
17    ($($t:ty),+) => { $( impl_prim![@$t]; )+ };
18    (@$t:ty) => {
19        impl Slice<$t> {
20            /// Checks the equality of two slices of primitives in compile-time.
21            #[must_use]
22            pub const fn eq(a: &[$t], b: &[$t]) -> bool {
23                is! { a.len() != b.len(), return false }
24                whilst! { i in 0..a.len(); {
25                    is! { a[i] != b[i], return false }
26                }}
27                true
28            }
29        }
30        impl Slice<&[$t]> {
31            /// Checks the equality of two slices of slices of primitives in compile-time.
32            #[must_use]
33            pub const fn eq(a: &[&[$t]], b: &[&[$t]]) -> bool {
34                is! { a.len() != b.len(), return false }
35                whilst! { i in 0..a.len(); {
36                    is! { !Slice::<$t>::eq(a[i], b[i]), return false }
37                }}
38                true
39            }
40        }
41    };
42}
43impl_prim!();
44
45/// # Methods for string slices.
46impl Slice<&str> {
47    /// Checks the equality of two string slices in compile-time.
48    #[must_use]
49    pub const fn eq(a: &str, b: &str) -> bool {
50        Slice::<u8>::eq(a.as_bytes(), b.as_bytes())
51    }
52}
53impl Slice<&[&str]> {
54    /// Checks the equality of two slices of string slices in compile-time.
55    #[must_use]
56    pub const fn eq(a: &[&str], b: &[&str]) -> bool {
57        is! { a.len() != b.len(), return false }
58        whilst! { i in 0..a.len(); {
59            is! { !Slice::<&str>::eq(a[i], b[i]), return false }
60        }}
61        true
62    }
63}