pub trait Resource: Sized + 'static {
type Rep: ResourceRep<Self>;
}
impl<T: 'static> Resource for T {
type Rep = Option<T>;
}
pub unsafe trait ResourceRep<T> {
fn rep_new(inner: T) -> Self;
unsafe fn rep_as_ref<'a>(ptr: *const Self) -> &'a T;
unsafe fn rep_as_mut<'a>(ptr: *mut Self) -> &'a mut T;
unsafe fn rep_take<'a>(ptr: *mut Self) -> T;
}
unsafe impl<T> ResourceRep<T> for Option<T> {
fn rep_new(inner: T) -> Option<T> {
Some(inner)
}
unsafe fn rep_as_ref<'a>(ptr: *const Option<T>) -> &'a T {
unsafe { (*ptr).as_ref().unwrap() }
}
unsafe fn rep_as_mut<'a>(ptr: *mut Option<T>) -> &'a mut T {
unsafe { (*ptr).as_mut().unwrap() }
}
unsafe fn rep_take(ptr: *mut Option<T>) -> T {
unsafe { (*ptr).take().unwrap() }
}
}