use super::relocation::{ArchivedRelocation, Relocation, RelocationLike};
use crate::entity::entity_impl;
use crate::lib::std::vec::Vec;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
#[derive(
RkyvSerialize,
RkyvDeserialize,
Archive,
rkyv::CheckBytes,
Copy,
Clone,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Debug,
Default,
)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[archive(as = "Self")]
pub struct SectionIndex(u32);
entity_impl!(SectionIndex);
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(
RkyvSerialize, RkyvDeserialize, Archive, rkyv::CheckBytes, Debug, Clone, PartialEq, Eq,
)]
#[archive(as = "Self")]
#[repr(u8)]
pub enum CustomSectionProtection {
Read,
ReadExecute,
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
#[archive_attr(derive(rkyv::CheckBytes, Debug))]
pub struct CustomSection {
pub protection: CustomSectionProtection,
pub bytes: SectionBody,
pub relocations: Vec<Relocation>,
}
#[allow(missing_docs)]
pub trait CustomSectionLike<'a> {
type Relocations: RelocationLike;
fn protection(&self) -> &CustomSectionProtection;
fn bytes(&self) -> &[u8];
fn relocations(&'a self) -> &[Self::Relocations];
}
impl<'a> CustomSectionLike<'a> for CustomSection {
type Relocations = Relocation;
fn protection(&self) -> &CustomSectionProtection {
&self.protection
}
fn bytes(&self) -> &[u8] {
self.bytes.0.as_ref()
}
fn relocations(&'a self) -> &[Self::Relocations] {
self.relocations.as_slice()
}
}
impl<'a> CustomSectionLike<'a> for ArchivedCustomSection {
type Relocations = ArchivedRelocation;
fn protection(&self) -> &CustomSectionProtection {
&self.protection
}
fn bytes(&self) -> &[u8] {
self.bytes.0.as_ref()
}
fn relocations(&'a self) -> &[Self::Relocations] {
self.relocations.as_slice()
}
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
#[archive_attr(derive(rkyv::CheckBytes, Debug))]
pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>);
impl SectionBody {
pub fn new_with_vec(contents: Vec<u8>) -> Self {
Self(contents)
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl ArchivedSectionBody {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}