wasmer_compiler/types/
section.rs1#![allow(missing_docs)]
6
7use super::relocation::{ArchivedRelocation, Relocation, RelocationLike};
15use crate::lib::std::vec::Vec;
16use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
17#[cfg(feature = "enable-serde")]
18use serde::{Deserialize, Serialize};
19use wasmer_types::entity_impl;
20
21#[derive(
23 RkyvSerialize,
24 RkyvDeserialize,
25 Archive,
26 Copy,
27 Clone,
28 PartialEq,
29 Eq,
30 Hash,
31 PartialOrd,
32 Ord,
33 Debug,
34 Default,
35)]
36#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
37#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
38#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
39pub struct SectionIndex(u32);
40
41entity_impl!(SectionIndex);
42
43#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
47#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
48#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
49#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
50#[repr(u8)]
51pub enum CustomSectionProtection {
52 Read,
54
55 ReadExecute,
57}
58
59#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
64#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
65#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
66#[rkyv(derive(Debug), compare(PartialEq))]
67pub struct CustomSection {
68 pub protection: CustomSectionProtection,
70
71 pub bytes: SectionBody,
78
79 pub relocations: Vec<Relocation>,
81}
82
83#[allow(missing_docs)]
85pub trait CustomSectionLike<'a> {
86 type Relocations: RelocationLike;
87
88 fn protection(&self) -> CustomSectionProtection;
89 fn bytes(&self) -> &[u8];
90 fn relocations(&'a self) -> &[Self::Relocations];
91}
92
93impl<'a> CustomSectionLike<'a> for CustomSection {
94 type Relocations = Relocation;
95
96 fn protection(&self) -> CustomSectionProtection {
97 self.protection.clone()
98 }
99
100 fn bytes(&self) -> &[u8] {
101 self.bytes.0.as_ref()
102 }
103
104 fn relocations(&'a self) -> &[Self::Relocations] {
105 self.relocations.as_slice()
106 }
107}
108
109impl<'a> CustomSectionLike<'a> for ArchivedCustomSection {
110 type Relocations = ArchivedRelocation;
111
112 fn protection(&self) -> CustomSectionProtection {
113 let protection = rkyv::deserialize::<CustomSectionProtection, ()>(&self.protection);
114 protection.unwrap()
115 }
116
117 fn bytes(&self) -> &[u8] {
118 self.bytes.0.as_ref()
119 }
120
121 fn relocations(&'a self) -> &[Self::Relocations] {
122 self.relocations.as_slice()
123 }
124}
125
126#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
128#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
129#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
130#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
131pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>);
132
133impl SectionBody {
134 pub fn new_with_vec(contents: Vec<u8>) -> Self {
136 Self(contents)
137 }
138
139 pub fn as_ptr(&self) -> *const u8 {
141 self.0.as_ptr()
142 }
143
144 pub fn len(&self) -> usize {
146 self.0.len()
147 }
148
149 pub fn as_slice(&self) -> &[u8] {
151 self.0.as_slice()
152 }
153
154 pub fn is_empty(&self) -> bool {
156 self.0.is_empty()
157 }
158}
159
160impl ArchivedSectionBody {
161 pub fn len(&self) -> usize {
163 self.0.len()
164 }
165
166 pub fn is_empty(&self) -> bool {
168 self.0.is_empty()
169 }
170}