1use std::cmp::{PartialEq, PartialOrd};
2use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
3use std::str;
4
5pub trait Set<T> {
7 fn contains(&self, elem: &T) -> bool;
9
10 fn to_str(&self) -> &str {
12 "<set>"
13 }
14}
15
16impl<T: PartialEq> Set<T> for [T] {
17 fn contains(&self, elem: &T) -> bool {
18 (self as &[T]).contains(elem)
19 }
20}
21
22impl Set<char> for str {
23 fn contains(&self, elem: &char) -> bool {
24 (self as &str).contains(*elem)
25 }
26
27 fn to_str(&self) -> &str {
28 self
29 }
30}
31
32impl<T: PartialOrd + Copy> Set<T> for Range<T> {
33 fn contains(&self, elem: &T) -> bool {
34 self.start <= *elem && self.end > *elem
35 }
36}
37
38impl<T: PartialOrd + Copy> Set<T> for RangeFrom<T> {
39 fn contains(&self, elem: &T) -> bool {
40 self.start <= *elem
41 }
42}
43
44impl<T: PartialOrd + Copy> Set<T> for RangeTo<T> {
45 fn contains(&self, elem: &T) -> bool {
46 self.end > *elem
47 }
48}
49
50impl<T> Set<T> for RangeFull {
51 fn contains(&self, _: &T) -> bool {
52 true
53 }
54
55 fn to_str(&self) -> &str {
56 ".."
57 }
58}
59
60macro_rules! impl_set_for_array {
61 ($n:expr) => {
62 impl Set<u8> for [u8; $n] {
63 fn contains(&self, elem: &u8) -> bool {
64 (self as &[u8]).contains(elem)
65 }
66
67 fn to_str(&self) -> &str {
68 str::from_utf8(self).unwrap_or("<byte array>")
69 }
70 }
71 };
72}
73
74impl_set_for_array!(0);
75impl_set_for_array!(1);
76impl_set_for_array!(2);
77impl_set_for_array!(3);
78impl_set_for_array!(4);
79impl_set_for_array!(5);
80impl_set_for_array!(6);
81impl_set_for_array!(7);
82impl_set_for_array!(8);
83impl_set_for_array!(9);
84impl_set_for_array!(10);
85impl_set_for_array!(11);
86impl_set_for_array!(12);
87impl_set_for_array!(13);
88impl_set_for_array!(14);
89impl_set_for_array!(15);
90impl_set_for_array!(16);
91impl_set_for_array!(17);
92impl_set_for_array!(18);
93impl_set_for_array!(19);
94impl_set_for_array!(20);
95impl_set_for_array!(21);
96impl_set_for_array!(22);
97impl_set_for_array!(23);
98impl_set_for_array!(24);
99impl_set_for_array!(25);
100impl_set_for_array!(26);
101impl_set_for_array!(27);
102impl_set_for_array!(28);
103impl_set_for_array!(29);
104impl_set_for_array!(30);
105impl_set_for_array!(31);
106impl_set_for_array!(32);