|
| 1 | +/// Create an [`RingMap`][crate::RingMap] from a list of key-value pairs |
| 2 | +/// and a `BuildHasherDefault`-wrapped custom hasher. |
| 3 | +/// |
| 4 | +/// ## Example |
| 5 | +/// |
| 6 | +/// ``` |
| 7 | +/// use ringmap::ringmap_with_default; |
| 8 | +/// use fnv::FnvHasher; |
| 9 | +/// |
| 10 | +/// let map = ringmap_with_default!{ |
| 11 | +/// FnvHasher; |
| 12 | +/// "a" => 1, |
| 13 | +/// "b" => 2, |
| 14 | +/// }; |
| 15 | +/// assert_eq!(map["a"], 1); |
| 16 | +/// assert_eq!(map["b"], 2); |
| 17 | +/// assert_eq!(map.get("c"), None); |
| 18 | +/// |
| 19 | +/// // "a" is the first key |
| 20 | +/// assert_eq!(map.keys().next(), Some(&"a")); |
| 21 | +/// ``` |
| 22 | +#[macro_export] |
| 23 | +macro_rules! ringmap_with_default { |
| 24 | + ($H:ty; $($key:expr => $value:expr,)+) => { $crate::ringmap_with_default!($H; $($key => $value),+) }; |
| 25 | + ($H:ty; $($key:expr => $value:expr),*) => {{ |
| 26 | + let builder = ::core::hash::BuildHasherDefault::<$H>::default(); |
| 27 | + const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]); |
| 28 | + #[allow(unused_mut)] |
| 29 | + // Specify your custom `H` (must implement Default + Hasher) as the hasher: |
| 30 | + let mut map = $crate::RingMap::with_capacity_and_hasher(CAP, builder); |
| 31 | + $( |
| 32 | + map.insert($key, $value); |
| 33 | + )* |
| 34 | + map |
| 35 | + }}; |
| 36 | +} |
| 37 | + |
1 | 38 | #[cfg(feature = "std")]
|
2 | 39 | #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
3 | 40 | #[macro_export]
|
@@ -35,6 +72,43 @@ macro_rules! ringmap {
|
35 | 72 | };
|
36 | 73 | }
|
37 | 74 |
|
| 75 | +/// Create an [`RingSet`][crate::RingSet] from a list of values |
| 76 | +/// and a `BuildHasherDefault`-wrapped custom hasher. |
| 77 | +/// |
| 78 | +/// ## Example |
| 79 | +/// |
| 80 | +/// ``` |
| 81 | +/// use ringmap::ringset_with_default; |
| 82 | +/// use fnv::FnvHasher; |
| 83 | +/// |
| 84 | +/// let set = ringset_with_default!{ |
| 85 | +/// FnvHasher; |
| 86 | +/// "a", |
| 87 | +/// "b", |
| 88 | +/// }; |
| 89 | +/// assert!(set.contains("a")); |
| 90 | +/// assert!(set.contains("b")); |
| 91 | +/// assert!(!set.contains("c")); |
| 92 | +/// |
| 93 | +/// // "a" is the first value |
| 94 | +/// assert_eq!(set.iter().next(), Some(&"a")); |
| 95 | +/// ``` |
| 96 | +#[macro_export] |
| 97 | +macro_rules! ringset_with_default { |
| 98 | + ($H:ty; $($value:expr,)+) => { $crate::ringset_with_default!($H; $($value),+) }; |
| 99 | + ($H:ty; $($value:expr),*) => {{ |
| 100 | + let builder = ::core::hash::BuildHasherDefault::<$H>::default(); |
| 101 | + const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]); |
| 102 | + #[allow(unused_mut)] |
| 103 | + // Specify your custom `H` (must implement Default + Hash) as the hasher: |
| 104 | + let mut set = $crate::RingSet::with_capacity_and_hasher(CAP, builder); |
| 105 | + $( |
| 106 | + set.insert($value); |
| 107 | + )* |
| 108 | + set |
| 109 | + }}; |
| 110 | +} |
| 111 | + |
38 | 112 | #[cfg(feature = "std")]
|
39 | 113 | #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
40 | 114 | #[macro_export]
|
|
0 commit comments