variant-set 0.1.0

A set-like data structure for enum variants, allowing you to store at most one value for each variant of an enum.
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#![warn(clippy::all, clippy::pedantic)]
use std::{
    collections::HashMap,
    hash::{BuildHasherDefault, Hash},
};

use nohash_hasher::NoHashHasher;
pub use variant_set_derive::VariantEnum;

/// A trait that must be implemented by enums that are used with `VariantSet`.
///
/// This trait provides a way to get the variant of an enum, which is another enum that represents the variants of the original enum,
/// but without the data.
pub trait VariantEnum {
    /// The enum that represents the variants of the original enum, but without the data.
    type Variant: Copy + Eq + Hash;

    /// For a given value of the enum, returns the variant of the enum.
    fn variant(&self) -> Self::Variant;
}

/// A set of values that are variants of an enum. The set can contain at most one value for each variant.
/// Functionally equivalent to a `HashSet<T>`, but the enum variants can contain complex data.
///
/// The enum must implement the `VariantEnum` trait, you will generally want to derive it using the `VariantEnum` derive macro.
///
/// # Examples
/// ```
/// use variant_set::{VariantSet, VariantEnum};
///
/// #[derive(VariantEnum, Debug, PartialEq)]
/// enum MyEnum {
///     Variant1(String),
///     Variant2(u32),
///     Variant3(bool),
/// }
///
/// let mut set = VariantSet::new();
///
/// set.set(MyEnum::Variant1("Hello".to_string()));
/// set.set(MyEnum::Variant2(42));
/// set.set(MyEnum::Variant3(true));
///
/// assert!(set.contains(MyEnumVariant::Variant1));
/// assert!(set.contains(MyEnumVariant::Variant2));
/// assert!(set.contains(MyEnumVariant::Variant3));
///
/// assert_eq!(set.get(MyEnumVariant::Variant1), Some(&MyEnum::Variant1("Hello".to_string())));
/// assert_eq!(set.get(MyEnumVariant::Variant2), Some(&MyEnum::Variant2(42)));
/// assert_eq!(set.get(MyEnumVariant::Variant3), Some(&MyEnum::Variant3(true)));
/// ```
///
/// # Performance
///
/// The `VariantSet` is backed by a `HashMap` and provides constant time insertion, removal, and lookup.
///
pub struct VariantSet<T>
where
    T: VariantEnum,
{
    data: HashMap<T::Variant, T, BuildHasherDefault<NoHashHasher<usize>>>,
}

impl<T> VariantSet<T>
where
    T: VariantEnum,
{
    /// Creates a new `VariantSet` with a default capacity and hasher.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let set: VariantSet<MyEnum> = VariantSet::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            data: HashMap::with_hasher(BuildHasherDefault::default()),
        }
    }

    /// Creates a new `VariantSet` with a specified capacity.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let set: VariantSet<MyEnum> = VariantSet::with_capacity(10);
    /// assert!(set.capacity() >= 10);
    /// ```
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: HashMap::with_capacity_and_hasher(capacity, BuildHasherDefault::default()),
        }
    }

    /// Returns the number of elements this set can hold without reallocating.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let set: VariantSet<MyEnum> = VariantSet::with_capacity(10);
    /// assert!(set.capacity() >= 10);
    /// ```
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }

    /// Clears the set, removing all values.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.clear();
    /// assert!(set.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.data.clear();
    }

    /// Adds a value to the set.
    ///
    /// Returns whether the value was newly inserted. That is:
    ///
    /// * If the set did not previously contain this value, `true` is returned.
    /// * If the set already contained this value, `false` is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
    ///
    /// Note that if the set already contains a value, but the contained value is not equal to the value passed as argument, the value passed as argument is dropped and `false` is returned.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// assert!(set.insert(MyEnum::Variant1("Hello".to_string())));
    /// assert!(!set.insert(MyEnum::Variant1("World".to_string())));
    /// ```
    pub fn insert(&mut self, value: T) -> bool {
        if let std::collections::hash_map::Entry::Vacant(entry) = self.data.entry(value.variant()) {
            entry.insert(value);
            true
        } else {
            false
        }
    }

    /// Sets a value in the set. If a previous value existed, it is returned.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, PartialEq, Debug)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// let previous = set.set(MyEnum::Variant1("Hello".to_string()));
    /// assert_eq!(previous, None);
    ///
    /// let previous = set.set(MyEnum::Variant1("World".to_string()));
    /// assert_eq!(previous, Some(MyEnum::Variant1("Hello".to_string())));
    /// ```
    pub fn set(&mut self, value: T) -> Option<T> {
        self.data.insert(value.variant(), value)
    }

    /// Returns `true` if the set contains a value.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// assert!(set.contains(MyEnumVariant::Variant1));
    /// ```
    pub fn contains(&self, value: T::Variant) -> bool {
        self.data.contains_key(&value)
    }

    /// Returns `true` if the set contains a value that is equal to the given value.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// assert!(set.contains_exact(&MyEnum::Variant1("Hello".to_string())));
    /// assert!(!set.contains_exact(&MyEnum::Variant1("World".to_string())));
    /// ```
    pub fn contains_exact(&self, value: &T) -> bool
    where
        T: PartialEq,
    {
        matches!(self.data.get(&value.variant()), Some(v) if v == value)
    }

    /// Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    /// let values: Vec<_> = set.drain().collect();
    ///
    /// assert_eq!(values.len(), 2);
    /// assert!(values.contains(&MyEnum::Variant1("Hello".to_string())));
    /// assert!(values.contains(&MyEnum::Variant2(42)));
    /// ```
    pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
        self.data.drain().map(|(_, value)| value)
    }

    /// Returns a reference to the value in the set, if any, that is equal to the given value.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// let value = set.get(MyEnumVariant::Variant1);
    /// assert_eq!(value, Some(&MyEnum::Variant1("Hello".to_string())));
    /// ```
    pub fn get(&self, value: T::Variant) -> Option<&T> {
        self.data.get(&value)
    }

    /// Inserts the given `value` into the set if it is not present, then returns a reference to the value in the set.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// let value = set.get_or_insert(MyEnum::Variant1("Hello".to_string()));
    /// assert_eq!(value, &MyEnum::Variant1("Hello".to_string()));
    /// ```
    pub fn get_or_insert(&mut self, default: T) -> &T {
        self.data.entry(default.variant()).or_insert(default)
    }

    /// Returns `true` if the set contains no elements.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let set: VariantSet<MyEnum> = VariantSet::new();
    /// assert!(set.is_empty());
    /// ```
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// An iterator visiting all elements in arbitrary order. The iterator element type is `&'a T`.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    ///
    /// for value in set.iter() {
    ///    println!("{:?}", value);
    /// }
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.data.values()
    }

    /// Returns the number of elements in the set.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    ///
    /// assert_eq!(set.len(), 2);
    /// ```
    #[must_use]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Removes a variant from the set. Returns the value if it existed.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///    Variant1(String),
    ///   Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// let value = set.remove(MyEnumVariant::Variant1);
    /// assert_eq!(value, Some(MyEnum::Variant1("Hello".to_string())));
    /// ```
    pub fn remove(&mut self, value: T::Variant) -> Option<T> {
        self.data.remove(&value)
    }

    /// Removes a variant from the set if it is equal to the given value. Returns the value if it existed.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    ///
    /// let not_matching = set.remove_exact(&MyEnum::Variant1("World".to_string()));
    /// assert_eq!(not_matching, None);
    ///
    /// let matching = set.remove_exact(&MyEnum::Variant1("Hello".to_string()));
    /// assert_eq!(matching, Some(MyEnum::Variant1("Hello".to_string())));
    /// ```
    pub fn remove_exact(&mut self, value: &T) -> Option<T>
    where
        T: PartialEq,
    {
        match self.data.get(&value.variant()) {
            Some(v) if v == value => self.data.remove(&value.variant()),
            _ => None,
        }
    }

    /// Reserves capacity for at least `additional` more elements to be inserted in the set.
    /// The collection may reserve more space to avoid frequent reallocations.
    /// After calling `reserve`, capacity will be greater than or equal to `self.len() + additional`.
    /// Does nothing if the capacity is already sufficient.
    ///
    /// Note that you can reserve more capacity than there are variants in the enum.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set: VariantSet<MyEnum> = VariantSet::new();
    /// set.reserve(10);
    /// assert!(set.capacity() >= 10);
    /// ```
    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional);
    }

    /// Tries to reserve capacity for at least `additional` more elements to be inserted in the set.
    /// The collection may reserve more space to avoid frequent reallocations.
    /// After calling `try_reserve`, capacity will be greater than or equal to `self.len() + additional` if it returns Ok(())
    /// Does nothing if the capacity is already sufficient.
    ///
    /// Note that you can reserve more capacity than there are variants in the enum.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set: VariantSet<MyEnum> = VariantSet::new();
    /// set.try_reserve(10).unwrap();
    /// assert!(set.capacity() >= 10);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a `std::collections::TryReserveError` if the new capacity would overflow usize.
    pub fn try_reserve(
        &mut self,
        additional: usize,
    ) -> Result<(), std::collections::TryReserveError> {
        self.data.try_reserve(additional)
    }

    /// Shrinks the capacity of the set with a lower limit. It will drop down to no lower than the supplied limit while maintaining the internal
    /// rules and possibly leaving some space in accordance with the resize policy.
    ///
    /// If the current capacity is less than the lower limit, this is a no-op.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set: VariantSet<MyEnum> = VariantSet::new();
    /// set.reserve(10);
    /// set.shrink_to(5);
    /// assert!(set.capacity() >= 5);
    /// ```
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.data.shrink_to(min_capacity);
    }

    /// Shrinks the capacity of the set as much as possible.
    /// It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///    Variant1(String),
    ///    Variant2(u32),
    /// }
    ///
    /// let mut set: VariantSet<MyEnum> = VariantSet::new();
    /// set.reserve(10);
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.shrink_to_fit();
    /// assert!(set.capacity() >= 1);
    /// ```
    pub fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit();
    }

    /// Removes and returns the value in the set, if any, that is equal to the given value.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// let value = set.take(MyEnumVariant::Variant1);
    /// assert_eq!(value, Some(MyEnum::Variant1("Hello".to_string())));
    /// ```
    pub fn take(&mut self, value: T::Variant) -> Option<T> {
        self.data.remove(&value)
    }
}

impl<T> Default for VariantSet<T>
where
    T: VariantEnum,
{
    /// Creates a new `VariantSet` with a default capacity.
    /// The default capacity is the capacity of a newly created `HashMap`.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let set: VariantSet<MyEnum> = Default::default();
    /// ```
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Clone for VariantSet<T>
where
    T: VariantEnum + Clone,
{
    /// Clones the set. The values are cloned using their `Clone` implementation.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, Clone, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    ///
    /// let cloned = set.clone();
    /// assert_eq!(set, cloned);
    /// ```
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
        }
    }
}

impl<T> std::fmt::Debug for VariantSet<T>
where
    T: VariantEnum + std::fmt::Debug,
    T::Variant: std::fmt::Debug,
{
    /// Formats the set as a map of variants to values.
    /// The values are formatted using their `Debug` implementation.
    /// The variants are formatted using their `Debug` implementation.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    ///
    /// println!("{:?}", set);
    /// ```
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map().finish()
    }
}

impl<T> PartialEq for VariantSet<T>
where
    T: VariantEnum + PartialEq,
{
    /// Compares two sets for equality.
    /// Two sets are equal if they contain the same variants, regardless of the order.
    /// The values of the variants must be equal for the sets to be equal.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set1 = VariantSet::new();
    /// set1.set(MyEnum::Variant1("Hello".to_string()));
    /// set1.set(MyEnum::Variant2(42));
    ///
    /// let mut set2 = VariantSet::new();
    /// set2.set(MyEnum::Variant2(42));
    /// set2.set(MyEnum::Variant1("Hello".to_string()));
    ///
    /// assert_eq!(set1, set2);
    /// ```
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl<T> Eq for VariantSet<T> where T: VariantEnum + Eq {}

impl<T> Extend<T> for VariantSet<T>
where
    T: VariantEnum,
{
    /// Extends the set with the contents of an iterator.
    /// If the set already contains a value that maps to the same variant, the value will be replaced.
    /// If the iterator yields multiple values that map to the same variant, the last value will be used.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant2(10));
    /// set.extend(vec![MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42), MyEnum::Variant1("World".to_string())]);
    ///
    /// assert_eq!(set.len(), 2);
    /// assert!(set.contains_exact(&MyEnum::Variant1("World".to_string())));
    /// assert!(set.contains_exact(&MyEnum::Variant2(42)));
    /// ```
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for value in iter {
            self.set(value);
        }
    }
}

impl<T> IntoIterator for VariantSet<T>
where
    T: VariantEnum,
{
    type Item = T;
    type IntoIter = std::collections::hash_map::IntoValues<T::Variant, T>;

    /// Consumes the set and returns an iterator over the values.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let mut set = VariantSet::new();
    /// set.set(MyEnum::Variant1("Hello".to_string()));
    /// set.set(MyEnum::Variant2(42));
    ///
    /// let values: Vec<_> = set.into_iter().collect();
    ///
    /// assert_eq!(values.len(), 2);
    /// assert!(values.contains(&MyEnum::Variant1("Hello".to_string())));
    /// assert!(values.contains(&MyEnum::Variant2(42)));
    /// ```
    fn into_iter(self) -> Self::IntoIter {
        self.data.into_values()
    }
}

impl<T> FromIterator<T> for VariantSet<T>
where
    T: VariantEnum,
{
    /// Creates a new `VariantSet` from an iterator.
    /// If the iterator yields multiple values that map to the same variant, the last value will be used.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum, Debug, PartialEq)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let iter = vec![MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42), MyEnum::Variant1("World".to_string())].into_iter();
    /// let set = VariantSet::from_iter(iter);
    ///
    /// assert_eq!(set.len(), 2);
    /// assert!(set.contains_exact(&MyEnum::Variant1("World".to_string())));
    /// ```
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut set = VariantSet::new();
        set.extend(iter);
        set
    }
}

impl<T, const N: usize> From<[T; N]> for VariantSet<T>
where
    T: VariantEnum,
{
    /// Creates a new `VariantSet` from an array.
    ///
    /// # Examples
    /// ```
    /// use variant_set::{VariantSet, VariantEnum};
    ///
    /// #[derive(VariantEnum)]
    /// enum MyEnum {
    ///     Variant1(String),
    ///     Variant2(u32),
    /// }
    ///
    /// let array = [MyEnum::Variant1("Hello".to_string()), MyEnum::Variant2(42)];
    /// let set = VariantSet::from(array);
    ///
    /// assert_eq!(set.len(), 2);
    /// ```
    fn from(array: [T; N]) -> Self {
        Self::from_iter(array)
    }
}