wasmer_types/
initializers.rs1#![allow(missing_docs)]
6
7use crate::indexes::{FunctionIndex, MemoryIndex, TableIndex};
8use crate::lib::std::boxed::Box;
9use crate::types::InitExpr;
10
11use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
12#[cfg(feature = "enable-serde")]
13use serde::{Deserialize, Serialize};
14
15#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
18#[derive(Clone, Debug, Hash, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
19#[rkyv(derive(Debug))]
20pub struct TableInitializer {
21 pub table_index: TableIndex,
23 pub offset_expr: InitExpr,
25 pub elements: Box<[FunctionIndex]>,
27}
28
29#[derive(Clone, Debug, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
32#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
33#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
34#[rkyv(derive(Debug))]
35pub struct DataInitializerLocation {
36 pub memory_index: MemoryIndex,
38
39 pub offset_expr: InitExpr,
41}
42
43#[allow(missing_docs)]
45pub trait DataInitializerLocationLike {
46 fn memory_index(&self) -> MemoryIndex;
47 fn offset_expr(&self) -> InitExpr;
48}
49
50impl DataInitializerLocationLike for &DataInitializerLocation {
51 fn memory_index(&self) -> MemoryIndex {
52 self.memory_index
53 }
54
55 fn offset_expr(&self) -> InitExpr {
56 self.offset_expr.clone()
57 }
58}
59
60impl DataInitializerLocationLike for &ArchivedDataInitializerLocation {
61 fn memory_index(&self) -> MemoryIndex {
62 MemoryIndex::from_u32(rkyv::deserialize::<_, ()>(&self.memory_index).unwrap().0)
63 }
64
65 fn offset_expr(&self) -> InitExpr {
66 rkyv::deserialize::<_, rkyv::rancor::Error>(&self.offset_expr).unwrap()
67 }
68}
69
70#[derive(Debug)]
72#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
73pub struct DataInitializer<'data> {
74 pub location: DataInitializerLocation,
76
77 pub data: &'data [u8],
79}
80
81#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
84#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
85#[derive(Debug, Clone, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
86#[rkyv(derive(Debug))]
87pub struct OwnedDataInitializer {
88 pub location: DataInitializerLocation,
90
91 pub data: Box<[u8]>,
93}
94
95#[allow(missing_docs)]
97pub trait DataInitializerLike<'a> {
98 type Location: DataInitializerLocationLike + Copy + 'a;
99
100 fn location(&self) -> Self::Location;
101 fn data(&self) -> &'a [u8];
102}
103
104impl OwnedDataInitializer {
105 pub fn new(borrowed: &DataInitializer<'_>) -> Self {
107 Self {
108 location: borrowed.location.clone(),
109 data: borrowed.data.to_vec().into_boxed_slice(),
110 }
111 }
112}
113
114impl<'a> DataInitializerLike<'a> for &'a OwnedDataInitializer {
115 type Location = &'a DataInitializerLocation;
116
117 fn location(&self) -> Self::Location {
118 &self.location
119 }
120
121 fn data(&self) -> &'a [u8] {
122 self.data.as_ref()
123 }
124}
125
126impl<'a> DataInitializerLike<'a> for &'a ArchivedOwnedDataInitializer {
127 type Location = &'a ArchivedDataInitializerLocation;
128
129 fn location(&self) -> Self::Location {
130 &self.location
131 }
132
133 fn data(&self) -> &'a [u8] {
134 self.data.as_ref()
135 }
136}