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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![cfg_attr(rustfmt, rustfmt::skip)]
//! Wrappers around `NonNull` to better express the semantics of such pointer.
//!
//! Useful when manually defining custom low-level `ReprC` types.

use_prelude!();

#[doc(no_inline)]
pub use ::core::ptr::*;

#[repr(transparent)]
pub
struct NonNullRef<T> (
    pub
    ptr::NonNull<T>, // Variance OK because immutable
);

#[repr(transparent)]
pub
struct NonNullMut<T> (
    pub
    ptr::NonNull<T>,

    pub
    PhantomInvariant<T>, // Must be invariant because non-owning mutable.
);

#[repr(transparent)]
pub
struct NonNullOwned<T> (
    pub
    ptr::NonNull<T>, // Variance OK because ownership

    pub
    PhantomData<T>, // Express ownership to dropck
);

macro_rules! impl_for_each {(
    [$($T:ident),* $(,)?]
        .impl_for_each!(|$dol:tt $NonNull:ident| {
            $($expansion:tt)*
        })
    ;
) => (
    // const _: () = {
        macro_rules! helper {(
            $dol $NonNull : ident
        ) => (
            $($expansion)*
        )}
        $(
            helper! { $T }
        )*
    // };
)}

impl_for_each! {
    [NonNullRef, NonNullMut, NonNullOwned].impl_for_each!(|$NonNull| {
        impl<T> From<NonNull<T>>
            for $NonNull<T>
        {
            #[inline]
            fn from (it: NonNull<T>)
              -> Self
            {
                unsafe { ::core::mem::transmute(it) }
            }
        }

        impl<T> ::core::ops::Deref
            for $NonNull<T>
        {
            type Target = ptr::NonNull<T>;

            #[inline]
            fn deref (self: &'_ $NonNull<T>)
            -> &'_ ptr::NonNull<T>
            {
                &self.0
            }
        }

        impl<T> fmt::Debug
            for $NonNull<T>
        {
            fn fmt (self: &'_ $NonNull<T>, fmt: &'_ mut fmt::Formatter<'_>)
              -> fmt::Result
            {
                fmt .debug_tuple(stringify!($NonNull))
                    .field(&self.0)
                    .finish()
            }
        }

        impl<T> $NonNull<T> {
            #[inline]
            pub
            fn as_ptr (self: &'_ Self)
              -> *const T
            {
                self.0.as_ptr()
            }

            #[inline]
            pub
            fn cast<U> (self: $NonNull<T>)
              -> $NonNull<U>
            {
                unsafe { ::core::mem::transmute(self) }
            }
        }
    });
}

impl_for_each! {
    [NonNullMut, NonNullOwned].impl_for_each!(|$NonNull| {
        impl<T> ::core::ops::DerefMut
            for $NonNull<T>
        {
            #[inline]
            fn deref_mut (self: &'_ mut $NonNull<T>)
              -> &'_ mut ptr::NonNull<T>
            {
                &mut self.0
            }
        }

        impl<T> $NonNull<T> {
            #[inline]
            pub
            fn as_mut_ptr (self: &'_ mut Self)
              -> *mut T
            {
                self.0.as_ptr()
            }

            #[inline]
            pub
            fn copy (self: &'_ mut $NonNull<T>)
              -> $NonNull<T>
            {
                $NonNull::<T> { .. *self }
            }
        }
    });
}
impl_for_each! {
    [NonNullMut, NonNullRef].impl_for_each!(|$NonNull| {
        impl<'lt, T : 'lt> From<&'lt mut T>
            for $NonNull<T>
        {
            #[inline]
            fn from (it: &'lt mut T)
              -> $NonNull<T>
            {
                $NonNull::from(NonNull::from(it))
            }
        }
    });
}
impl<'lt, T : 'lt> From<&'lt T>
    for NonNullRef<T>
{
    #[inline]
    fn from (it: &'lt T)
      -> NonNullRef<T>
    {
        NonNullRef::from(NonNull::from(it))
    }
}

impl<__> NonNullOwned<__> {
    cfg_alloc! {
        #[inline]
        pub
        unsafe
        fn dealloc<T> (self)
        {
            if ::core::mem::size_of::<T>() == 0 {
                return;
            }
            ::alloc::alloc::dealloc(
                self.0.as_ptr().cast(),
                ::alloc::alloc::Layout::new::<T>(),
            );
        }

        #[inline]
        pub
        unsafe
        fn drop_in_place_and_dealloc<T> (mut self)
        {
            drop_in_place::<T>(self.copy().cast().as_mut());
            self.dealloc::<T>();
        }
    }

    #[inline]
    pub
    unsafe
    fn drop_in_place<T> (self)
    {
        drop_in_place::<T>(self.0.cast().as_mut());
    }
}

impl<__> Copy
    for NonNullRef<__>
{}
impl<__> Clone
    for NonNullRef<__>
{
    #[inline]
    fn clone (self: &'_ Self)
      -> Self
    {
        *self
    }
}