1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Provides `StackPtr`, an owned pointer to stack-allocated data. Create one
//! using the provided `stack_ptr!` macro:
//!
//! ```
//! stack_ptr! {
//!     let slice: StackPtr<[_]> = StackPtr::new([1,2,3,4,5]);
//! }
//! ```
#![cfg_attr(feature = "nightly", feature(unsize, coerce_unsized))]
use std::marker::PhantomData;
use std::{ptr, mem};
use std::ops::{Deref, DerefMut};

/// An owning pointer to stack-allocated data. Similar to `Box`, except `Box` is heap-allocated.
pub struct StackPtr<'a, T: 'a + ?Sized> {
    ptr: *mut T,
    _marker: PhantomData<T>,
    lifetime: PhantomData<&'a mut ()>
}

impl<'a, T: 'a + ?Sized> StackPtr<'a, T> {
    /// `ptr` must be a pointer to forgotten data on the stack, whose lifetime is at least as long as `lifetime`'s lifetime. Better to just use the `stack_ptr!` macro.
    pub unsafe fn new(ptr: *mut T, _lifetime: &'a mut ()) -> StackPtr<'a, T> {
        StackPtr {
            ptr: ptr,
            lifetime: PhantomData,
            _marker: PhantomData,
        }
    }

    /// an implementation of std::borrow::Borrow, where the returned reference has the same lifetime as `self`.
    pub fn borrow(&self) -> &'a T {
        unsafe {
            &*self.ptr
        }
    }

    /// an implementation of std::borrow::BorrowMut, where the returned reference has the same lifetime as `self`.
    pub fn borrow_mut(&mut self) -> &'a mut T {
        unsafe {
            &mut *self.ptr
        }
    }

    /// Consumes a `StackPtr` without running its destructor, and returns a `*mut` pointer to the data, and a `std::marker::PhantomData` representing the lifetime of the `StackPtr`. Useful for doing a conversion on the pointer and reconstructing a `StackPtr` with `from_raw_parts`.
    pub fn into_raw_parts(sp: StackPtr<'a, T>) -> (*mut T, PhantomData<&'a mut ()>) {
        let ret = (sp.ptr, sp.lifetime);
        mem::forget(sp);
        ret
    }

    /// Constructs a new `StackPtr` from its raw parts. Usually called on the result of `into_raw_parts`.
    pub unsafe fn from_raw_parts(ptr: *mut T, lifetime: PhantomData<&'a mut ()>) -> StackPtr<'a, T> {
        StackPtr {
            ptr: ptr,
            _marker: PhantomData,
            lifetime: lifetime
        }
    }
}

impl<'a, T: ?Sized> Drop for StackPtr<'a, T> {
    fn drop(&mut self) {
        unsafe {
            ptr::drop_in_place(self.ptr);
        }
    }
}

impl<'a, T: ?Sized> ::std::fmt::Debug for StackPtr<'a, T> where T: ::std::fmt::Debug {
    fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        self.deref().fmt(formatter)
    }
}

impl<'a, T: ?Sized> Deref for StackPtr<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.borrow()
    }
}

impl<'a, T: ?Sized> DerefMut for StackPtr<'a, T> {

    fn deref_mut(&mut self) -> &mut T {
        self.borrow_mut()
    }
}

unsafe impl<'a, T: 'a + Send + ?Sized> Send for StackPtr<'a, T> {}
unsafe impl<'a, T: 'a + Sync + ?Sized> Sync for StackPtr<'a, T> {}

/// Safely declares a StackPtr<$ty> with an appropriate lifetime to the data contained in $expr.
#[macro_export]
macro_rules! stack_ptr {
    (let $name:ident: StackPtr<$ty:ty> = StackPtr::new($expr:expr);) => {
        let mut _value = $expr;
        let mut _lifetime_marker = ();
        let $name = unsafe {
            let ptr = &mut _value as *mut $ty;
            ::std::mem::forget(_value);
            StackPtr::new(ptr, &mut _lifetime_marker)
        };
    };
}

/// An implementation of `std::ops::CoerceUnsized` on stable rust. On nightly, you can convert a `StackPtr<T>` into a `StackPtr<U>` if `T` implements `U`, with `let sp = sp as StackPtr<U>;`, but this requires the unstable `CoerceUnsized` trait. On stable you can do `let sp = coerce_stackptr!(sp, U);`.
#[macro_export]
macro_rules! coerce_stackptr {
    ($sp:expr, $ty:ty) => {{
        let (ptr, lifetime) = StackPtr::into_raw_parts($sp);
        unsafe {
            StackPtr::from_raw_parts(ptr as *mut $ty, lifetime)
        }
    }};
}

#[cfg(feature="nightly")]
mod nightly {
    use super::StackPtr;
    use std::ops::CoerceUnsized;
    use std::marker::Unsize;

    impl<'a, T, U> CoerceUnsized<StackPtr<'a, U>> for StackPtr<'a, T> where T: Unsize<U> + ?Sized, U: ?Sized {}
}