use crate::{
collections::arena::ArenaIndex,
module::{self, PassiveDataSegmentBytes},
store::Stored,
AsContextMut,
};
use core::convert::AsRef;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct DataSegmentIdx(u32);
impl ArenaIndex for DataSegmentIdx {
fn into_usize(self) -> usize {
self.0 as usize
}
fn from_usize(value: usize) -> Self {
let value = value.try_into().unwrap_or_else(|error| {
panic!("index {value} is out of bounds as data segment index: {error}")
});
Self(value)
}
}
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct DataSegment(Stored<DataSegmentIdx>);
impl DataSegment {
pub fn from_inner(stored: Stored<DataSegmentIdx>) -> Self {
Self(stored)
}
pub fn as_inner(&self) -> &Stored<DataSegmentIdx> {
&self.0
}
pub fn new_active(mut ctx: impl AsContextMut) -> Self {
ctx.as_context_mut()
.store
.inner
.alloc_data_segment(DataSegmentEntity::active())
}
pub fn new_passive(mut ctx: impl AsContextMut, bytes: PassiveDataSegmentBytes) -> Self {
ctx.as_context_mut()
.store
.inner
.alloc_data_segment(DataSegmentEntity::passive(bytes))
}
}
#[derive(Debug)]
pub struct DataSegmentEntity {
bytes: Option<PassiveDataSegmentBytes>,
}
impl DataSegmentEntity {
pub fn active() -> Self {
Self { bytes: None }
}
pub fn passive(bytes: PassiveDataSegmentBytes) -> Self {
Self { bytes: Some(bytes) }
}
}
impl From<&'_ module::DataSegment> for DataSegmentEntity {
fn from(segment: &'_ module::DataSegment) -> Self {
Self {
bytes: segment.passive_data_segment_bytes(),
}
}
}
impl DataSegmentEntity {
pub fn bytes(&self) -> &[u8] {
self.bytes
.as_ref()
.map(AsRef::as_ref)
.unwrap_or_else(|| &[])
}
pub fn drop_bytes(&mut self) {
self.bytes = None;
}
}