#![allow(missing_docs)]
use crate::indexes::{FunctionIndex, MemoryIndex, TableIndex};
use crate::lib::std::boxed::Box;
use crate::types::InitExpr;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(Clone, Debug, Hash, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
#[rkyv(derive(Debug))]
pub struct TableInitializer {
pub table_index: TableIndex,
pub offset_expr: InitExpr,
pub elements: Box<[FunctionIndex]>,
}
#[derive(Clone, Debug, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[rkyv(derive(Debug))]
pub struct DataInitializerLocation {
pub memory_index: MemoryIndex,
pub offset_expr: InitExpr,
}
#[allow(missing_docs)]
pub trait DataInitializerLocationLike {
fn memory_index(&self) -> MemoryIndex;
fn offset_expr(&self) -> InitExpr;
}
impl DataInitializerLocationLike for &DataInitializerLocation {
fn memory_index(&self) -> MemoryIndex {
self.memory_index
}
fn offset_expr(&self) -> InitExpr {
self.offset_expr.clone()
}
}
impl DataInitializerLocationLike for &ArchivedDataInitializerLocation {
fn memory_index(&self) -> MemoryIndex {
MemoryIndex::from_u32(rkyv::deserialize::<_, ()>(&self.memory_index).unwrap().0)
}
fn offset_expr(&self) -> InitExpr {
rkyv::deserialize::<_, rkyv::rancor::Error>(&self.offset_expr).unwrap()
}
}
#[derive(Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct DataInitializer<'data> {
pub location: DataInitializerLocation,
pub data: &'data [u8],
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(Debug, Clone, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
#[rkyv(derive(Debug))]
pub struct OwnedDataInitializer {
pub location: DataInitializerLocation,
pub data: Box<[u8]>,
}
#[allow(missing_docs)]
pub trait DataInitializerLike<'a> {
type Location: DataInitializerLocationLike + Copy + 'a;
fn location(&self) -> Self::Location;
fn data(&self) -> &'a [u8];
}
impl OwnedDataInitializer {
pub fn new(borrowed: &DataInitializer<'_>) -> Self {
Self {
location: borrowed.location.clone(),
data: borrowed.data.to_vec().into_boxed_slice(),
}
}
}
impl<'a> DataInitializerLike<'a> for &'a OwnedDataInitializer {
type Location = &'a DataInitializerLocation;
fn location(&self) -> Self::Location {
&self.location
}
fn data(&self) -> &'a [u8] {
self.data.as_ref()
}
}
impl<'a> DataInitializerLike<'a> for &'a ArchivedOwnedDataInitializer {
type Location = &'a ArchivedDataInitializerLocation;
fn location(&self) -> Self::Location {
&self.location
}
fn data(&self) -> &'a [u8] {
self.data.as_ref()
}
}