szun/src/prelude/refer.rs

64 lines
1.1 KiB
Rust

use crate::Runtime;
#[derive(Clone, Copy)]
pub struct Ref {
typeid:usize,
address:*const u8,
}
impl Ref {
pub(crate) fn new(typeid:usize, address:*const u8) -> Self
{
Self {
typeid,
address,
}
}
pub fn null() -> Self
{
Self {
typeid:0,
address:std::ptr::null(),
}
}
pub fn inner(&self) -> Self
{
let mut refer = *self;
refer.typeid = Runtime::type_in(self.typeid);
self.temporary()
}
pub fn temporary(&self) -> Self
{
let mut refer = *self;
refer.typeid |= 1 << (usize::BITS - 1);
refer
}
pub fn is_temporary(&self) -> bool
{
(self.typeid & (1 << (usize::BITS - 1))) != 0
}
pub fn is_null(&self) -> bool
{
self.address.is_null()
}
pub fn typeid(&self) -> usize
{
self.typeid & !(1 << (usize::BITS - 1))
}
pub fn address(&self) -> *const u8
{
self.address
}
pub fn address_mut(&self) -> *mut u8
{
self.address as _
}
}