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