2019-12-04 08:12:27 -05:00
|
|
|
use std::marker::PhantomData;
|
2019-11-30 10:31:51 -05:00
|
|
|
use std::ops::Deref;
|
2019-12-04 08:12:27 -05:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
use std::ptr::NonNull;
|
2019-12-13 21:40:54 -05:00
|
|
|
use crate::value::Value;
|
2019-11-30 10:31:51 -05:00
|
|
|
|
2019-12-04 08:12:27 -05:00
|
|
|
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(self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
2019-11-30 10:31:51 -05:00
|
|
|
|
|
|
|
impl<'sc, T> Local<'sc, T> {
|
2019-12-04 08:12:27 -05:00
|
|
|
pub(crate) unsafe fn from_raw(ptr: *mut T) -> Option<Self> {
|
|
|
|
Some(Self(NonNull::new(ptr)?, PhantomData))
|
2019-11-30 10:31:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'sc, T> Deref for Local<'sc, T> {
|
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
2019-12-04 08:12:27 -05:00
|
|
|
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() }
|
2019-11-30 10:31:51 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-13 21:40:54 -05:00
|
|
|
|
|
|
|
// TODO make it possible for targets other than Local<Value>. For example
|
|
|
|
// Local<String> should be able to be down cast to Local<Name>.
|
|
|
|
impl<'sc, T> From<Local<'sc, T>> for Local<'sc, Value>
|
|
|
|
where
|
|
|
|
T: Deref<Target = Value>,
|
|
|
|
{
|
|
|
|
fn from(v: Local<'sc, T>) -> Local<'sc, Value> {
|
|
|
|
unsafe { std::mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|