wasmer_wasix/runtime/module_cache/hashed_module.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
use shared_buffer::OwnedBuffer;
use wasmer_types::ModuleHash;
use crate::bin_factory::BinaryPackageCommand;
/// A wrapper around Webassembly code and its hash.
///
/// Allows passing around WASM code and it's hash without the danger of
/// using a wrong hash.
///
/// Safe by construction: can only be created from a [`BinaryCommand`], which
/// already has the hash embedded, or from bytes that will be hashed in the
/// constructor.
///
/// Can be cloned cheaply.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HashedModuleData {
hash: ModuleHash,
wasm: OwnedBuffer,
}
impl HashedModuleData {
pub fn new(bytes: impl Into<OwnedBuffer>) -> Self {
Self::new_sha256(bytes)
}
pub fn new_sha256(bytes: impl Into<OwnedBuffer>) -> Self {
let wasm = bytes.into();
let hash = ModuleHash::sha256(&wasm);
Self { hash, wasm }
}
/// Create new [`HashedModuleData`] from the given bytes, hashing the
/// the bytes into a [`ModuleHash`] with xxhash.
pub fn new_xxhash(bytes: impl Into<OwnedBuffer>) -> Self {
let wasm = bytes.into();
let hash = ModuleHash::xxhash(&wasm);
Self { hash, wasm }
}
/// Create new [`HashedModuleData`] from the given [`BinaryPackageCommand`].
///
/// This is very cheap, as the hash is already available in the command.
pub fn from_command(command: &BinaryPackageCommand) -> Self {
Self {
hash: *command.hash(),
wasm: command.atom(),
}
}
/// Get the module hash.
pub fn hash(&self) -> &ModuleHash {
&self.hash
}
/// Get the WASM code.
pub fn wasm(&self) -> &OwnedBuffer {
&self.wasm
}
pub fn into_parts(self) -> (ModuleHash, OwnedBuffer) {
(self.hash, self.wasm)
}
}