use std::marker::PhantomData; use std::ops::Deref; use std::ops::DerefMut; use std::ptr::NonNull; use crate::value::Value; pub struct Local<'sc, T>(NonNull, PhantomData<&'sc ()>); impl<'sc, T> Copy for Local<'sc, T> {} impl<'sc, T> Clone for Local<'sc, T> { fn clone(&self) -> Self { Self(self.0, self.1) } } impl<'sc, T> Local<'sc, T> { pub(crate) unsafe fn from_raw(ptr: *mut T) -> Option { Some(Self(NonNull::new(ptr)?, PhantomData)) } } 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() } } } // TODO make it possible for targets other than Local. For example // Local should be able to be down cast to Local. impl<'sc, T> From> for Local<'sc, Value> where T: Deref, { fn from(v: Local<'sc, T>) -> Local<'sc, Value> { unsafe { std::mem::transmute(v) } } }