nonempty_collections/
map.rs

1//! Non-empty [`HashMap`]s.
2
3use core::fmt;
4use std::borrow::Borrow;
5use std::collections::HashMap;
6use std::hash::BuildHasher;
7use std::hash::Hash;
8use std::num::NonZeroUsize;
9
10#[cfg(feature = "serde")]
11use serde::Deserialize;
12#[cfg(feature = "serde")]
13use serde::Serialize;
14
15use crate::FromNonEmptyIterator;
16use crate::IntoIteratorExt;
17use crate::IntoNonEmptyIterator;
18use crate::NonEmptyIterator;
19use crate::Singleton;
20
21/// Like the [`crate::nev!`] macro, but for Maps. A nice short-hand for
22/// constructing [`NEMap`] values.
23///
24/// ```
25/// use nonempty_collections::nem;
26///
27/// let m = nem! {"elves" => 3000, "orcs" => 10000};
28/// assert_eq!(2, m.len().get());
29/// ```
30#[macro_export]
31macro_rules! nem {
32    ($hk:expr => $hv:expr, $( $xk:expr => $xv:expr ),* $(,)?) => {{
33        let mut map = $crate::NEMap::new($hk, $hv);
34        $( map.insert($xk, $xv); )*
35        map
36    }};
37    ($hk:expr => $hv:expr) => {
38        $crate::NEMap::new($hk, $hv)
39    }
40}
41
42/// A non-empty, growable `HashMap`.
43///
44/// ```
45/// use nonempty_collections::nem;
46///
47/// let m = nem!["elves" => 3000, "orcs" => 10000];
48/// assert_eq!(2, m.len().get());
49/// ```
50#[allow(clippy::unsafe_derive_deserialize)]
51#[cfg_attr(
52    feature = "serde",
53    derive(Deserialize, Serialize),
54    serde(bound(
55        serialize = "K: Eq + Hash + Clone + Serialize, V: Clone + Serialize, S: Clone + BuildHasher",
56        deserialize = "K: Eq + Hash + Clone + Deserialize<'de>, V: Deserialize<'de>, S: Default + BuildHasher"
57    )),
58    serde(into = "HashMap<K, V, S>", try_from = "HashMap<K, V, S>")
59)]
60#[derive(Clone)]
61pub struct NEMap<K, V, S = std::collections::hash_map::RandomState> {
62    inner: HashMap<K, V, S>,
63}
64
65impl<K, V> NEMap<K, V>
66where
67    K: Eq + Hash,
68{
69    /// Creates a new `NEMap` with a single element.
70    #[must_use]
71    pub fn new(k: K, v: V) -> NEMap<K, V> {
72        let mut inner = HashMap::new();
73        inner.insert(k, v);
74        NEMap { inner }
75    }
76
77    /// Creates a new `NEMap` with a single element and specified capacity.
78    /// ```
79    /// use std::num::*;
80    ///
81    /// use nonempty_collections::*;
82    /// let map = NEMap::with_capacity(NonZeroUsize::MIN, 1, 1);
83    /// assert_eq!(nem! { 1 => 1 }, map);
84    /// assert!(map.capacity().get() >= 1);
85    /// ```
86    #[must_use]
87    pub fn with_capacity(capacity: NonZeroUsize, k: K, v: V) -> NEMap<K, V> {
88        let mut inner = HashMap::with_capacity(capacity.get());
89        inner.insert(k, v);
90        NEMap { inner }
91    }
92}
93
94impl<K, V, S> NEMap<K, V, S> {
95    /// Attempt a conversion from [`HashMap`], consuming the given `HashMap`.
96    /// Will return `None` if the `HashMap` is empty.
97    ///
98    /// ```
99    /// use std::collections::*;
100    ///
101    /// use nonempty_collections::*;
102    ///
103    /// let mut map = HashMap::new();
104    /// map.extend([("a", 1), ("b", 2)]);
105    /// assert_eq!(Some(nem! {"a" => 1, "b" => 2}), NEMap::try_from_map(map));
106    /// let map: HashMap<(), ()> = HashMap::new();
107    /// assert_eq!(None, NEMap::try_from_map(map));
108    /// ```
109    #[must_use]
110    pub fn try_from_map(map: HashMap<K, V, S>) -> Option<Self> {
111        if map.is_empty() {
112            None
113        } else {
114            Some(Self { inner: map })
115        }
116    }
117
118    /// Returns the number of elements the map can hold without reallocating.
119    #[must_use]
120    pub fn capacity(&self) -> NonZeroUsize {
121        unsafe { NonZeroUsize::new_unchecked(self.inner.capacity()) }
122    }
123
124    /// Returns a reference to the map's `BuildHasher`.
125    #[must_use]
126    pub fn hasher(&self) -> &S {
127        self.inner.hasher()
128    }
129
130    /// Returns a regular iterator over the entries in this non-empty map.
131    ///
132    /// For a `NonEmptyIterator` see `Self::nonempty_iter()`.
133    pub fn iter(&self) -> std::collections::hash_map::Iter<'_, K, V> {
134        self.inner.iter()
135    }
136
137    /// Returns a regular mutable iterator over the entries in this non-empty
138    /// map.
139    ///
140    /// For a `NonEmptyIterator` see `Self::nonempty_iter_mut()`.
141    pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<'_, K, V> {
142        self.inner.iter_mut()
143    }
144
145    /// An iterator visiting all elements in arbitrary order. The iterator
146    /// element type is `(&'a K, &'a V)`.
147    pub fn nonempty_iter(&self) -> Iter<'_, K, V> {
148        Iter {
149            iter: self.inner.iter(),
150        }
151    }
152
153    /// An iterator visiting all elements in arbitrary order. The iterator
154    /// element type is `(&'a K, &'a mut V)`.
155    ///
156    /// # Panics
157    ///
158    /// If you manually advance this iterator until empty and then call `first`,
159    /// you're in for a surprise.
160    pub fn nonempty_iter_mut(&mut self) -> IterMut<'_, K, V> {
161        IterMut {
162            iter: self.inner.iter_mut(),
163        }
164    }
165
166    /// An iterator visiting all keys in arbitrary order. The iterator element
167    /// type is `&'a K`.
168    ///
169    /// ```
170    /// use nonempty_collections::*;
171    ///
172    /// let m = nem!["Valmar" => "Vanyar", "Tirion" => "Noldor", "Alqualondë" => "Teleri"];
173    /// let mut v: NEVec<_> = m.keys().collect();
174    /// v.sort();
175    /// assert_eq!(nev![&"Alqualondë", &"Tirion", &"Valmar"], v);
176    /// ```
177    pub fn keys(&self) -> Keys<'_, K, V> {
178        Keys {
179            inner: self.inner.keys(),
180        }
181    }
182
183    /// Returns the number of elements in the map. Always 1 or more.
184    ///
185    /// ```
186    /// use nonempty_collections::nem;
187    ///
188    /// let m = nem!["a" => 1, "b" => 2];
189    /// assert_eq!(2, m.len().get());
190    /// ```
191    #[must_use]
192    pub fn len(&self) -> NonZeroUsize {
193        unsafe { NonZeroUsize::new_unchecked(self.inner.len()) }
194    }
195
196    /// A `NEMap` is never empty.
197    #[deprecated(since = "0.1.0", note = "A NEMap is never empty.")]
198    #[must_use]
199    pub const fn is_empty(&self) -> bool {
200        false
201    }
202
203    /// An iterator visiting all values in arbitrary order. The iterator element
204    /// type is `&'a V`.
205    ///
206    /// ```
207    /// use nonempty_collections::*;
208    ///
209    /// let m = nem!["Valmar" => "Vanyar", "Tirion" => "Noldor", "Alqualondë" => "Teleri"];
210    /// let mut v: NEVec<_> = m.values().collect();
211    /// v.sort();
212    /// assert_eq!(nev![&"Noldor", &"Teleri", &"Vanyar"], v);
213    /// ```
214    pub fn values(&self) -> Values<'_, K, V> {
215        Values {
216            inner: self.inner.values(),
217        }
218    }
219
220    // /// An iterator visiting all values mutably in arbitrary order. The iterator
221    // /// element type is `&'a mut V`.
222    // ///
223    // /// ```
224    // /// use nonempty_collections::nem;
225    // ///
226    // /// let mut m = nem!["Valmar" => 10000, "Tirion" => 10000, "Alqualondë" =>
227    // 10000]; ///
228    // /// for v in m.values_mut() {
229    // ///     *v += 1000;
230    // /// }
231    // /// ```
232    // pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
233    //     ValuesMut {
234    //         inner: self.iter_mut(),
235    //         head_val: todo!(),
236    //     }
237    // }
238}
239
240impl<K, V, S> NEMap<K, V, S>
241where
242    K: Eq + Hash,
243    S: BuildHasher,
244{
245    /// Returns true if the map contains a value.
246    ///
247    /// ```
248    /// use nonempty_collections::nem;
249    ///
250    /// let m = nem!["Jack" => 8];
251    /// assert!(m.contains_key("Jack"));
252    /// assert!(!m.contains_key("Colin"));
253    /// ```
254    #[must_use]
255    pub fn contains_key<Q>(&self, k: &Q) -> bool
256    where
257        K: Borrow<Q>,
258        Q: Eq + Hash + ?Sized,
259    {
260        self.inner.contains_key(k)
261    }
262
263    /// Returns a reference to the value corresponding to the key.
264    ///
265    /// The key may be any borrowed form of the map's value type, but `Hash` and
266    /// `Eq` on the borrowed form must match those for the key type.
267    ///
268    /// ```
269    /// use nonempty_collections::nem;
270    ///
271    /// let m = nem!["silmarils" => 3];
272    /// assert_eq!(Some(&3), m.get("silmarils"));
273    /// assert_eq!(None, m.get("arkenstone"));
274    /// ```
275    #[must_use]
276    pub fn get<Q>(&self, k: &Q) -> Option<&V>
277    where
278        K: Borrow<Q>,
279        Q: Eq + Hash + ?Sized,
280    {
281        self.inner.get(k)
282    }
283
284    /// Returns the key-value pair corresponding to the key.
285    ///
286    /// The key may be any borrowed form of the map's value type, but `Hash` and
287    /// `Eq` on the borrowed form must match those for the key type.
288    ///
289    /// ```
290    /// use nonempty_collections::nem;
291    ///
292    /// let m = nem!["silmarils" => 3];
293    /// assert_eq!(Some((&"silmarils", &3)), m.get_key_value("silmarils"));
294    /// assert_eq!(None, m.get_key_value("arkenstone"));
295    /// ```
296    #[must_use]
297    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
298    where
299        K: Borrow<Q>,
300        Q: Eq + Hash + ?Sized,
301    {
302        self.inner.get_key_value(k)
303    }
304
305    /// Returns a reference to the value corresponding to the key.
306    ///
307    /// The key may be any borrowed form of the map's value type, but `Hash` and
308    /// `Eq` on the borrowed form must match those for the key type.
309    ///
310    /// ```
311    /// use nonempty_collections::nem;
312    ///
313    /// let mut m = nem!["silmarils" => 3];
314    /// let mut v = m.get_mut("silmarils").unwrap();
315    ///
316    /// // And thus it came to pass that the Silmarils found their long homes:
317    /// // one in the airs of heaven, and one in the fires of the heart of the
318    /// // world, and one in the deep waters.
319    /// *v -= 3;
320    ///
321    /// assert_eq!(Some(&0), m.get("silmarils"));
322    /// ```
323    #[must_use]
324    pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
325    where
326        K: Borrow<Q>,
327        Q: Eq + Hash + ?Sized,
328    {
329        self.inner.get_mut(k)
330    }
331
332    /// Insert a key-value pair into the map.
333    ///
334    /// If the map did not have this present, [`None`] is returned.
335    ///
336    /// If the map did have this key present, the value is updated, and the old
337    /// value is returned. The key is not updated, though; this matters for
338    /// types that can be `==` without being identical. See [`HashMap::insert`]
339    /// for more.
340    ///
341    /// ```
342    /// use nonempty_collections::nem;
343    ///
344    /// let mut m = nem!["Vilya" => "Elrond", "Nenya" => "Galadriel"];
345    /// assert_eq!(None, m.insert("Narya", "Cirdan"));
346    ///
347    /// // The Ring of Fire was given to Gandalf upon his arrival in Middle Earth.
348    /// assert_eq!(Some("Cirdan"), m.insert("Narya", "Gandalf"));
349    /// ```
350    pub fn insert(&mut self, k: K, v: V) -> Option<V> {
351        self.inner.insert(k, v)
352    }
353
354    /// Shrinks the capacity of the map as much as possible. It will drop down
355    /// as much as possible while maintaining the internal rules and possibly
356    /// leaving some space in accordance with the resize policy.
357    pub fn shrink_to_fit(&mut self) {
358        self.inner.shrink_to_fit();
359    }
360
361    /// See [`HashMap::with_capacity_and_hasher`].
362    #[must_use]
363    pub fn with_capacity_and_hasher(
364        capacity: NonZeroUsize,
365        hasher: S,
366        k: K,
367        v: V,
368    ) -> NEMap<K, V, S> {
369        let mut inner = HashMap::with_capacity_and_hasher(capacity.get(), hasher);
370        inner.insert(k, v);
371        NEMap { inner }
372    }
373
374    /// See [`HashMap::with_hasher`].
375    #[must_use]
376    pub fn with_hasher(hasher: S, k: K, v: V) -> NEMap<K, V, S> {
377        let mut inner = HashMap::with_hasher(hasher);
378        inner.insert(k, v);
379        NEMap { inner }
380    }
381}
382
383impl<K, V, S> AsRef<HashMap<K, V, S>> for NEMap<K, V, S> {
384    fn as_ref(&self) -> &HashMap<K, V, S> {
385        &self.inner
386    }
387}
388
389impl<K, V, S> AsMut<HashMap<K, V, S>> for NEMap<K, V, S> {
390    fn as_mut(&mut self) -> &mut HashMap<K, V, S> {
391        &mut self.inner
392    }
393}
394
395impl<K, V, S> PartialEq for NEMap<K, V, S>
396where
397    K: Eq + Hash,
398    V: PartialEq,
399    S: BuildHasher,
400{
401    /// This is an `O(n)` comparison of each key/value pair, one by one.
402    /// Short-circuits if any comparison fails.
403    ///
404    /// ```
405    /// use nonempty_collections::*;
406    ///
407    /// let m0 = nem!['a' => 1, 'b' => 2];
408    /// let m1 = nem!['b' => 2, 'a' => 1];
409    /// assert_eq!(m0, m1);
410    /// ```
411    fn eq(&self, other: &Self) -> bool {
412        self.inner.eq(&other.inner)
413    }
414}
415
416impl<K, V, S> Eq for NEMap<K, V, S>
417where
418    K: Eq + Hash,
419    V: Eq,
420    S: BuildHasher,
421{
422}
423
424impl<K, V, S> From<NEMap<K, V, S>> for HashMap<K, V, S>
425where
426    K: Eq + Hash,
427    S: BuildHasher,
428{
429    /// ```
430    /// use nonempty_collections::nem;
431    /// use std::collections::HashMap;
432    ///
433    /// let m: HashMap<&str, usize> = nem!["population" => 1000].into();
434    /// assert!(m.contains_key("population"));
435    /// ```
436    fn from(m: NEMap<K, V, S>) -> Self {
437        m.inner
438    }
439}
440
441impl<K, V, S> TryFrom<HashMap<K, V, S>> for NEMap<K, V, S>
442where
443    K: Eq + Hash,
444    S: BuildHasher + Default,
445{
446    type Error = crate::Error;
447
448    fn try_from(map: HashMap<K, V, S>) -> Result<Self, Self::Error> {
449        map.try_into_nonempty_iter()
450            .map(NonEmptyIterator::collect)
451            .ok_or(crate::Error::Empty)
452    }
453}
454
455impl<K, V, S> IntoNonEmptyIterator for NEMap<K, V, S> {
456    type IntoNEIter = IntoIter<K, V>;
457
458    fn into_nonempty_iter(self) -> Self::IntoNEIter {
459        IntoIter {
460            iter: self.inner.into_iter(),
461        }
462    }
463}
464
465impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEMap<K, V, S> {
466    type IntoNEIter = Iter<'a, K, V>;
467
468    fn into_nonempty_iter(self) -> Self::IntoNEIter {
469        self.nonempty_iter()
470    }
471}
472
473impl<K, V, S> IntoIterator for NEMap<K, V, S> {
474    type Item = (K, V);
475
476    type IntoIter = std::collections::hash_map::IntoIter<K, V>;
477
478    fn into_iter(self) -> Self::IntoIter {
479        self.inner.into_iter()
480    }
481}
482
483impl<'a, K, V, S> IntoIterator for &'a NEMap<K, V, S> {
484    type Item = (&'a K, &'a V);
485
486    type IntoIter = std::collections::hash_map::Iter<'a, K, V>;
487
488    fn into_iter(self) -> Self::IntoIter {
489        self.iter()
490    }
491}
492
493impl<'a, K, V, S> IntoIterator for &'a mut NEMap<K, V, S> {
494    type Item = (&'a K, &'a mut V);
495
496    type IntoIter = std::collections::hash_map::IterMut<'a, K, V>;
497
498    fn into_iter(self) -> Self::IntoIter {
499        self.iter_mut()
500    }
501}
502
503/// ```
504/// use nonempty_collections::*;
505///
506/// let v = nev![('a', 1), ('b', 2), ('c', 3), ('a', 4)];
507/// let m0: NEMap<_, _> = v.into_nonempty_iter().collect();
508/// let m1: NEMap<_, _> = nem!['a' => 4, 'b' => 2, 'c' => 3];
509/// assert_eq!(m0, m1);
510/// ```
511impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEMap<K, V, S>
512where
513    K: Eq + Hash,
514    S: BuildHasher + Default,
515{
516    fn from_nonempty_iter<I>(iter: I) -> Self
517    where
518        I: IntoNonEmptyIterator<Item = (K, V)>,
519    {
520        NEMap {
521            inner: iter.into_nonempty_iter().into_iter().collect(),
522        }
523    }
524}
525
526/// A non-empty iterator over the entries of an [`NEMap`].
527#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
528pub struct Iter<'a, K: 'a, V: 'a> {
529    iter: std::collections::hash_map::Iter<'a, K, V>,
530}
531
532impl<K, V> NonEmptyIterator for Iter<'_, K, V> {}
533
534impl<'a, K, V> IntoIterator for Iter<'a, K, V> {
535    type Item = (&'a K, &'a V);
536
537    type IntoIter = std::collections::hash_map::Iter<'a, K, V>;
538
539    fn into_iter(self) -> Self::IntoIter {
540        self.iter
541    }
542}
543
544impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
545    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
546        self.iter.fmt(f)
547    }
548}
549
550/// A non-empty iterator over mutable values of an [`NEMap`].
551#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
552pub struct IterMut<'a, K: 'a, V: 'a> {
553    iter: std::collections::hash_map::IterMut<'a, K, V>,
554}
555
556impl<K, V> NonEmptyIterator for IterMut<'_, K, V> {}
557
558impl<'a, K, V> IntoIterator for IterMut<'a, K, V> {
559    type Item = (&'a K, &'a mut V);
560
561    type IntoIter = std::collections::hash_map::IterMut<'a, K, V>;
562
563    fn into_iter(self) -> Self::IntoIter {
564        self.iter
565    }
566}
567
568impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
569    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
570        self.iter.fmt(f)
571    }
572}
573
574/// A non-empty iterator over the entries of an [`NEMap`].
575pub struct IntoIter<K, V> {
576    iter: std::collections::hash_map::IntoIter<K, V>,
577}
578
579impl<K, V> NonEmptyIterator for IntoIter<K, V> {}
580
581impl<K, V> IntoIterator for IntoIter<K, V> {
582    type Item = (K, V);
583
584    type IntoIter = std::collections::hash_map::IntoIter<K, V>;
585
586    fn into_iter(self) -> Self::IntoIter {
587        self.iter
588    }
589}
590
591impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
592    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
593        self.iter.fmt(f)
594    }
595}
596
597/// A non-empty iterator over the keys of an [`NEMap`].
598#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
599pub struct Keys<'a, K: 'a, V: 'a> {
600    inner: std::collections::hash_map::Keys<'a, K, V>,
601}
602
603impl<K, V> NonEmptyIterator for Keys<'_, K, V> {}
604
605impl<'a, K, V> IntoIterator for Keys<'a, K, V> {
606    type Item = &'a K;
607
608    type IntoIter = std::collections::hash_map::Keys<'a, K, V>;
609
610    fn into_iter(self) -> Self::IntoIter {
611        self.inner
612    }
613}
614
615impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Keys<'_, K, V> {
616    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
617        self.inner.fmt(f)
618    }
619}
620
621/// A non-empty iterator over the values of an [`NEMap`].
622#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
623pub struct Values<'a, K: 'a, V: 'a> {
624    inner: std::collections::hash_map::Values<'a, K, V>,
625}
626
627impl<K, V> NonEmptyIterator for Values<'_, K, V> {}
628
629impl<'a, K, V> IntoIterator for Values<'a, K, V> {
630    type Item = &'a V;
631
632    type IntoIter = std::collections::hash_map::Values<'a, K, V>;
633
634    fn into_iter(self) -> Self::IntoIter {
635        self.inner
636    }
637}
638
639impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
640    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
641        self.inner.fmt(f)
642    }
643}
644
645impl<K: fmt::Debug, V: fmt::Debug, S> fmt::Debug for NEMap<K, V, S> {
646    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
647        self.inner.fmt(f)
648    }
649}
650
651// /// A non-empty iterator over mutable values of an [`NEMap`].
652// pub struct ValuesMut<'a, K: 'a, V: 'a> {
653//     inner: IterMut<'a, K, V>,
654// }
655
656// impl<'a, K, V> NonEmptyIterator for ValuesMut<'a, K, V> {
657//     type Item = &'a mut V;
658
659//     type Iter = Skip<Chain<Once<&'a mut V>,
660// std::collections::hash_map::IterMut<'a, K, V>>>;
661
662//     fn first(self) -> (Self::Item, Self::Iter) {
663//         (self.head_val, self.inner.skip(1))
664//     }
665
666//     fn next(&mut self) -> Option<Self::Item> {
667//         self.inner.next().map(|(_, v)| v)
668//     }
669// }
670
671impl<K, V> Singleton for NEMap<K, V>
672where
673    K: Eq + Hash,
674{
675    type Item = (K, V);
676
677    /// ```
678    /// use nonempty_collections::{NEMap, Singleton, nem};
679    ///
680    /// let m = NEMap::singleton(('a', 1));
681    /// assert_eq!(nem!['a' => 1], m);
682    /// ```
683    fn singleton((k, v): Self::Item) -> Self {
684        NEMap::new(k, v)
685    }
686}
687
688impl<K, V> Extend<(K, V)> for NEMap<K, V>
689where
690    K: Eq + Hash,
691{
692    fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
693        self.inner.extend(iter);
694    }
695}
696
697#[cfg(test)]
698mod test {
699    use std::num::NonZeroUsize;
700
701    use maplit::hashmap;
702
703    use crate::nem;
704
705    struct Foo {
706        user: String,
707    }
708
709    #[test]
710    fn debug_impl() {
711        let expected = format!("{:?}", hashmap! {0 => 10});
712        let actual = format!("{:?}", nem! {0 => 10});
713        assert_eq!(expected, actual);
714    }
715
716    #[test]
717    fn macro_usage() {
718        let a = Foo {
719            user: "a".to_string(),
720        };
721        let b = Foo {
722            user: "b".to_string(),
723        };
724
725        let map = nem![1 => a, 2 => b];
726        assert_eq!("a", map.get(&1).unwrap().user);
727        assert_eq!("b", map.get(&2).unwrap().user);
728    }
729
730    #[test]
731    fn macro_length() {
732        let map = nem![1 => 'a', 2 => 'b', 1 => 'c'];
733        assert_eq!(unsafe { NonZeroUsize::new_unchecked(2) }, map.len());
734        assert_eq!('c', *map.get(&1).unwrap());
735        assert_eq!('b', *map.get(&2).unwrap());
736    }
737
738    #[test]
739    fn iter_mut() {
740        let mut v = nem! {"a" => 0, "b" => 1, "c" => 2};
741
742        v.iter_mut().for_each(|(_k, v)| {
743            *v += 1;
744        });
745        assert_eq!(nem! {"a" => 1, "b" => 2, "c" => 3}, v);
746
747        for (_k, v) in &mut v {
748            *v -= 1;
749        }
750        assert_eq!(nem! {"a" => 0, "b" => 1, "c" => 2}, v);
751    }
752}
753
754#[cfg(feature = "serde")]
755#[cfg(test)]
756mod serde_tests {
757    use std::collections::HashMap;
758
759    use crate::nem;
760    use crate::NEMap;
761
762    #[test]
763    fn json() {
764        let map0 = nem![1 => 'a', 2 => 'b', 1 => 'c'];
765        let j = serde_json::to_string(&map0).unwrap();
766        let map1 = serde_json::from_str(&j).unwrap();
767        assert_eq!(map0, map1);
768
769        let empty: HashMap<usize, char> = HashMap::new();
770        let j = serde_json::to_string(&empty).unwrap();
771        let bad = serde_json::from_str::<NEMap<usize, char>>(&j);
772        assert!(bad.is_err());
773    }
774}