diff --git a/crates/oxc_allocator/src/hash_map.rs b/crates/oxc_allocator/src/hash_map.rs index f2e1e54af80ba..e5c63bc8a999d 100644 --- a/crates/oxc_allocator/src/hash_map.rs +++ b/crates/oxc_allocator/src/hash_map.rs @@ -129,6 +129,32 @@ impl<'alloc, K, V> HashMap<'alloc, K, V> { Self(ManuallyDrop::new(inner)) } + /// Create a new [`HashMap`] whose elements are taken from an iterator and + /// allocated in the given `allocator`. + /// + /// This is behaviorally identical to [`FromIterator::from_iter`]. + #[inline] + pub fn from_iter_in>( + iter: I, + allocator: &'alloc Allocator, + ) -> Self + where + K: Eq + Hash, + { + const { Self::ASSERT_K_AND_V_ARE_NOT_DROP }; + + let iter = iter.into_iter(); + let mut map = ManuallyDrop::new(FxHashMap::with_capacity_and_hasher_in( + iter.size_hint().0, + FxBuildHasher, + allocator.bump(), + )); + iter.for_each(|(k, v)| { + map.insert(k, v); + }); + Self(map) + } + /// Creates a consuming iterator visiting all the keys in arbitrary order. /// /// The map cannot be used after calling this. The iterator element type is `K`.