0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-12 02:27:50 -05:00
denoland-rusty-v8/src/local.rs

46 lines
1 KiB
Rust
Raw Normal View History

2019-12-04 08:12:27 -05:00
use std::marker::PhantomData;
use std::ops::Deref;
2019-12-04 08:12:27 -05:00
use std::ops::DerefMut;
use std::ptr::NonNull;
use crate::value::Value;
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)
}
}
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))
}
}
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() }
}
}
// 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) }
}
}