Skip to content

Commit f2c7c2d

Browse files
authored
Merge pull request #39 from jofas/explicitly-typed-macros
Explicit equivalents for every macro and doc enhancements
2 parents ac465a2 + 2cb6bd9 commit f2c7c2d

File tree

5 files changed

+286
-27
lines changed

5 files changed

+286
-27
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
* `hashbrown::hash_set` macro
1616

17+
* `hashbrown::hash_set_e` macro
18+
19+
* `binary_heap_e` macro
20+
21+
* `btree_set_e` macro
22+
23+
* `hash_set_e` macro
24+
25+
* `vec_no_clone_e` macro
26+
1727
### Changed
1828

1929
* Explicitly typed map macros: keys also cast now (before only values were

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ let hello = hash_map! {
2222
"de" => "Hallo",
2323
"fr" => "Bonjour",
2424
"es" => "Hola",
25+
"cat" => "Hola",
26+
"🌍" => "👋",
2527
};
2628
```
2729

src/_std.rs

+137-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/// "de" => "Auf Wiedersehen",
1313
/// "fr" => "Au revoir",
1414
/// "es" => "Adios",
15+
/// "cat" => "Adéu",
1516
/// };
1617
/// ```
1718
///
@@ -22,8 +23,9 @@ macro_rules! hash_map {
2223
};
2324
}
2425

25-
/// Explicitly typed equivalent of [`hash_map!`], suitable for
26-
/// [trait object values](crate#explicitly-typed-values-for-trait-objects).
26+
/// Explicitly typed equivalent of [`hash_map!`].
27+
///
28+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
2729
///
2830
/// # Examples
2931
///
@@ -38,6 +40,7 @@ macro_rules! hash_map {
3840
/// "de" => &"Auf Wiedersehen",
3941
/// "fr" => &"Au revoir",
4042
/// "es" => &"Adios",
43+
/// "cat" => &"Adéu",
4144
/// };
4245
///
4346
/// println!("{:?}", goodbye);
@@ -64,6 +67,7 @@ macro_rules! hash_map_e {
6467
/// "de" => "Auf Wiedersehen",
6568
/// "fr" => "Au revoir",
6669
/// "es" => "Adios",
70+
/// "cat" => "Adéu",
6771
/// };
6872
/// ```
6973
///
@@ -74,8 +78,9 @@ macro_rules! btree_map {
7478
};
7579
}
7680

77-
/// Explicitly typed equivalent of [`btree_map!`], suitable for
78-
/// [trait object values](crate#explicitly-typed-values-for-trait-objects).
81+
/// Explicitly typed equivalent of [`btree_map!`].
82+
///
83+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
7984
///
8085
/// # Examples
8186
///
@@ -90,6 +95,7 @@ macro_rules! btree_map {
9095
/// "de" => &"Auf Wiedersehen",
9196
/// "fr" => &"Au revoir",
9297
/// "es" => &"Adios",
98+
/// "cat" => &"Adéu",
9399
/// };
94100
/// ```
95101
///
@@ -121,6 +127,31 @@ macro_rules! hash_set {
121127
};
122128
}
123129

130+
/// Explicitly typed equivalent of [`hash_set!`].
131+
///
132+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
133+
///
134+
/// # Examples
135+
///
136+
/// ```rust
137+
/// use std::collections::HashSet;
138+
///
139+
/// use map_macro::hash_set_e;
140+
///
141+
/// enum Foo { A, B, C, D }
142+
///
143+
/// let x: HashSet<u8> = hash_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
144+
///
145+
/// assert_eq!(x.len(), 4);
146+
/// ```
147+
///
148+
#[macro_export]
149+
macro_rules! hash_set_e {
150+
{$($v: expr),* $(,)?} => {
151+
::std::collections::HashSet::from([$($v as _,)*])
152+
};
153+
}
154+
124155
/// Macro for creating a [`BTreeSet`](::std::collections::BTreeSet).
125156
///
126157
/// Syntactic sugar for [`BTreeSet::from`](::std::collections::BTreeSet::from).
@@ -142,6 +173,31 @@ macro_rules! btree_set {
142173
};
143174
}
144175

176+
/// Explicitly typed equivalent of [`btree_set!`].
177+
///
178+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
179+
///
180+
/// # Examples
181+
///
182+
/// ```rust
183+
/// use std::collections::BTreeSet;
184+
///
185+
/// use map_macro::btree_set_e;
186+
///
187+
/// enum Foo { A, B, C, D }
188+
///
189+
/// let x: BTreeSet<u8> = btree_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
190+
///
191+
/// assert_eq!(x.len(), 4);
192+
/// ```
193+
///
194+
#[macro_export]
195+
macro_rules! btree_set_e {
196+
{$($v: expr),* $(,)?} => {
197+
::std::collections::BTreeSet::from([$($v as _,)*])
198+
};
199+
}
200+
145201
/// Macro for creating a [`VecDeque`](::std::collections::VecDeque).
146202
///
147203
/// Follows the same syntax as the [`vec!`](::std::vec!) macro.
@@ -173,8 +229,9 @@ macro_rules! vec_deque {
173229
};
174230
}
175231

176-
/// Explicitly typed equivalent of [`vec_deque!`], suitable for
177-
/// [trait object values](crate#explicitly-typed-values-for-trait-objects).
232+
/// Explicitly typed equivalent of [`vec_deque!`].
233+
///
234+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
178235
///
179236
/// # Examples
180237
///
@@ -237,8 +294,9 @@ macro_rules! linked_list {
237294
};
238295
}
239296

240-
/// Explicitly typed equivalent of [`linked_list!`], suitable for
241-
/// [trait object values](crate#explicitly-typed-values-for-trait-objects).
297+
/// Explicitly typed equivalent of [`linked_list!`].
298+
///
299+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
242300
///
243301
/// # Examples
244302
///
@@ -301,6 +359,41 @@ macro_rules! binary_heap {
301359
};
302360
}
303361

362+
/// Explicitly typed equivalent of [`binary_heap!`].
363+
///
364+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
365+
///
366+
/// # Examples
367+
///
368+
/// ```
369+
/// use std::collections::BinaryHeap;
370+
///
371+
/// use map_macro::binary_heap_e;
372+
///
373+
/// enum Foo { A, B, C, D }
374+
///
375+
/// let v: BinaryHeap<u8> = binary_heap_e![Foo::A, Foo::B, Foo::C, Foo::D];
376+
/// let v: BinaryHeap<u8> = binary_heap_e![Foo::A; 4];
377+
/// ```
378+
///
379+
#[macro_export]
380+
macro_rules! binary_heap_e {
381+
{$v: expr; $c: expr} => {
382+
{
383+
let mut bh = ::std::collections::BinaryHeap::with_capacity($c);
384+
385+
for _ in 0..$c {
386+
bh.push($v as _);
387+
}
388+
389+
bh
390+
}
391+
};
392+
{$($v: expr),* $(,)?} => {
393+
::std::collections::BinaryHeap::from([$($v as _,)*])
394+
};
395+
}
396+
304397
/// Version of the [`vec!`](::std::vec!) macro where the value does not have to implement [`Clone`].
305398
///
306399
/// Useful for unclonable types or where `Clone` is exerting undesired behaviour.
@@ -439,6 +532,7 @@ macro_rules! binary_heap {
439532
#[macro_export]
440533
macro_rules! vec_no_clone {
441534
{$v: expr; $c: expr} => {
535+
442536
{
443537
let mut vec = Vec::with_capacity($c);
444538

@@ -455,3 +549,38 @@ macro_rules! vec_no_clone {
455549
}
456550
};
457551
}
552+
553+
/// Explicitly typed equivalent of [`vec_no_clone!`].
554+
///
555+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
556+
///
557+
/// # Examples
558+
///
559+
/// ```
560+
/// use std::fmt::Display;
561+
///
562+
/// use map_macro::vec_no_clone_e;
563+
///
564+
/// let v: Vec<&dyn Display> = vec_no_clone_e![&0; 4];
565+
/// ```
566+
///
567+
#[macro_export]
568+
macro_rules! vec_no_clone_e {
569+
{$v: expr; $c: expr} => {
570+
571+
{
572+
let mut vec = Vec::with_capacity($c);
573+
574+
for _ in 0..$c {
575+
vec.push($v as _);
576+
}
577+
578+
vec
579+
}
580+
};
581+
{$($v: expr),* $(,)?} => {
582+
{
583+
vec![$($v as _),*]
584+
}
585+
};
586+
}

src/hashbrown.rs

+54-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
//! Macros for initializing [`hashbrown`] maps and sets.
22
//!
3-
//! # Supported versions of `hashbrown`
3+
//! # Example
4+
//!
5+
//! ```
6+
//! use map_macro::hashbrown::hash_map;
7+
//!
8+
//! let hello = hash_map! {
9+
//! "en" => "Hello",
10+
//! "de" => "Hallo",
11+
//! "fr" => "Bonjour",
12+
//! "es" => "Hola",
13+
//! "cat" => "Hola",
14+
//! "🌍" => "👋",
15+
//! };
16+
//! ```
17+
//!
18+
//! # Supported Versions of `hashbrown`
419
//!
520
//! As of writing this, up to the current `hashbrown` version `0.14` **all**
621
//! versions of `hashbrown` are supported.
@@ -15,9 +30,10 @@
1530
//! macros from this module would break for that new version.
1631
//!
1732
//! **Note:** to be compatible with all versions of `hashbrown` at once, this
18-
//! crate doesn't re-export `hashbrown`, which means that if you rename
19-
//! `hashbrown` in your dependencies, the macros from this module won't be able
20-
//! to import the needed types resulting in a compile-time error.
33+
//! crate doesn't re-export `hashbrown`.
34+
//! That means that (I) you need to specify it as a dependency yourself and
35+
//! (II) you can't rename it or the macros from this module won't be able to
36+
//! import the needed types, resulting in a compile-time error.
2137
//!
2238
2339
/// Macro for creating a [`HashMap`](::hashbrown::HashMap).
@@ -34,6 +50,7 @@
3450
/// "de" => "Auf Wiedersehen",
3551
/// "fr" => "Au revoir",
3652
/// "es" => "Adios",
53+
/// "cat" => "Adéu",
3754
/// };
3855
/// ```
3956
///
@@ -45,8 +62,9 @@ macro_rules! __hb_hash_map {
4562
};
4663
}
4764

48-
/// Explicitly typed equivalent of [`hash_map!`](self::hash_map), suitable for
49-
/// [trait object values](crate#explicitly-typed-values-for-trait-objects).
65+
/// Explicitly typed equivalent of [`hash_map!`](self::hash_map).
66+
///
67+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
5068
///
5169
/// # Examples
5270
///
@@ -62,6 +80,7 @@ macro_rules! __hb_hash_map {
6280
/// "de" => &"Auf Wiedersehen",
6381
/// "fr" => &"Au revoir",
6482
/// "es" => &"Adios",
83+
/// "cat" => &"Adéu",
6584
/// };
6685
///
6786
/// println!("{:?}", goodbye);
@@ -97,6 +116,32 @@ macro_rules! __hb_hash_set {
97116
};
98117
}
99118

119+
/// Explicitly typed equivalent of [`hash_set!`](self::hash_set).
120+
///
121+
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
122+
///
123+
/// # Examples
124+
///
125+
/// ```rust
126+
/// use hashbrown::HashSet;
127+
///
128+
/// use map_macro::hashbrown::hash_set_e;
129+
///
130+
/// enum Foo { A, B, C, D }
131+
///
132+
/// let x: HashSet<u8> = hash_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
133+
///
134+
/// assert_eq!(x.len(), 4);
135+
/// ```
136+
///
137+
#[doc(hidden)]
138+
#[macro_export]
139+
macro_rules! __hb_hash_set_e {
140+
{$($v: expr),* $(,)?} => {
141+
<::hashbrown::HashSet::<_> as ::core::iter::FromIterator<_>>::from_iter([$($v as _,)*])
142+
};
143+
}
144+
100145
#[doc(inline)]
101146
pub use __hb_hash_map as hash_map;
102147

@@ -105,3 +150,6 @@ pub use __hb_hash_map_e as hash_map_e;
105150

106151
#[doc(inline)]
107152
pub use __hb_hash_set as hash_set;
153+
154+
#[doc(inline)]
155+
pub use __hb_hash_set_e as hash_set_e;

0 commit comments

Comments
 (0)