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 $(
63 impl Set<u8> for [u8; $n] {
64 fn contains(&self, elem: &u8) -> bool {
65 (self as &[u8]).contains(elem)
66 }
67
68 fn to_str(&self) -> &str {
69 str::from_utf8(self).unwrap_or("<byte array>")
70 }
71 }
72 )+
73 };
74}
75
76impl_set_for_array!(
77 0, 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,
78 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
79 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
80);