micromap_rawl/lib.rs
1mod clone;
2mod ctors;
3mod debug;
4mod display;
5mod drain;
6mod entry;
7mod eq;
8mod from;
9mod index;
10mod iterators;
11mod keys;
12mod map;
13mod set;
14mod values;
15
16pub use crate::set::{Set, SetDrain, SetIntoIter, SetIter};
17use core::mem::MaybeUninit;
18
19/// A faster alternative of [`std::collections::HashMap`].
20///
21/// For example, this is how you make a map, which is allocated on stack and is capable of storing
22/// up to eight key-values pairs:
23///
24/// ```
25/// let mut m : micromap_rawl::Map<u64, &str, 8> = micromap_rawl::Map::new();
26/// m.insert(1, "Jeff Lebowski");
27/// m.insert(2, "Walter Sobchak");
28/// # #[cfg(std)]
29/// assert_eq!(2, m.len());
30/// ```
31///
32/// It is faster because it doesn't use a hash function at all. It simply keeps
33/// all pairs in an array and when it's necessary to find a value, it goes through
34/// all pairs comparing the needle with each pair available. Also it is faster
35/// because it doesn't use heap. When a [`Map`] is being created, it allocates the necessary
36/// space on stack. That's why the maximum size of the map must be provided in
37/// compile time.
38///
39/// It is also faster because it doesn't grow in size. When a [`Map`] is created,
40/// its size is fixed on stack. If an attempt is made to insert too many keys
41/// into it, it simply panics. Moreover, in the "release" mode it doesn't panic,
42/// but its behaviour is undefined. In the "release" mode all boundary checks
43/// are disabled, for the sake of higher performance.
44pub struct Map<K: PartialEq, V, const N: usize> {
45 /// The next available pair in the array.
46 len: usize,
47 /// The fixed-size array of key-value pairs.
48 pairs: [MaybeUninit<(K, V)>; N],
49}
50
51/// Iterator over the [`Map`].
52#[repr(transparent)]
53pub struct Iter<'a, K, V> {
54 iter: core::slice::Iter<'a, MaybeUninit<(K, V)>>,
55}
56
57/// Mutable Iterator over the [`Map`].
58#[repr(transparent)]
59pub struct IterMut<'a, K, V> {
60 iter: core::slice::IterMut<'a, MaybeUninit<(K, V)>>,
61}
62
63/// Into-iterator over the [`Map`].
64#[repr(transparent)]
65pub struct IntoIter<K: PartialEq, V, const N: usize> {
66 map: Map<K, V, N>,
67}
68
69/// An iterator over the values of the [`Map`].
70#[repr(transparent)]
71pub struct Values<'a, K, V> {
72 iter: Iter<'a, K, V>,
73}
74
75/// Mutable iterator over the values of the [`Map`].
76#[repr(transparent)]
77pub struct ValuesMut<'a, K, V> {
78 iter: IterMut<'a, K, V>,
79}
80
81/// Consuming iterator over the values of the [`Map`].
82#[repr(transparent)]
83pub struct IntoValues<K: PartialEq, V, const N: usize> {
84 iter: IntoIter<K, V, N>,
85}
86
87/// A read-only iterator over the keys of the [`Map`].
88#[repr(transparent)]
89pub struct Keys<'a, K, V> {
90 iter: Iter<'a, K, V>,
91}
92
93/// Consuming iterator over the keys of the [`Map`].
94#[repr(transparent)]
95pub struct IntoKeys<K: PartialEq, V, const N: usize> {
96 iter: IntoIter<K, V, N>,
97}
98
99/// A view into a single entry in a map, which may either be vacant or occupied.
100///
101/// This `enum` is constructed from the [`entry`] method on [`Map`].
102///
103/// [`entry`]: Map::entry
104pub enum Entry<'a, K: 'a + PartialEq, V: 'a, const N: usize> {
105 /// An occupied entry.
106 Occupied(OccupiedEntry<'a, K, V, N>),
107
108 /// A vacant entry.
109 Vacant(VacantEntry<'a, K, V, N>),
110}
111
112/// A view into an occupied entry in a `Map`.
113/// It is part of the [`Entry`] enum.
114pub struct OccupiedEntry<'a, K: 'a + PartialEq, V: 'a, const N: usize> {
115 index: usize,
116 table: &'a mut Map<K, V, N>,
117}
118
119/// A view into a vacant entry in a `Map`.
120/// It is part of the [`Entry`] enum.
121pub struct VacantEntry<'a, K: 'a + PartialEq, V: 'a, const N: usize> {
122 key: K,
123 table: &'a mut Map<K, V, N>,
124}
125
126/// A draining iterator over the entries of a `Map`.
127///
128/// This struct is created by the drain method on `Map`. See its documentation for more.
129pub struct Drain<'a, K: 'a, V: 'a> {
130 iter: core::slice::IterMut<'a, MaybeUninit<(K, V)>>,
131}