fixed_map/set/storage/
option.rs

1use core::iter;
2use core::mem;
3use core::option;
4
5use crate::set::SetStorage;
6use crate::Key;
7
8type Iter<'a, T> = iter::Chain<
9    iter::Map<<<T as Key>::SetStorage as SetStorage<T>>::Iter<'a>, fn(T) -> Option<T>>,
10    option::IntoIter<Option<T>>,
11>;
12type IntoIter<T> = iter::Chain<
13    iter::Map<<<T as Key>::SetStorage as SetStorage<T>>::IntoIter, fn(T) -> Option<T>>,
14    option::IntoIter<Option<T>>,
15>;
16
17/// [`SetStorage`] for [`Option`] types.
18///
19/// # Examples
20///
21/// ```
22/// use fixed_map::{Key, Map};
23///
24/// #[derive(Debug, Clone, Copy, PartialEq, Key)]
25/// enum Part {
26///     A,
27///     B,
28/// }
29///
30/// #[derive(Debug, Clone, Copy, PartialEq, Key)]
31/// enum MyKey {
32///     First(Option<Part>),
33///     Second,
34/// }
35///
36/// let mut a = Map::new();
37/// a.insert(MyKey::First(None), 1);
38/// a.insert(MyKey::First(Some(Part::A)), 2);
39///
40/// assert_eq!(a.get(MyKey::First(Some(Part::A))), Some(&2));
41/// assert_eq!(a.get(MyKey::First(Some(Part::B))), None);
42/// assert_eq!(a.get(MyKey::First(None)), Some(&1));
43/// assert_eq!(a.get(MyKey::Second), None);
44///
45/// assert!(a.iter().eq([(MyKey::First(Some(Part::A)), &2), (MyKey::First(None), &1)]));
46/// assert!(a.values().copied().eq([2, 1]));
47/// assert!(a.keys().eq([MyKey::First(Some(Part::A)), MyKey::First(None)]));
48/// ```
49pub struct OptionSetStorage<T>
50where
51    T: Key,
52{
53    some: T::SetStorage,
54    none: bool,
55}
56
57impl<T> Clone for OptionSetStorage<T>
58where
59    T: Key,
60    T::SetStorage: Clone,
61{
62    #[inline]
63    fn clone(&self) -> Self {
64        Self {
65            some: self.some.clone(),
66            none: self.none,
67        }
68    }
69}
70
71impl<T> Copy for OptionSetStorage<T>
72where
73    T: Key,
74    T::SetStorage: Copy,
75{
76}
77
78impl<T> PartialEq for OptionSetStorage<T>
79where
80    T: Key,
81    T::SetStorage: PartialEq,
82{
83    #[inline]
84    fn eq(&self, other: &Self) -> bool {
85        self.none == other.none && self.some == other.some
86    }
87}
88
89impl<T> Eq for OptionSetStorage<T>
90where
91    T: Key,
92    T::SetStorage: Eq,
93{
94}
95
96impl<T> SetStorage<Option<T>> for OptionSetStorage<T>
97where
98    T: Key,
99{
100    type Iter<'this> = Iter<'this, T> where T: 'this;
101    type IntoIter = IntoIter<T>;
102
103    #[inline]
104    fn empty() -> Self {
105        Self {
106            some: T::SetStorage::empty(),
107            none: false,
108        }
109    }
110
111    #[inline]
112    fn len(&self) -> usize {
113        self.some.len() + usize::from(self.none)
114    }
115
116    #[inline]
117    fn is_empty(&self) -> bool {
118        self.some.is_empty() && self.none
119    }
120
121    #[inline]
122    fn insert(&mut self, value: Option<T>) -> bool {
123        match value {
124            Some(value) => self.some.insert(value),
125            None => mem::replace(&mut self.none, true),
126        }
127    }
128
129    #[inline]
130    fn contains(&self, value: Option<T>) -> bool {
131        match value {
132            Some(key) => self.some.contains(key),
133            None => self.none,
134        }
135    }
136
137    #[inline]
138    fn remove(&mut self, key: Option<T>) -> bool {
139        match key {
140            Some(key) => self.some.remove(key),
141            None => mem::replace(&mut self.none, false),
142        }
143    }
144
145    #[inline]
146    fn retain<F>(&mut self, mut func: F)
147    where
148        F: FnMut(Option<T>) -> bool,
149    {
150        self.some.retain(|value| func(Some(value)));
151
152        if self.none {
153            self.none = func(None);
154        }
155    }
156
157    #[inline]
158    fn clear(&mut self) {
159        self.some.clear();
160        self.none = false;
161    }
162
163    #[inline]
164    fn iter(&self) -> Self::Iter<'_> {
165        let map: fn(_) -> _ = Some;
166        self.some
167            .iter()
168            .map(map)
169            .chain(self.none.then_some(None::<T>))
170    }
171
172    #[inline]
173    fn into_iter(self) -> Self::IntoIter {
174        let map: fn(_) -> _ = Some;
175        self.some
176            .into_iter()
177            .map(map)
178            .chain(self.none.then_some(None::<T>))
179    }
180}