wasmer_wasm_interface/
interface_matcher.rs

1use serde::{Deserialize, Serialize};
2use std::collections::{HashMap, HashSet};
3
4use crate::interface::{Export, Import};
5
6/// A struct containing data for more efficient matching.
7///
8/// An ideal use case for this is to parse [`Interface`]s at compile time,
9/// create [`InterfaceMatcher`]s, and store them as bytes so that they
10/// can be efficiently loaded at runtime for matching.
11#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
12pub struct InterfaceMatcher {
13    pub namespaces: HashSet<String>,
14    pub namespace_imports: HashMap<String, HashSet<Import>>,
15    pub exports: HashSet<Export>,
16}
17
18#[cfg(feature = "binary_encode")]
19impl InterfaceMatcher {
20    /// Store the matcher as bytes to avoid reparsing
21    fn into_bytes(&self) -> Vec<u8> {
22        bincode::serialize(self).expect("Could not serialize InterfaceMatcher")
23    }
24
25    /// Load the matcher from bytes to avoid reparsing
26    fn from_bytes(bytes: &[u8]) -> Option<Self> {
27        bincode::deserialize(bytes).ok()
28    }
29}