A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Rename our OOM-handling `HashMap` to `TryHashMap` (#12723)
Nick Fitzgerald87bbb9077a51
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rsindex c84583906a..699da59f80 100644--- a/crates/environ/src/collections.rs+++ b/crates/environ/src/collections.rs@@ -10,7 +10,7 @@ mod secondary_map;pub use btree_map::BTreeMap;pub use entity_set::TryEntitySet;-pub use hash_map::HashMap;+pub use hash_map::TryHashMap;pub use hash_set::HashSet;pub use index_map::IndexMap;pub use primary_map::PrimaryMap;diff --git a/crates/environ/src/collections/hash_map.rs b/crates/environ/src/collections/hash_map.rsindex 53fe10efde..6f3a674ef9 100644--- a/crates/environ/src/collections/hash_map.rs+++ b/crates/environ/src/collections/hash_map.rs@@ -15,11 +15,11 @@ use wasmtime_core::alloc::TryClone;/// A wrapper type around [`hashbrown::hash_map::HashMap`] that only exposes/// fallible allocation.-pub struct HashMap<K, V, S = DefaultHashBuilder> {+pub struct TryHashMap<K, V, S = DefaultHashBuilder> {inner: inner::HashMap<K, V, S>,}-impl<K, V, S> TryClone for HashMap<K, V, S>+impl<K, V, S> TryClone for TryHashMap<K, V, S>whereK: Eq + Hash + TryClone,V: TryClone,@@ -35,7 +35,7 @@ where}}-impl<K, V, S> Default for HashMap<K, V, S>+impl<K, V, S> Default for TryHashMap<K, V, S>whereS: Default,{@@ -46,7 +46,7 @@ where}}-impl<K, V, S> PartialEq for HashMap<K, V, S>+impl<K, V, S> PartialEq for TryHashMap<K, V, S>whereK: Eq + Hash,V: PartialEq,@@ -57,7 +57,7 @@ where}}-impl<K, V, S> Eq for HashMap<K, V, S>+impl<K, V, S> Eq for TryHashMap<K, V, S>whereK: Eq + Hash,V: Eq,@@ -65,7 +65,7 @@ where{}-impl<K, V, S> fmt::Debug for HashMap<K, V, S>+impl<K, V, S> fmt::Debug for TryHashMap<K, V, S>whereK: fmt::Debug,V: fmt::Debug,@@ -75,7 +75,7 @@ where}}-impl<K, V> HashMap<K, V, DefaultHashBuilder> {+impl<K, V> TryHashMap<K, V, DefaultHashBuilder> {/// Same as [`hashbrown::hash_map::HashMap::new`].pub fn new() -> Self {Self {@@ -84,7 +84,7 @@ impl<K, V> HashMap<K, V, DefaultHashBuilder> {}}-impl<K, V> HashMap<K, V, DefaultHashBuilder>+impl<K, V> TryHashMap<K, V, DefaultHashBuilder>whereK: Eq + Hash,{@@ -97,7 +97,7 @@ where}}-impl<K, V, S> HashMap<K, V, S> {+impl<K, V, S> TryHashMap<K, V, S> {/// Same as [`hashbrown::hash_map::HashMap::with_hasher`].pub const fn with_hasher(hasher: S) -> Self {Self {@@ -162,7 +162,7 @@ impl<K, V, S> HashMap<K, V, S> {}}-impl<K, V, S> HashMap<K, V, S>+impl<K, V, S> TryHashMap<K, V, S>whereK: Eq + Hash,S: BuildHasher,@@ -223,7 +223,7 @@ where}}-impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {+impl<'a, K, V, S> IntoIterator for &'a TryHashMap<K, V, S> {type Item = (&'a K, &'a V);type IntoIter = inner::Iter<'a, K, V>;@@ -233,7 +233,7 @@ impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {}}-impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> {+impl<'a, K, V, S> IntoIterator for &'a mut TryHashMap<K, V, S> {type Item = (&'a K, &'a mut V);type IntoIter = inner::IterMut<'a, K, V>;@@ -243,7 +243,7 @@ impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> {}}-impl<K, V, S> IntoIterator for HashMap<K, V, S> {+impl<K, V, S> IntoIterator for TryHashMap<K, V, S> {type Item = (K, V);type IntoIter = inner::IntoIter<K, V>;diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rsindex 059e9dc55e..6712bffaae 100644--- a/crates/environ/src/prelude.rs+++ b/crates/environ/src/prelude.rs@@ -20,8 +20,8 @@//! and then `use crate::*` works as usual.pub use crate::collections::{- TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned,- TryVec, try_new, try_vec,+ TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap, TryNew, TryString,+ TryToOwned, TryVec, try_new, try_vec,};pub use crate::error::{Context, Error, Result, bail, ensure, format_err};pub use alloc::borrow::ToOwned;diff --git a/crates/environ/src/string_pool.rs b/crates/environ/src/string_pool.rsindex 0adc43932d..f29a90edfd 100644--- a/crates/environ/src/string_pool.rs+++ b/crates/environ/src/string_pool.rs@@ -1,6 +1,6 @@//! Simple string interning.-use crate::{collections::HashMap, error::OutOfMemory, prelude::*};+use crate::{error::OutOfMemory, prelude::*};use core::{fmt, mem, num::NonZeroU32};/// An interned string associated with a particular string in a `StringPool`.@@ -28,7 +28,7 @@ pub struct Atom {pub struct StringPool {/// A map from each string in this pool (as an unsafe borrow from/// `self.strings`) to its `Atom`.- map: mem::ManuallyDrop<HashMap<&'static str, Atom>>,+ map: mem::ManuallyDrop<TryHashMap<&'static str, Atom>>,/// Strings in this pool. These must never be mutated or reallocated once/// inserted.diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rsindex 2793c58b6c..59dd10278a 100644--- a/crates/fuzzing/tests/oom.rs+++ b/crates/fuzzing/tests/oom.rs@@ -244,26 +244,26 @@ fn hash_set_insert() -> Result<()> {}#[test]-fn hash_map_with_capacity() -> Result<()> {+fn try_hash_map_with_capacity() -> Result<()> {OomTest::new().test(|| {- let _s = HashMap::<usize, usize>::with_capacity(100)?;+ let _s = TryHashMap::<usize, usize>::with_capacity(100)?;Ok(())})}#[test]-fn hash_map_reserve() -> Result<()> {+fn try_hash_map_reserve() -> Result<()> {OomTest::new().test(|| {- let mut map = HashMap::<usize, usize>::new();+ let mut map = TryHashMap::<usize, usize>::new();map.reserve(100)?;Ok(())})}#[test]-fn hash_map_insert() -> Result<()> {+fn try_hash_map_insert() -> Result<()> {OomTest::new().test(|| {- let mut map = HashMap::<usize, usize>::new();+ let mut map = TryHashMap::<usize, usize>::new();for i in 0..1024 {map.insert(i, i * 2)?;}@@ -275,9 +275,9 @@ fn hash_map_insert() -> Result<()> {}#[test]-fn hash_map_try_clone() -> Result<()> {+fn try_hash_map_try_clone() -> Result<()> {OomTest::new().test(|| {- let mut map = HashMap::new();+ let mut map = TryHashMap::new();for i in 0..10 {map.insert(i, i * 2)?;}diff --git a/crates/wasmtime/src/runtime/linker.rs b/crates/wasmtime/src/runtime/linker.rsindex ebb98d8a78..f8984fb64f 100644--- a/crates/wasmtime/src/runtime/linker.rs+++ b/crates/wasmtime/src/runtime/linker.rs@@ -13,7 +13,7 @@ use core::future::Future;use core::marker;use core::mem::MaybeUninit;use log::warn;-use wasmtime_environ::{Atom, StringPool, collections::HashMap};+use wasmtime_environ::{Atom, StringPool};/// Structure used to link wasm modules/instances together.///@@ -84,7 +84,7 @@ use wasmtime_environ::{Atom, StringPool, collections::HashMap};pub struct Linker<T> {engine: Engine,pool: StringPool,- map: HashMap<ImportKey, Definition>,+ map: TryHashMap<ImportKey, Definition>,allow_shadowing: bool,allow_unknown_exports: bool,_marker: marker::PhantomData<fn() -> T>,@@ -160,7 +160,7 @@ impl<T> Linker<T> {pub fn new(engine: &Engine) -> Linker<T> {Linker {engine: engine.clone(),- map: HashMap::new(),+ map: TryHashMap::new(),pool: StringPool::new(),allow_shadowing: false,allow_unknown_exports: false,