fixed_map/set/storage/
singleton.rs

1use core::mem;
2
3use crate::set::SetStorage;
4
5/// [`SetStorage`]  types that can only inhabit a single value (like `()`).
6#[repr(transparent)]
7#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct SingletonSetStorage {
9    is_set: bool,
10}
11
12impl<T> SetStorage<T> for SingletonSetStorage
13where
14    T: Default,
15{
16    type Iter<'this> = ::core::option::IntoIter<T>;
17    type IntoIter = ::core::option::IntoIter<T>;
18
19    #[inline]
20    fn empty() -> Self {
21        Self { is_set: false }
22    }
23
24    #[inline]
25    fn len(&self) -> usize {
26        usize::from(self.is_set)
27    }
28
29    #[inline]
30    fn is_empty(&self) -> bool {
31        !self.is_set
32    }
33
34    #[inline]
35    fn insert(&mut self, _: T) -> bool {
36        !mem::replace(&mut self.is_set, true)
37    }
38
39    #[inline]
40    fn contains(&self, _: T) -> bool {
41        self.is_set
42    }
43
44    #[inline]
45    fn remove(&mut self, _: T) -> bool {
46        mem::replace(&mut self.is_set, false)
47    }
48
49    #[inline]
50    fn retain<F>(&mut self, mut func: F)
51    where
52        F: FnMut(T) -> bool,
53    {
54        self.is_set = func(T::default());
55    }
56
57    #[inline]
58    fn clear(&mut self) {
59        self.is_set = false;
60    }
61
62    #[inline]
63    fn iter(&self) -> Self::Iter<'_> {
64        self.is_set.then_some(T::default()).into_iter()
65    }
66
67    #[inline]
68    fn into_iter(self) -> Self::IntoIter {
69        self.is_set.then_some(T::default()).into_iter()
70    }
71}