near_vm_compiler/
section.rs1use crate::Relocation;
9use crate::lib::std::vec::Vec;
10use near_vm_types::entity::entity_impl;
11
12#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
14#[rkyv(derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug))]
15#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
16pub struct SectionIndex(u32);
17entity_impl!(SectionIndex);
18entity_impl!(ArchivedSectionIndex);
19
20#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Copy, Clone, PartialEq, Eq)]
24pub enum CustomSectionProtection {
25 Read,
27
28 ReadExecute,
30}
31
32#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
37pub struct CustomSection {
38 pub protection: CustomSectionProtection,
40
41 pub bytes: SectionBody,
48
49 pub relocations: Vec<Relocation>,
51}
52
53#[derive(Clone, Copy)]
57pub struct CustomSectionRef<'a> {
58 pub protection: CustomSectionProtection,
60
61 pub bytes: &'a [u8],
63}
64
65impl<'a> From<&'a CustomSection> for CustomSectionRef<'a> {
66 fn from(section: &'a CustomSection) -> Self {
67 CustomSectionRef { protection: section.protection, bytes: section.bytes.as_slice() }
68 }
69}
70
71impl<'a> From<&'a ArchivedCustomSection> for CustomSectionRef<'a> {
72 fn from(section: &'a ArchivedCustomSection) -> Self {
73 CustomSectionRef {
74 protection: match section.protection {
75 ArchivedCustomSectionProtection::Read => CustomSectionProtection::Read,
76 ArchivedCustomSectionProtection::ReadExecute => {
77 CustomSectionProtection::ReadExecute
78 }
79 },
80 bytes: §ion.bytes.0[..],
81 }
82 }
83}
84
85#[derive(
87 rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq, Default,
88)]
89pub struct SectionBody(Vec<u8>);
90
91impl SectionBody {
92 pub fn new_with_vec(contents: Vec<u8>) -> Self {
94 Self(contents)
95 }
96
97 pub fn as_ptr(&self) -> *const u8 {
99 self.0.as_ptr()
100 }
101
102 pub fn len(&self) -> usize {
104 self.0.len()
105 }
106
107 pub fn as_slice(&self) -> &[u8] {
109 self.0.as_slice()
110 }
111
112 pub fn is_empty(&self) -> bool {
114 self.0.is_empty()
115 }
116}