Skip to content

Commit 1767176

Browse files
committed
feat(allocator): add HashMap::from_iter_in
1 parent e2ba8ad commit 1767176

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

crates/oxc_allocator/src/hash_map.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,32 @@ impl<'alloc, K, V> HashMap<'alloc, K, V> {
129129
Self(ManuallyDrop::new(inner))
130130
}
131131

132+
/// Create a new [`HashMap`] whose elements are taken from an iterator and
133+
/// allocated in the given `allocator`.
134+
///
135+
/// This is behaviorially identical to [`FromIterator::from_iter`].
136+
#[inline]
137+
pub fn from_iter_in<I: IntoIterator<Item = (K, V)>>(
138+
iter: I,
139+
allocator: &'alloc Allocator,
140+
) -> Self
141+
where
142+
K: Eq + Hash,
143+
{
144+
const { Self::ASSERT_K_AND_V_ARE_NOT_DROP };
145+
146+
let iter = iter.into_iter();
147+
let mut map = ManuallyDrop::new(FxHashMap::with_capacity_and_hasher_in(
148+
iter.size_hint().0,
149+
FxBuildHasher,
150+
allocator.bump(),
151+
));
152+
iter.for_each(|(k, v)| {
153+
map.insert(k, v);
154+
});
155+
Self(map)
156+
}
157+
132158
/// Creates a consuming iterator visiting all the keys in arbitrary order.
133159
///
134160
/// The map cannot be used after calling this. The iterator element type is `K`.

0 commit comments

Comments
 (0)