singletonset/
lib.rs

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#![doc = include_str!("../README.md")]

use std::{
    any::{Any, TypeId},
    collections::{HashMap, TryReserveError},
};

/// A hash map that uses the value's type as its key.
///
/// This data structure can be used to create a locally-scoped Singleton out
/// of any data type it holds. It ensures there is only one instance of any
/// type, similar to a Singleton, without requiring a global scope.
#[derive(Debug, Default)]
pub struct SingletonSet {
    data: HashMap<TypeId, Box<dyn Any>>,
}

impl SingletonSet {
    /// Creates an empty `SingletonSet`.
    ///
    /// The set is initially created with a capacity of 0, so it will not
    /// allocate until an element is inserted. This behavior is inherited
    /// from the internal `HashMap` that is used to stored the elements.
    ///
    /// # Example
    ///
    /// ```
    /// use singletonset::SingletonSet;
    /// let mut set = SingletonSet::new();
    /// ```
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        SingletonSet {
            data: HashMap::new(),
        }
    }

    /// Creates an empty `SingletonSet` with at least the specified capacity.
    ///
    /// The set will be able to hold at least `capacity` elements without
    /// reallocating. The hash map that stores the elements internally does
    /// not provide any guarantee that more space won't be allocated.
    ///
    /// # Example
    ///
    /// ```
    /// use singletonset::SingletonSet;
    /// let mut set: SingletonSet = SingletonSet::with_capacity(10);
    /// ```
    #[inline]
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        SingletonSet {
            data: HashMap::with_capacity(capacity),
        }
    }

    /// Returns the number of elements the set can hold without reallocating.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }

    /// Returns the number of elements the set currently holds.
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns true if the set contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Clears the set, removing all values.
    #[inline]
    pub fn clear(&mut self) {
        self.data.clear()
    }

    /// Reserves capacity for at least `additional` more values.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional)
    }

    /// Tries to reserve capacity for at least `additional` more values.
    #[inline]
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.data.try_reserve(additional)
    }

    /// Shrinks the capacity of the set as much as possible.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit()
    }

    /// Shrinks the capacity of the set as much as possible, but not less than
    /// `min_capacity`.
    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.data.shrink_to(min_capacity)
    }

    /// Returns an immutable reference to the value of the specified type.
    ///
    /// This method does not insert an element into the set, so it can be
    /// used with types that do not implement [`Default`] and does not need
    /// the `SimpletonSet` to be mutable.
    ///
    /// # Safety
    ///
    /// This method panics if there is no existing value for the given type.
    /// If this is not acceptable, use [`SingletonSet::try_get()`],
    /// [`SingletonSet::get_mut()`], or one of the `get_or` methods.
    pub fn get<T>(&self) -> &T
    where
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data
            .get(&type_id)
            .and_then(|boxed| boxed.downcast_ref::<T>())
            .expect("try_get() should be used if the slot might be empty")
    }

    /// Returns an immutable reference to the value of the specified type,
    /// if it exists.
    ///
    /// This method does not insert an element into the set, so it can be
    /// used with types that do not implement [`Default`] and does not need
    /// the `SimpletonSet` to be mutable.
    pub fn try_get<T>(&self) -> Option<&T>
    where
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data
            .get(&type_id)
            .and_then(|boxed| boxed.downcast_ref::<T>())
    }

    /// Returns a mutable reference to the value of the specified type.
    ///
    /// This method inserts an element into the set if the type is not
    /// already represented, so the type must implement [`Default`].
    pub fn get_mut<T>(&mut self) -> &mut T
    where
        T: Any + Default,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data.entry(type_id).or_insert(Box::new(T::default()));
        self.data
            .get_mut(&type_id)
            .and_then(|boxed| boxed.downcast_mut::<T>())
            // Safety: The key exists and the type must be correct
            .unwrap()
    }

    /// Returns a mutable reference to the value of the specified type,
    /// if it exists.
    ///
    /// This method does not insert an element into the set, so it can be
    /// used with types that do not implement [`Default`].
    pub fn try_get_mut<T>(&mut self) -> Option<&mut T>
    where
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data
            .get_mut(&type_id)
            .and_then(|boxed| boxed.downcast_mut::<T>())
    }

    /// Returns an immutable reference to the value of the specified type,
    /// inserting the provided value if the type isn't already in the set.
    ///
    /// If the type is already represented in the set, the provided value
    /// is ignored.
    pub fn get_or_insert<T>(&mut self, value: T) -> &T
    where
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data.entry(type_id).or_insert(Box::new(value));
        self.data
            .get(&type_id)
            .and_then(|boxed| boxed.downcast_ref::<T>())
            // Safety: The key exists and the type must be correct
            .unwrap()
    }

    /// Returns a mutable reference to the value of the specified type,
    /// inserting the provided value if the type isn't already in the set.
    ///
    /// If the type is already represented in the set, the provided value
    /// is ignored. To avoid allocating memory for a default value that is
    /// discarded, use [`SingletonSet::get_or_insert_with_mut()`].
    pub fn get_or_insert_mut<T>(&mut self, value: T) -> &mut T
    where
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data.entry(type_id).or_insert(Box::new(value));
        self.data
            .get_mut(&type_id)
            .and_then(|boxed| boxed.downcast_mut::<T>())
            // Safety: The key exists and the type must be correct
            .unwrap()
    }

    /// Returns an immutable reference to the value of the specified type,
    /// inserting the return value of the provided method if the type isn't
    /// already in the set.
    pub fn get_or_insert_with<F, T>(&mut self, default: F) -> &T
    where
        F: FnOnce() -> T,
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data
            .entry(type_id)
            .or_insert_with(|| Box::new(default()));
        self.data
            .get(&type_id)
            .and_then(|boxed| boxed.downcast_ref::<T>())
            // Safety: The key exists and the type must be correct
            .unwrap()
    }

    /// Returns a mutable reference to the value of the specified type,
    /// inserting the return value of the provided method if the type isn't
    /// already in the set.
    pub fn get_or_insert_with_mut<F, T>(&mut self, default: F) -> &mut T
    where
        F: FnOnce() -> T,
        T: Any,
    {
        let type_id = std::any::TypeId::of::<T>();
        self.data
            .entry(type_id)
            .or_insert_with(|| Box::new(default()));
        self.data
            .get_mut(&type_id)
            .and_then(|boxed| boxed.downcast_mut::<T>())
            // Safety: The key exists and the type must be correct
            .unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_singletonset_retains_last_element_of_type() {
        let mut set = SingletonSet::new();

        *set.get_mut() = 1u8;
        *set.get_mut() = 2u8;
        *set.get_mut() = 3u8;
        *set.get_mut() = 1u16;
        *set.get_mut() = 2u16;
        *set.get_mut() = 3u32;
        *set.get_mut() = 2u32;
        *set.get_mut() = "foo";
        *set.get_mut() = "bar";
        *set.get_mut() = "baz".to_string();

        assert_eq!(set.len(), 5);
        assert_ne!(set.get::<u8>(), &1u8);
        assert_ne!(set.get::<u8>(), &1u8);
        assert_eq!(set.get::<u8>(), &3u8);
        assert_ne!(set.get::<u16>(), &1u16);
        assert_eq!(set.get::<u16>(), &2u16);
        assert_ne!(set.get::<u32>(), &3u32);
        assert_eq!(set.get::<u32>(), &2u32);
        assert_ne!(set.get::<&str>(), &"foo");
        assert_eq!(set.get::<&str>(), &"bar");
        assert_eq!(set.get::<String>(), &"baz".to_string());
    }

    #[test]
    fn test_singletonset_mutations() {
        let mut set = SingletonSet::new();

        *set.get_mut() = "foo".to_string();
        (*set.get_mut::<String>()).push_str("bar");

        *set.get_mut::<u8>() += 2;
        *set.get_mut::<u8>() *= 2;

        // The "Hello, World!" string should be gone, replaced by the longer
        // one, which can be retrieved by accessing the `&str` element.
        assert_ne!(set.get::<String>(), &"foo".to_string());
        assert_eq!(set.get::<String>(), &"foobar".to_string());
        assert_ne!(set.get::<u8>(), &2);
        assert_eq!(set.get::<u8>(), &4);
    }

    #[test]
    fn singletonset_works_without_default() {
        let mut set = SingletonSet::new();

        #[derive(Debug, PartialEq)]
        struct Foo(&'static str);

        assert_eq!(set.try_get::<Foo>(), None);

        set.get_or_insert(Foo("bar"));

        assert_eq!(set.try_get::<Foo>(), Some(&Foo("bar")));
    }

    #[test]
    fn singletonset_works_with_custom_defaults() {
        let mut set = SingletonSet::new();

        #[derive(Debug, PartialEq)]
        struct Foo(&'static str);

        impl Default for Foo {
            fn default() -> Self {
                Self("foo")
            }
        }

        set.get_mut::<Foo>();

        assert_eq!(set.try_get::<Foo>(), Some(&Foo("foo")));

        *set.get_mut() = Foo("bar");

        assert_eq!(set.try_get::<Foo>(), Some(&Foo("bar")));
    }
}