use core::marker::PhantomData;
use std::io;
use bstack::BStack;
use bytemuck::{Pod, Zeroable};
use super::{FileId, Offset, ResolvedFileId, ResolvedTypeId, TypeId};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Pod, Zeroable)]
#[repr(C)]
pub struct WidePtr {
file: FileId,
ty: TypeId,
offset: Offset,
}
const _: () = {
assert!(core::mem::size_of::<WidePtr>() == 16);
assert!(core::mem::align_of::<WidePtr>() == 8);
assert!(core::mem::offset_of!(WidePtr, file) == 0);
assert!(core::mem::offset_of!(WidePtr, ty) == 4);
assert!(core::mem::offset_of!(WidePtr, offset) == 8);
};
impl WidePtr {
pub const NULL: WidePtr = WidePtr {
file: FileId::SELF,
ty: TypeId::UNTYPED,
offset: Offset::NULL,
};
pub const fn new(file: FileId, offset: Offset) -> Self {
WidePtr {
file,
ty: TypeId::UNTYPED,
offset,
}
}
pub const fn with_parts(file: FileId, ty: TypeId, offset: Offset) -> Self {
WidePtr { file, ty, offset }
}
pub const fn with_type(mut self, ty: TypeId) -> Self {
self.ty = ty;
self
}
pub const fn from_raw(file_id: u64, type_index: u32, offset: u64) -> Self {
WidePtr {
file: FileId::from_raw(file_id as u32),
ty: TypeId::from_raw(type_index),
offset: Offset::from_raw(offset),
}
}
pub fn decode(b: &[u8]) -> WidePtr {
let file_id = u32::from_le_bytes(b[0..4].try_into().unwrap()) as u64;
let type_index = u32::from_le_bytes(b[4..8].try_into().unwrap());
let offset = u64::from_le_bytes(b[8..16].try_into().unwrap());
WidePtr::from_raw(file_id, type_index, offset)
}
pub fn read_from_stack(data: &BStack, off: u64) -> io::Result<WidePtr> {
let mut b = [0u8; core::mem::size_of::<WidePtr>()];
data.get_into(off, &mut b)?;
Ok(WidePtr::decode(&b))
}
pub const fn file(self) -> FileId {
self.file
}
pub const fn type_id(self) -> TypeId {
self.ty
}
pub const fn offset(self) -> Offset {
self.offset
}
pub const fn file_id(self) -> u64 {
self.file.as_u64()
}
pub const fn type_index(self) -> u32 {
self.ty.get()
}
pub const fn is_self(self) -> bool {
self.file.is_self()
}
pub const fn is_null(self) -> bool {
self.offset.is_null()
}
pub const fn resolved_file(self) -> Option<ResolvedFileId> {
self.file.resolve()
}
pub const fn resolved_type(self) -> Option<ResolvedTypeId> {
self.ty.resolve()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BrandedWidePtr<'a> {
ptr: WidePtr,
_brand: PhantomData<fn() -> &'a ()>,
}
impl<'a> BrandedWidePtr<'a> {
#[inline(always)]
pub const fn from_wide(ptr: WidePtr) -> Self {
BrandedWidePtr {
ptr,
_brand: PhantomData,
}
}
#[inline(always)]
pub const fn wide(self) -> WidePtr {
self.ptr
}
#[inline(always)]
pub const fn new(file: FileId, offset: Offset) -> Self {
Self::from_wide(WidePtr::new(file, offset))
}
#[inline(always)]
pub const fn with_parts(file: FileId, ty: TypeId, offset: Offset) -> Self {
Self::from_wide(WidePtr::with_parts(file, ty, offset))
}
#[inline(always)]
pub const fn with_type(self, ty: TypeId) -> Self {
Self::from_wide(self.ptr.with_type(ty))
}
#[inline(always)]
pub fn decode(b: &[u8]) -> Self {
Self::from_wide(WidePtr::decode(b))
}
#[inline(always)]
pub fn read_from_stack(data: &BStack, off: u64) -> io::Result<Self> {
WidePtr::read_from_stack(data, off).map(Self::from_wide)
}
#[inline(always)]
pub const fn file(self) -> FileId {
self.ptr.file()
}
#[inline(always)]
pub const fn type_id(self) -> TypeId {
self.ptr.type_id()
}
#[inline(always)]
pub const fn offset(self) -> Offset {
self.ptr.offset()
}
#[inline(always)]
pub const fn is_self(self) -> bool {
self.ptr.is_self()
}
#[inline(always)]
pub const fn is_null(self) -> bool {
self.ptr.is_null()
}
#[inline(always)]
pub const fn resolved_file(self) -> Option<ResolvedFileId> {
self.ptr.resolved_file()
}
#[inline(always)]
pub const fn resolved_type(self) -> Option<ResolvedTypeId> {
self.ptr.resolved_type()
}
}
impl<'a> From<WidePtr> for BrandedWidePtr<'a> {
#[inline(always)]
fn from(ptr: WidePtr) -> Self {
Self::from_wide(ptr)
}
}
impl From<BrandedWidePtr<'_>> for WidePtr {
#[inline(always)]
fn from(b: BrandedWidePtr<'_>) -> WidePtr {
b.wide()
}
}