diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs index 543ef582c522..5a3e9a838420 100644 --- a/crates/environ/src/collections.rs +++ b/crates/environ/src/collections.rs @@ -12,7 +12,7 @@ pub use btree_map::BTreeMap; pub use entity_set::TryEntitySet; pub use hash_map::TryHashMap; pub use hash_set::TryHashSet; -pub use index_map::IndexMap; +pub use index_map::TryIndexMap; pub use primary_map::PrimaryMap; pub use secondary_map::SecondaryMap; pub use wasmtime_core::{ diff --git a/crates/environ/src/collections/index_map.rs b/crates/environ/src/collections/index_map.rs index efb9654bfc84..55629f871d2a 100644 --- a/crates/environ/src/collections/index_map.rs +++ b/crates/environ/src/collections/index_map.rs @@ -18,11 +18,11 @@ use hashbrown::DefaultHashBuilder; /// A wrapper around [`indexmap::IndexMap`] that only provides fallible /// allocation. -pub struct IndexMap { +pub struct TryIndexMap { inner: indexmap::IndexMap, } -impl TryClone for IndexMap +impl TryClone for TryIndexMap where K: Eq + Hash + TryClone, V: TryClone, @@ -37,7 +37,7 @@ where } } -impl fmt::Debug for IndexMap +impl fmt::Debug for TryIndexMap where K: fmt::Debug, V: fmt::Debug, @@ -48,19 +48,19 @@ where } } -impl From> for indexmap::IndexMap { - fn from(map: IndexMap) -> Self { +impl From> for indexmap::IndexMap { + fn from(map: TryIndexMap) -> Self { map.inner } } -impl From> for IndexMap { +impl From> for TryIndexMap { fn from(inner: indexmap::IndexMap) -> Self { Self { inner } } } -impl serde::ser::Serialize for IndexMap +impl serde::ser::Serialize for TryIndexMap where K: serde::ser::Serialize, V: serde::ser::Serialize, @@ -78,7 +78,7 @@ where } } -impl<'de, K, V> serde::de::Deserialize<'de> for IndexMap +impl<'de, K, V> serde::de::Deserialize<'de> for TryIndexMap where K: serde::de::Deserialize<'de> + Hash + Eq, V: serde::de::Deserialize<'de>, @@ -87,14 +87,14 @@ where where D: serde::Deserializer<'de>, { - struct Visitor(PhantomData IndexMap>); + struct Visitor(PhantomData TryIndexMap>); impl<'de, K, V> serde::de::Visitor<'de> for Visitor where K: serde::de::Deserialize<'de> + Hash + Eq, V: serde::de::Deserialize<'de>, { - type Value = IndexMap; + type Value = TryIndexMap; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("an `IndexMap`") @@ -106,7 +106,7 @@ where { use serde::de::Error as _; - let mut result = IndexMap::::new(); + let mut result = TryIndexMap::::new(); if let Some(len) = map.size_hint() { result.reserve(len).map_err(|oom| A::Error::custom(oom))?; @@ -124,7 +124,7 @@ where } } -impl IndexMap { +impl TryIndexMap { /// Same as [`indexmap::IndexMap::new`]. pub fn new() -> Self { Self { @@ -139,7 +139,7 @@ impl IndexMap { } } -impl IndexMap { +impl TryIndexMap { /// Same as [`indexmap::IndexMap::with_capacity_and_hasher`] but returns an /// error on allocation failure. pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Result { @@ -150,7 +150,7 @@ impl IndexMap { /// Same as [`indexmap::IndexMap::with_hasher`]. pub const fn with_hasher(hash_builder: S) -> Self { - IndexMap { + TryIndexMap { inner: indexmap::IndexMap::with_hasher(hash_builder), } } @@ -279,7 +279,7 @@ impl IndexMap { } } -impl IndexMap +impl TryIndexMap where K: Hash + Eq, S: BuildHasher, @@ -369,7 +369,7 @@ where } } -impl IndexMap +impl TryIndexMap where S: BuildHasher, { @@ -509,7 +509,7 @@ where } } -impl IndexMap { +impl TryIndexMap { /// Same as [`indexmap::IndexMap::pop`]. pub fn pop(&mut self) -> Option<(K, V)> { self.inner.pop() @@ -712,7 +712,7 @@ impl IndexMap { } } -impl Index<&Q> for IndexMap +impl Index<&Q> for TryIndexMap where Q: ?Sized + Hash + Eq, K: Borrow, @@ -725,7 +725,7 @@ where } } -impl IndexMut<&Q> for IndexMap +impl IndexMut<&Q> for TryIndexMap where Q: ?Sized + Hash + Eq, K: Borrow, @@ -736,7 +736,7 @@ where } } -impl Index for IndexMap { +impl Index for TryIndexMap { type Output = V; fn index(&self, index: usize) -> &V { @@ -744,13 +744,13 @@ impl Index for IndexMap { } } -impl IndexMut for IndexMap { +impl IndexMut for TryIndexMap { fn index_mut(&mut self, index: usize) -> &mut V { &mut self.inner[index] } } -impl Default for IndexMap +impl Default for TryIndexMap where S: Default, { @@ -759,19 +759,19 @@ where } } -impl PartialEq> for IndexMap +impl PartialEq> for TryIndexMap where K: Hash + Eq, V1: PartialEq, S1: BuildHasher, S2: BuildHasher, { - fn eq(&self, other: &IndexMap) -> bool { + fn eq(&self, other: &TryIndexMap) -> bool { &self.inner == &other.inner } } -impl Eq for IndexMap +impl Eq for TryIndexMap where K: Eq + Hash, V: Eq, @@ -779,7 +779,7 @@ where { } -impl<'a, K, V, S> IntoIterator for &'a IndexMap { +impl<'a, K, V, S> IntoIterator for &'a TryIndexMap { type Item = (&'a K, &'a V); type IntoIter = inner::Iter<'a, K, V>; @@ -788,7 +788,7 @@ impl<'a, K, V, S> IntoIterator for &'a IndexMap { } } -impl<'a, K, V, S> IntoIterator for &'a mut IndexMap { +impl<'a, K, V, S> IntoIterator for &'a mut TryIndexMap { type Item = (&'a K, &'a mut V); type IntoIter = inner::IterMut<'a, K, V>; @@ -797,7 +797,7 @@ impl<'a, K, V, S> IntoIterator for &'a mut IndexMap { } } -impl IntoIterator for IndexMap { +impl IntoIterator for TryIndexMap { type Item = (K, V); type IntoIter = inner::IntoIter; @@ -813,7 +813,7 @@ mod tests { #[test] fn smoke() -> Result<()> { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert("a", 10)?; map.insert("b", 20)?; diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index d277fc57b821..f983aa304d01 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -304,7 +304,7 @@ pub struct Module { pub initializers: TryVec, /// Exported entities. - pub exports: collections::IndexMap, + pub exports: TryIndexMap, /// The module "start" function, if present. pub start_func: Option, diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs index 23e7228dbebd..8c5a3bfb2339 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, TryHashMap, TryHashSet, TryNew, - TryString, TryToOwned, TryVec, try_new, try_vec, + TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap, TryHashSet, + TryIndexMap, 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/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rs index 300788690747..af3bb117900b 100644 --- a/crates/fuzzing/tests/oom.rs +++ b/crates/fuzzing/tests/oom.rs @@ -863,13 +863,13 @@ fn try_vec_macro_elem_len() -> Result<()> { } #[test] -fn index_map_try_clone() -> Result<()> { +fn try_index_map_try_clone() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map1 = IndexMap::new(); + let mut map1 = TryIndexMap::new(); map1.insert("a", try_new::>(42)?)?; map1.insert("b", try_new::>(36)?)?; let map2 = map1.try_clone()?; @@ -879,25 +879,25 @@ fn index_map_try_clone() -> Result<()> { } #[test] -fn index_map_with_capacity() -> Result<()> { +fn try_index_map_with_capacity() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let _map = IndexMap::<&str, usize>::with_capacity(100)?; + let _map = TryIndexMap::<&str, usize>::with_capacity(100)?; Ok(()) }) } #[test] -fn index_map_split_off() -> Result<()> { +fn try_index_map_split_off() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map1 = IndexMap::new(); + let mut map1 = TryIndexMap::new(); map1.insert("a", 42)?; map1.insert("b", 36)?; @@ -915,100 +915,100 @@ fn index_map_split_off() -> Result<()> { } #[test] -fn index_map_reserve() -> Result<()> { +fn try_index_map_reserve() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::::new(); + let mut map = TryIndexMap::::new(); map.reserve(100)?; Ok(()) }) } #[test] -fn index_map_reserve_exact() -> Result<()> { +fn try_index_map_reserve_exact() -> Result<()> { OomTest::new().test(|| { - let mut map = IndexMap::::new(); + let mut map = TryIndexMap::::new(); map.reserve_exact(100)?; Ok(()) }) } #[test] -fn index_map_insert() -> Result<()> { +fn try_index_map_insert() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert(10, 20)?; Ok(()) }) } #[test] -fn index_map_insert_full() -> Result<()> { +fn try_index_map_insert_full() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert_full(10, 20)?; Ok(()) }) } #[test] -fn index_map_insert_sorted() -> Result<()> { +fn try_index_map_insert_sorted() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert_sorted(10, 20)?; Ok(()) }) } #[test] -fn index_map_insert_sorted_by() -> Result<()> { +fn try_index_map_insert_sorted_by() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert_sorted_by(10, 20, |_k, _v, _k2, _v2| core::cmp::Ordering::Less)?; Ok(()) }) } #[test] -fn index_map_insert_sorted_by_key() -> Result<()> { +fn try_index_map_insert_sorted_by_key() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert_sorted_by_key(10, 20, |_k, v| *v)?; Ok(()) }) } #[test] -fn index_map_insert_before() -> Result<()> { +fn try_index_map_insert_before() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert("a", 20)?; map.insert("b", 30)?; map.insert_before(1, "c", 40)?; @@ -1020,13 +1020,13 @@ fn index_map_insert_before() -> Result<()> { } #[test] -fn index_map_shift_insert() -> Result<()> { +fn try_index_map_shift_insert() -> Result<()> { OomTest::new() // `indexmap` will first try to double its capacity, and, if that fails, // will then try to allocate only as much as it absolutely must. .allow_alloc_after_oom(true) .test(|| { - let mut map = IndexMap::new(); + let mut map = TryIndexMap::new(); map.insert("a", 20)?; map.insert("b", 30)?; map.shift_insert(1, "c", 40)?;