Skip to main content

total_maps/
empty.rs

1//! Provides [EmptyCommonality], a [commonality](Commonality) for collection types where the common
2//! value is an empty collection.
3
4use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
5
6use crate::{Commonality, TotalBTreeMap, TotalHashMap};
7
8/// A [commonality](Commonality) for collection types, where the common value is an empty
9/// collection.
10///
11/// In most cases, this commonality behaves the same as
12/// [DefaultCommonality][super::DefaultCommonality] because typical collection types'
13/// implementations of [Default::default] yield empty collections. However, this commonality avoids
14/// the need for [PartialEq] bounds on the collection type.
15pub struct EmptyCommonality(());
16
17macro_rules! impl_empty {
18    ({$($generics:tt)*}, $Coll:path $(,)?) => {
19        impl<$($generics)*> Commonality<$Coll> for EmptyCommonality {
20            fn common() -> $Coll {
21                Default::default()
22            }
23            fn is_common(value: &$Coll) -> bool {
24                value.is_empty()
25            }
26        }
27    };
28}
29
30impl_empty!({ T }, Vec<T>);
31impl_empty!({ T }, VecDeque<T>);
32impl_empty!({ T }, HashSet<T>);
33impl_empty!({ K, V }, HashMap<K, V>);
34impl_empty!({ T }, BTreeSet<T>);
35impl_empty!({ K, V }, BTreeMap<K, V>);
36impl_empty!({ K, V, C: Commonality<V> }, TotalHashMap<K, V, C>);
37impl_empty!({ K, V, C: Commonality<V> }, TotalBTreeMap<K, V, C>);