easy_pin/
lib.rs

1/// The main `proc_macro_attribute`
2pub use ::easy_pin_proc_macro::easy_pin;
3
4/// Stuff exported under a name collision with the proc_macro_attribute
5#[doc(hidden)] pub mod easy_pin {pub use super::{
6    core,
7    PinDrop,
8    PinSensitive,
9};}
10
11pub
12trait PinDrop : Drop {
13    /// # Safety
14    ///
15    ///   - `PinDrop::drop_pinned` must never be called directly,
16    ///     only by `Drop::drop` (_e.g._, generated by `#[easy_pin(Drop)]`)
17    ///
18    ///   - However, **it must always be safe to call on `Drop`**, even when
19    ///     `Self` has never been behind a `Pin`-ned pointer.
20    unsafe
21    fn drop_pinned (self: core::pin::Pin<&'_ mut Self>);
22}
23
24#[derive(
25    Debug,
26    // Clone, /* `!Unpin` stuff should not have #[derive(Clone)] accessible */
27    PartialEq, Eq,
28    PartialOrd, Ord,
29)]
30#[repr(transparent)]
31pub
32struct PinSensitive<T> {
33    inner: T,
34    _marker: core::marker::PhantomPinned,
35}
36
37impl<T : Default> Default for PinSensitive<T> {
38    #[inline]
39    fn default () -> Self
40    {
41        Self {
42            inner: Default::default(),
43            _marker: core::marker::PhantomPinned,
44        }
45    }
46}
47
48impl<T> PinSensitive<T> {
49    #[inline]
50    pub
51    fn new (value: T) -> Self
52    {
53        Self {
54            inner: value,
55            _marker: core::marker::PhantomPinned,
56        }
57    }
58
59    #[inline]
60    pub
61    fn pinned_address<'__> (
62        self: core::pin::Pin<&'__ Self>,
63    ) -> core::ptr::NonNull<T> // variance is justified because *const
64    {
65        (&self.inner).into()
66    }
67}
68
69impl<T> core::ops::Deref for PinSensitive<T> {
70    type Target = T;
71
72    #[inline]
73    fn deref (self: &'_ Self) -> &'_ Self::Target
74    {
75        &self.inner
76    }
77}
78impl<T> core::ops::DerefMut for PinSensitive<T> {
79    #[inline]
80    fn deref_mut (self: &'_ mut Self) -> &'_ mut Self::Target
81    {
82        &mut self.inner
83    }
84}
85
86#[doc(hidden)]
87pub use ::core;