wasmtime_environ/
module_types.rs1use crate::{PrimaryMap, SignatureIndex, WasmFuncType};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::ops::Index;
5
6#[derive(Default, Serialize, Deserialize)]
14#[allow(missing_docs)]
15pub struct ModuleTypes {
16 wasm_signatures: PrimaryMap<SignatureIndex, WasmFuncType>,
17}
18
19impl ModuleTypes {
20 pub fn wasm_signatures(&self) -> impl Iterator<Item = (SignatureIndex, &WasmFuncType)> {
23 self.wasm_signatures.iter()
24 }
25}
26
27impl Index<SignatureIndex> for ModuleTypes {
28 type Output = WasmFuncType;
29
30 fn index(&self, sig: SignatureIndex) -> &WasmFuncType {
31 &self.wasm_signatures[sig]
32 }
33}
34
35#[derive(Default)]
37#[allow(missing_docs)]
38pub struct ModuleTypesBuilder {
39 types: ModuleTypes,
40 interned_func_types: HashMap<WasmFuncType, SignatureIndex>,
41}
42
43impl ModuleTypesBuilder {
44 pub fn reserve_wasm_signatures(&mut self, amt: usize) {
46 self.types.wasm_signatures.reserve(amt);
47 }
48
49 pub fn wasm_func_type(&mut self, sig: WasmFuncType) -> SignatureIndex {
53 if let Some(idx) = self.interned_func_types.get(&sig) {
54 return *idx;
55 }
56
57 let idx = self.types.wasm_signatures.push(sig.clone());
58 self.interned_func_types.insert(sig, idx);
59 return idx;
60 }
61
62 pub fn finish(self) -> ModuleTypes {
64 self.types
65 }
66}
67
68impl<T> Index<T> for ModuleTypesBuilder
70where
71 ModuleTypes: Index<T>,
72{
73 type Output = <ModuleTypes as Index<T>>::Output;
74
75 fn index(&self, sig: T) -> &Self::Output {
76 &self.types[sig]
77 }
78}