drop-ptr 0.1.2

Smart pointer with per-type custom drop
Documentation
#![no_std]

use core::marker::PhantomData;
use core::ops::*;

pub unsafe trait DropPtr {
    unsafe fn drop_ptr(*mut Self);
}

pub struct Box<'a, A: ?Sized + DropPtr>(*mut A, PhantomData<&'a mut ()>);

impl<'a, A: ?Sized + DropPtr> Box<'a, A> {
    #[inline]
    pub unsafe fn new(ptr: *mut A) -> Option<Self> {
        if 0 == ptr as *mut () as usize { None } else { Some(Box(ptr, PhantomData)) }
    }

    #[inline]
    pub unsafe fn new_unchecked(ptr: *mut A) -> Self { Box(ptr, PhantomData) }
}

impl<'a, A: ?Sized + DropPtr> Drop for Box<'a, A> {
    #[inline]
    fn drop(&mut self) { unsafe { A::drop_ptr(self.0) } }
}

impl<'a, A: ?Sized + DropPtr> Deref for Box<'a, A> {
    type Target = A;

    #[inline]
    fn deref(&self) -> &A { unsafe { &*self.0 } }
}

impl<'a, A: ?Sized + DropPtr> DerefMut for Box<'a, A> {
    #[inline]
    fn deref_mut(&mut self) -> &mut A { unsafe { &mut *self.0 } }
}