use alloc::collections::BTreeSet;
pub trait MatchPattern<T: Copy + Eq + Ord + Sized>: Copy + Sized {
fn is_match(self, thing: T) -> bool;
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for T {
#[inline]
fn is_match(self, thing: T) -> bool { self == thing }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for &[T] {
#[inline]
fn is_match(self, thing: T) -> bool { self.contains(&thing) }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for &BTreeSet<T> {
#[inline]
fn is_match(self, thing: T) -> bool { self.contains(&thing) }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for [T; 1] {
#[inline]
fn is_match(self, thing: T) -> bool { self[0] == thing }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for &[T; 1] {
#[inline]
fn is_match(self, thing: T) -> bool { self[0] == thing }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for [T; 2] {
#[inline]
fn is_match(self, thing: T) -> bool { self[0] == thing || self[1] == thing }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for &[T; 2] {
#[inline]
fn is_match(self, thing: T) -> bool { self[0] == thing || self[1] == thing }
}
impl<F: Fn(u8) -> bool + Copy> MatchPattern<u8> for F {
#[inline]
fn is_match(self, thing: u8) -> bool { self(thing) }
}
impl<F: Fn(char) -> bool + Copy> MatchPattern<char> for F {
#[inline]
fn is_match(self, thing: char) -> bool { self(thing) }
}
macro_rules! arr {
($($size:literal),+ $(,)?) => ($(
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for [T; $size] {
#[inline]
fn is_match(self, thing: T) -> bool { self.contains(&thing) }
}
impl<T: Copy + Eq + Ord + Sized> MatchPattern<T> for &[T; $size] {
#[inline]
fn is_match(self, thing: T) -> bool { self.contains(&thing) }
}
)+);
}
arr!(
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,
);
#[cfg(test)]
mod test {
use super::*;
const fn strip_b(b: u8) -> bool { b == b'b' }
#[test]
fn t_patterns() {
assert!(b'b'.is_match(b'b'));
assert!(! b'b'.is_match(b'.'));
let arr: [u8; 1] = [b'b'];
assert!(arr.is_match(b'b'));
assert!(! arr.is_match(b'a'));
let arr: [u8; 2] = [b'b', b'.'];
assert!(arr.is_match(b'b'));
assert!(arr.is_match(b'.'));
assert!(! arr.is_match(b'a'));
let arr: [u8; 3] = [b'b', b'.', b'!'];
assert!(arr.is_match(b'b'));
assert!(arr.is_match(b'.'));
assert!(arr.is_match(b'!'));
assert!(! arr.is_match(b'a'));
assert!(arr.as_slice().is_match(b'b'));
assert!(arr.as_slice().is_match(b'.'));
assert!(arr.as_slice().is_match(b'!'));
assert!(! arr.as_slice().is_match(b'a'));
let set = BTreeSet::from(arr);
assert!(set.is_match(b'b'));
assert!(set.is_match(b'.'));
assert!(set.is_match(b'!'));
assert!(! set.is_match(b'a'));
assert!(strip_b.is_match(b'b'));
assert!(! strip_b.is_match(b'B'));
let foo = |b: u8| -> bool { b == b'b' };
assert!(foo.is_match(b'b'));
assert!(! foo.is_match(b'X'));
}
}