Skip to content

Commit 48613c8

Browse files
authored
Unrolled build for #154322
Rollup merge of #154322 - m4rch3n1ng:hash-map-macro, r=Mark-Simulacrum feat: reimplement `hash_map!` macro originally implemented in #144070, this had to be reverted in #148049 due to name ambiguity, as the macro was automatically put into the prelude. now, that #139493 has landed, it is possible to have a top-level macro, that is not exported by default, which should make it possible to reland this again. implements #144032 implementation from #144070, original author has been added as co-author effectively reverts #148049
2 parents a25435b + 2e65734 commit 48613c8

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

library/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@
337337
#![feature(formatting_options)]
338338
#![feature(funnel_shifts)]
339339
#![feature(generic_atomic)]
340+
#![feature(hash_map_internals)]
341+
#![feature(hash_map_macro)]
340342
#![feature(hasher_prefixfree_extras)]
341343
#![feature(hashmap_internals)]
342344
#![feature(hint_must_use)]

library/std/src/macros.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,79 @@ pub macro dbg_internal {
414414
}
415415
},
416416
}
417+
418+
#[doc(hidden)]
419+
#[macro_export]
420+
#[allow_internal_unstable(hash_map_internals)]
421+
#[unstable(feature = "hash_map_internals", issue = "none")]
422+
macro_rules! repetition_utils {
423+
(@count $($tokens:tt),*) => {{
424+
[$($crate::repetition_utils!(@replace $tokens => ())),*].len()
425+
}};
426+
427+
(@replace $x:tt => $y:tt) => { $y }
428+
}
429+
430+
/// Creates a [`HashMap`] containing the arguments.
431+
///
432+
/// `hash_map!` allows specifying the entries that make
433+
/// up the [`HashMap`] where the key and value are separated by a `=>`.
434+
///
435+
/// The entries are separated by commas with a trailing comma being allowed.
436+
///
437+
/// It is semantically equivalent to using repeated [`HashMap::insert`]
438+
/// on a newly created hashmap.
439+
///
440+
/// `hash_map!` will attempt to avoid repeated reallocations by
441+
/// using [`HashMap::with_capacity`].
442+
///
443+
/// # Examples
444+
///
445+
/// ```rust
446+
/// #![feature(hash_map_macro)]
447+
/// use std::hash_map;
448+
///
449+
/// let map = hash_map! {
450+
/// "key" => "value",
451+
/// "key1" => "value1"
452+
/// };
453+
///
454+
/// assert_eq!(map.get("key"), Some(&"value"));
455+
/// assert_eq!(map.get("key1"), Some(&"value1"));
456+
/// assert!(map.get("brrrrrrooooommm").is_none());
457+
/// ```
458+
///
459+
/// And with a trailing comma
460+
///
461+
///```rust
462+
/// #![feature(hash_map_macro)]
463+
/// use std::hash_map;
464+
///
465+
/// let map = hash_map! {
466+
/// "key" => "value", // notice the ,
467+
/// };
468+
///
469+
/// assert_eq!(map.get("key"), Some(&"value"));
470+
/// ```
471+
///
472+
/// The key and value are moved into the HashMap.
473+
///
474+
/// [`HashMap`]: crate::collections::HashMap
475+
/// [`HashMap::insert`]: crate::collections::HashMap::insert
476+
/// [`HashMap::with_capacity`]: crate::collections::HashMap::with_capacity
477+
#[macro_export]
478+
#[allow_internal_unstable(hash_map_internals)]
479+
#[unstable(feature = "hash_map_macro", issue = "144032")]
480+
macro_rules! hash_map {
481+
() => {{
482+
$crate::collections::HashMap::new()
483+
}};
484+
485+
( $( $key:expr => $value:expr ),* $(,)? ) => {{
486+
let mut map = $crate::collections::HashMap::with_capacity(
487+
const { $crate::repetition_utils!(@count $($key),*) }
488+
);
489+
$( map.insert($key, $value); )*
490+
map
491+
}}
492+
}

0 commit comments

Comments
 (0)