holochain_types/dna/
wasm.rs

1//! crate::dna::wasm is a module for managing webassembly code
2//!  - within the in-memory dna struct
3//!  - and serialized to json
4use backtrace::Backtrace;
5use holo_hash::*;
6use holochain_serialized_bytes::prelude::*;
7use serde::Deserialize;
8use serde::Serialize;
9use std::fmt;
10use std::hash::Hash;
11use std::hash::Hasher;
12use std::sync::Arc;
13use tracing::*;
14
15/// Represents web assembly code.
16#[derive(Serialize, Deserialize, Clone, Eq)]
17pub struct DnaWasm {
18    /// the wasm bytes from a .wasm file
19    #[allow(clippy::redundant_allocation)]
20    pub code: Arc<Box<[u8]>>,
21}
22
23/// A DnaWasm paired with its WasmHash
24pub type DnaWasmHashed = HoloHashed<DnaWasm>;
25
26impl HashableContent for DnaWasm {
27    type HashType = hash_type::Wasm;
28
29    fn hash_type(&self) -> Self::HashType {
30        hash_type::Wasm
31    }
32
33    fn hashable_content(&self) -> HashableContentBytes {
34        HashableContentBytes::Content(
35            self.try_into()
36                .expect("Could not serialize HashableContent"),
37        )
38    }
39}
40
41impl TryFrom<&DnaWasm> for SerializedBytes {
42    type Error = SerializedBytesError;
43    fn try_from(dna_wasm: &DnaWasm) -> Result<Self, Self::Error> {
44        Ok(SerializedBytes::from(UnsafeBytes::from(
45            dna_wasm.code.to_vec(),
46        )))
47    }
48}
49impl TryFrom<DnaWasm> for SerializedBytes {
50    type Error = SerializedBytesError;
51    fn try_from(dna_wasm: DnaWasm) -> Result<Self, Self::Error> {
52        Self::try_from(&dna_wasm)
53    }
54}
55
56impl TryFrom<SerializedBytes> for DnaWasm {
57    type Error = SerializedBytesError;
58    fn try_from(serialized_bytes: SerializedBytes) -> Result<Self, Self::Error> {
59        Ok(DnaWasm {
60            code: Arc::new(serialized_bytes.bytes().to_owned().into_boxed_slice()),
61        })
62    }
63}
64
65impl DnaWasm {
66    /// Provide basic placeholder for wasm entries in dna structs, used for testing only.
67    pub fn new_invalid() -> Self {
68        debug!(
69            "DnaWasm::new_invalid() called from:\n{:?}",
70            Backtrace::new()
71        );
72        DnaWasm {
73            code: Arc::new(Box::new([])),
74        }
75    }
76
77    /// get a new Arc to the `Vec<u8>` bytes for the wasm
78    #[allow(clippy::redundant_allocation)]
79    pub fn code(&self) -> Arc<Box<[u8]>> {
80        Arc::clone(&self.code)
81    }
82}
83
84impl fmt::Debug for DnaWasm {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        write!(f, "<<<DNA WASM CODE>>>")
87    }
88}
89
90impl PartialEq for DnaWasm {
91    fn eq(&self, other: &DnaWasm) -> bool {
92        self.code == other.code
93    }
94}
95
96impl Hash for DnaWasm {
97    fn hash<H: Hasher>(&self, state: &mut H) {
98        self.code.hash(state);
99    }
100}
101
102impl From<Vec<u8>> for DnaWasm {
103    fn from(wasm: Vec<u8>) -> Self {
104        Self {
105            code: Arc::new(wasm.into_boxed_slice()),
106        }
107    }
108}