Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/oxc_allocator/src/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I: IntoIterator<Item = (K, V)>>(
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`.
Expand Down
Loading