wasmer_types/compilation/
section.rs1use super::relocation::{ArchivedRelocation, Relocation, RelocationLike};
9use crate::entity::entity_impl;
10use crate::lib::std::vec::Vec;
11use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
12#[cfg(feature = "enable-serde")]
13use serde::{Deserialize, Serialize};
14
15#[derive(
17 RkyvSerialize,
18 RkyvDeserialize,
19 Archive,
20 rkyv::CheckBytes,
21 Copy,
22 Clone,
23 PartialEq,
24 Eq,
25 Hash,
26 PartialOrd,
27 Ord,
28 Debug,
29 Default,
30)]
31#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
32#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
33#[archive(as = "Self")]
34pub struct SectionIndex(u32);
35
36entity_impl!(SectionIndex);
37
38#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
42#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
43#[derive(
44 RkyvSerialize, RkyvDeserialize, Archive, rkyv::CheckBytes, Debug, Clone, PartialEq, Eq,
45)]
46#[archive(as = "Self")]
47#[repr(u8)]
48pub enum CustomSectionProtection {
49 Read,
51
52 ReadExecute,
54}
55
56#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
61#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
62#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
63#[archive_attr(derive(rkyv::CheckBytes, Debug))]
64pub struct CustomSection {
65 pub protection: CustomSectionProtection,
67
68 pub bytes: SectionBody,
75
76 pub relocations: Vec<Relocation>,
78}
79
80#[allow(missing_docs)]
82pub trait CustomSectionLike<'a> {
83 type Relocations: RelocationLike;
84
85 fn protection(&self) -> &CustomSectionProtection;
86 fn bytes(&self) -> &[u8];
87 fn relocations(&'a self) -> &[Self::Relocations];
88}
89
90impl<'a> CustomSectionLike<'a> for CustomSection {
91 type Relocations = Relocation;
92
93 fn protection(&self) -> &CustomSectionProtection {
94 &self.protection
95 }
96
97 fn bytes(&self) -> &[u8] {
98 self.bytes.0.as_ref()
99 }
100
101 fn relocations(&'a self) -> &[Self::Relocations] {
102 self.relocations.as_slice()
103 }
104}
105
106impl<'a> CustomSectionLike<'a> for ArchivedCustomSection {
107 type Relocations = ArchivedRelocation;
108
109 fn protection(&self) -> &CustomSectionProtection {
110 &self.protection
111 }
112
113 fn bytes(&self) -> &[u8] {
114 self.bytes.0.as_ref()
115 }
116
117 fn relocations(&'a self) -> &[Self::Relocations] {
118 self.relocations.as_slice()
119 }
120}
121
122#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
124#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
125#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
126#[archive_attr(derive(rkyv::CheckBytes, Debug))]
127pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>);
128
129impl SectionBody {
130 pub fn new_with_vec(contents: Vec<u8>) -> Self {
132 Self(contents)
133 }
134
135 pub fn as_ptr(&self) -> *const u8 {
137 self.0.as_ptr()
138 }
139
140 pub fn len(&self) -> usize {
142 self.0.len()
143 }
144
145 pub fn as_slice(&self) -> &[u8] {
147 self.0.as_slice()
148 }
149
150 pub fn is_empty(&self) -> bool {
152 self.0.is_empty()
153 }
154}
155
156impl ArchivedSectionBody {
157 pub fn len(&self) -> usize {
159 self.0.len()
160 }
161
162 pub fn is_empty(&self) -> bool {
164 self.0.is_empty()
165 }
166}