empty_collections/
set.rs

1//! Empty sets. Sets of empty sets. Sets of sets of empty sets.
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use std::hash::Hash;
6
7use crate::Empty;
8use std::borrow::Borrow;
9use std::collections::HashSet;
10use std::marker::PhantomData;
11
12/// A set that is guaranteed to be empty.
13///
14/// Naturally, the following operations are impossible with [`ESet`]:
15///
16/// - `insert`
17/// - `remove`
18///
19/// And many others have been elided for being pointless.
20#[cfg_attr(
21    feature = "serde",
22    derive(Serialize, Deserialize),
23    serde(bound(
24        serialize = "T: Eq + Hash + Clone + Serialize",
25        deserialize = "T: Eq + Hash + Deserialize<'de>"
26    )),
27    serde(into = "HashSet<T>", try_from = "HashSet<T>")
28)]
29#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub struct ESet<T> {
31    val: PhantomData<T>,
32}
33
34impl<T> ESet<T> {
35    /// No it doesn't.
36    pub const fn contains<Q>(&self, _: &Q) -> bool
37    where
38        T: Borrow<Q>,
39    {
40        false
41    }
42
43    /// What would a set type be without set operations?
44    pub fn difference(&self, _: &ESet<T>) -> Empty<&T> {
45        Empty::new()
46    }
47
48    /// Elements only present in both sets. Wouldn't it be funny if something
49    /// popped out of this?
50    pub fn intersection(&self, _: &ESet<T>) -> Empty<&T> {
51        Empty::new()
52    }
53
54    /// You never want the book to end, but sometimes it has to.
55    pub fn iter(&self) -> Empty<&T> {
56        Empty::new()
57    }
58
59    /// Yes.
60    pub const fn is_empty(&self) -> bool {
61        true
62    }
63
64    /// It's 0.
65    pub const fn len(&self) -> usize {
66        0
67    }
68
69    /// Think of the possibilities!
70    pub fn new() -> ESet<T> {
71        ESet { val: PhantomData }
72    }
73
74    /// All elements in both sets. _All_ of them.
75    pub fn union(&self, _: &ESet<T>) -> Empty<&T> {
76        Empty::new()
77    }
78}
79
80impl<T> IntoIterator for ESet<T> {
81    type Item = T;
82
83    type IntoIter = Empty<T>;
84
85    fn into_iter(self) -> Self::IntoIter {
86        Empty::new()
87    }
88}
89
90impl<T> From<ESet<T>> for HashSet<T> {
91    fn from(_: ESet<T>) -> Self {
92        HashSet::new()
93    }
94}
95
96impl<T> TryFrom<HashSet<T>> for ESet<T> {
97    type Error = &'static str;
98
99    fn try_from(value: HashSet<T>) -> Result<Self, Self::Error> {
100        if value.is_empty() {
101            Ok(ESet::new())
102        } else {
103            Err("Cannot convert a non-empty HashSet into an empty one")
104        }
105    }
106}
107
108impl<T> std::fmt::Debug for ESet<T> {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        let v: [usize; 0] = [];
111        v.fmt(f)
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn eq() {
121        let a: ESet<usize> = ESet::new();
122        let b = ESet::new();
123        assert_eq!(a, b);
124    }
125
126    #[test]
127    fn ord() {
128        let a: ESet<usize> = ESet::new();
129        let b = ESet::new();
130        assert!(!(a < b));
131        assert!(!(a > b));
132    }
133
134    #[cfg(feature = "serde")]
135    mod serialize {
136        use crate::ESet;
137
138        #[test]
139        fn roundtrip() {
140            let a: ESet<usize> = ESet::new();
141            let j = serde_json::to_string(&a).unwrap();
142            let r = serde_json::from_str(&j).unwrap();
143            assert_eq!(a, r);
144        }
145    }
146}