1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! The cache module provides the common data structures used by compiler backends to allow
//! serializing compiled wasm code to a binary format.  The binary format can be persisted,
//! and loaded to allow skipping compilation and fast startup.

use crate::hash::Hash;
use std::error::Error;
use wasmer::{Module, Store};

/// A generic cache for storing and loading compiled wasm modules.
pub trait Cache {
    /// The serialization error for the implementation
    type SerializeError: Error + Send + Sync;
    /// The deserialization error for the implementation
    type DeserializeError: Error + Send + Sync;

    /// Loads a module using the provided [`Store`] and [`Hash`].
    ///
    /// # Safety
    /// This function is unsafe as the cache store could be tampered with.
    unsafe fn load(&self, store: &Store, key: Hash) -> Result<Module, Self::DeserializeError>;

    /// Store a [`Module`] into the cache with the given [`Hash`].
    fn store(&mut self, key: Hash, module: &Module) -> Result<(), Self::SerializeError>;
}