use crate::prelude::*;
pub unsafe trait PointerWrapper<N>
where
Self: Sized,
{
fn wrap(ptr: *mut N) -> Option<Self>;
fn unwrap(self) -> *mut N;
fn inner(&self) -> &N;
fn inner_mut(&mut self) -> &mut N;
}
pub unsafe trait ValueWrapper<N> {
fn wrap(native: N) -> Self;
fn unwrap(self) -> N;
fn inner(&self) -> &N;
fn inner_mut(&mut self) -> &mut N;
}
pub unsafe trait NativeTransmutableWrapper<N> {
fn wrap(native: N) -> Self;
fn unwrap(self) -> N;
fn inner(&self) -> &N;
fn inner_mut(&mut self) -> &mut N;
}
pub unsafe trait RefWrapper<N> {
fn wrap_ref(native: &N) -> &Self;
fn wrap_mut(native: &mut N) -> &mut Self;
fn inner(&self) -> &N;
fn inner_mut(&mut self) -> &mut N;
}
unsafe impl<N> ValueWrapper<N> for Handle<N>
where
N: NativeDrop,
{
fn wrap(native: N) -> Self
where
N: NativeDrop,
{
Self::from_native_c(native)
}
fn unwrap(self) -> N {
self.into_native()
}
fn inner(&self) -> &N {
self.native()
}
fn inner_mut(&mut self) -> &mut N {
self.native_mut()
}
}
unsafe impl<N> RefWrapper<N> for Handle<N>
where
N: NativeDrop,
{
fn wrap_ref(native: &N) -> &Self {
Self::from_native_ref(native)
}
fn wrap_mut(native: &mut N) -> &mut Self {
Self::from_native_ref_mut(native)
}
fn inner(&self) -> &N {
self.native()
}
fn inner_mut(&mut self) -> &mut N {
self.native_mut()
}
}
unsafe impl<N> PointerWrapper<N> for RefHandle<N>
where
N: NativeDrop,
{
fn wrap(ptr: *mut N) -> Option<Self> {
Self::from_ptr(ptr)
}
fn unwrap(self) -> *mut N {
self.into_ptr()
}
fn inner(&self) -> &N {
self.native()
}
fn inner_mut(&mut self) -> &mut N {
self.native_mut()
}
}
unsafe impl<N> PointerWrapper<N> for RCHandle<N>
where
N: NativeRefCounted,
{
fn wrap(ptr: *mut N) -> Option<Self> {
Self::from_ptr(ptr)
}
fn unwrap(self) -> *mut N {
self.into_ptr()
}
fn inner(&self) -> &N {
self.native()
}
fn inner_mut(&mut self) -> &mut N {
self.native_mut()
}
}
unsafe impl<N, T> NativeTransmutableWrapper<N> for T
where
N: Sized,
T: Sized,
T: NativeTransmutable<N>,
{
fn wrap(native: N) -> Self {
Self::from_native_c(native)
}
fn unwrap(self) -> N {
Self::into_native(self)
}
fn inner(&self) -> &N {
self.native()
}
fn inner_mut(&mut self) -> &mut N {
self.native_mut()
}
}