use std::marker::PhantomData;
use std::mem::transmute;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr::NonNull;
#[repr(C)]
pub struct Local<'sc, T>(NonNull<T>, PhantomData<&'sc ()>);
impl<'sc, T> Copy for Local<'sc, T> {}
impl<'sc, T> Clone for Local<'sc, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'sc, T> Local<'sc, T> {
pub unsafe fn cast<A>(other: Local<'sc, A>) -> Self
where
Local<'sc, A>: From<Self>,
{
transmute(other)
}
pub(crate) unsafe fn from_raw(ptr: *const T) -> Option<Self> {
Some(Self(NonNull::new(ptr as *mut _)?, PhantomData))
}
pub(crate) fn as_non_null(self) -> NonNull<T> {
self.0
}
pub(crate) fn as_ptr(self) -> *const T {
self.0.as_ptr()
}
pub(crate) fn slice_into_raw(slice: &[Self]) -> &[*const T] {
unsafe { &*(slice as *const [Self] as *const [*const T]) }
}
}
impl<'sc, T> Deref for Local<'sc, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.0.as_ref() }
}
}
impl<'sc, T> DerefMut for Local<'sc, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { self.0.as_mut() }
}
}
#[test]
fn test_size_of_local() {
use crate::Value;
use std::mem::size_of;
assert_eq!(size_of::<Local<Value>>(), size_of::<*const Value>());
assert_eq!(size_of::<Option<Local<Value>>>(), size_of::<*const Value>());
}