Skip to content

Commit 102a393

Browse files
authored
Implement TryExtend<V> for OOM-handling PrimaryMap<K, V> (#12610)
1 parent 55aa6a5 commit 102a393

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

crates/environ/src/collections.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use index_map::IndexMap;
1414
pub use primary_map::PrimaryMap;
1515
pub use secondary_map::SecondaryMap;
1616
pub use wasmtime_core::{
17-
alloc::{String, TryClone, TryNew, Vec, try_new},
17+
alloc::{String, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, Vec, try_new},
1818
vec,
1919
};
2020

crates/environ/src/collections/primary_map.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::{collections::TryExtend, error::OutOfMemory};
12
use core::{
23
fmt,
34
ops::{Index, IndexMut},
45
};
56
use cranelift_entity::EntityRef;
67
use serde::{Serialize, ser::SerializeSeq};
7-
use wasmtime_core::error::OutOfMemory;
88

99
/// Like [`cranelift_entity::PrimaryMap`] but enforces fallible allocation for
1010
/// all methods that allocate.
@@ -217,6 +217,25 @@ where
217217
}
218218
}
219219

220+
impl<K, V> TryExtend<V> for PrimaryMap<K, V>
221+
where
222+
K: EntityRef,
223+
{
224+
fn try_extend<I>(&mut self, iter: I) -> Result<(), OutOfMemory>
225+
where
226+
I: IntoIterator<Item = V>,
227+
{
228+
let iter = iter.into_iter();
229+
let (min, max) = iter.size_hint();
230+
let cap = max.unwrap_or(min);
231+
self.reserve(cap)?;
232+
for v in iter {
233+
self.push(v)?;
234+
}
235+
Ok(())
236+
}
237+
}
238+
220239
impl<K, V> Index<K> for PrimaryMap<K, V>
221240
where
222241
K: EntityRef,

0 commit comments

Comments
 (0)