~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/init/macros.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/init/macros.rs (Version linux-6.12-rc7) and /rust/kernel/init/macros.rs (Version linux-6.4.16)


  1 // SPDX-License-Identifier: Apache-2.0 OR MIT       1 // SPDX-License-Identifier: Apache-2.0 OR MIT
  2                                                     2 
  3 //! This module provides the macros that actua      3 //! This module provides the macros that actually implement the proc-macros `pin_data` and
  4 //! `pinned_drop`. It also contains `__init_in !!   4 //! `pinned_drop`.
  5 //! macros.                                    << 
  6 //!                                                 5 //!
  7 //! These macros should never be called direct      6 //! These macros should never be called directly, since they expect their input to be
  8 //! in a certain format which is internal. If  !!   7 //! in a certain format which is internal. Use the proc-macros instead.
  9 //! safe code! Use the public facing macros in << 
 10 //!                                                 8 //!
 11 //! This architecture has been chosen because       9 //! This architecture has been chosen because the kernel does not yet have access to `syn` which
 12 //! would make matters a lot easier for implem     10 //! would make matters a lot easier for implementing these as proc-macros.
 13 //!                                                11 //!
 14 //! # Macro expansion example                      12 //! # Macro expansion example
 15 //!                                                13 //!
 16 //! This section is intended for readers tryin     14 //! This section is intended for readers trying to understand the macros in this module and the
 17 //! `pin_init!` macros from `init.rs`.             15 //! `pin_init!` macros from `init.rs`.
 18 //!                                                16 //!
 19 //! We will look at the following example:         17 //! We will look at the following example:
 20 //!                                                18 //!
 21 //! ```rust,ignore                             !!  19 //! ```rust
 22 //! # use kernel::init::*;                         20 //! # use kernel::init::*;
 23 //! # use core::pin::Pin;                      << 
 24 //! #[pin_data]                                    21 //! #[pin_data]
 25 //! #[repr(C)]                                     22 //! #[repr(C)]
 26 //! struct Bar<T> {                                23 //! struct Bar<T> {
 27 //!     #[pin]                                     24 //!     #[pin]
 28 //!     t: T,                                      25 //!     t: T,
 29 //!     pub x: usize,                              26 //!     pub x: usize,
 30 //! }                                              27 //! }
 31 //!                                                28 //!
 32 //! impl<T> Bar<T> {                               29 //! impl<T> Bar<T> {
 33 //!     fn new(t: T) -> impl PinInit<Self> {       30 //!     fn new(t: T) -> impl PinInit<Self> {
 34 //!         pin_init!(Self { t, x: 0 })            31 //!         pin_init!(Self { t, x: 0 })
 35 //!     }                                          32 //!     }
 36 //! }                                              33 //! }
 37 //!                                                34 //!
 38 //! #[pin_data(PinnedDrop)]                        35 //! #[pin_data(PinnedDrop)]
 39 //! struct Foo {                                   36 //! struct Foo {
 40 //!     a: usize,                                  37 //!     a: usize,
 41 //!     #[pin]                                     38 //!     #[pin]
 42 //!     b: Bar<u32>,                               39 //!     b: Bar<u32>,
 43 //! }                                              40 //! }
 44 //!                                                41 //!
 45 //! #[pinned_drop]                                 42 //! #[pinned_drop]
 46 //! impl PinnedDrop for Foo {                      43 //! impl PinnedDrop for Foo {
 47 //!     fn drop(self: Pin<&mut Self>) {            44 //!     fn drop(self: Pin<&mut Self>) {
 48 //!         pr_info!("{self:p} is getting drop !!  45 //!         println!("{self:p} is getting dropped.");
 49 //!     }                                          46 //!     }
 50 //! }                                              47 //! }
 51 //!                                                48 //!
 52 //! let a = 42;                                    49 //! let a = 42;
 53 //! let initializer = pin_init!(Foo {              50 //! let initializer = pin_init!(Foo {
 54 //!     a,                                         51 //!     a,
 55 //!     b <- Bar::new(36),                         52 //!     b <- Bar::new(36),
 56 //! });                                            53 //! });
 57 //! ```                                            54 //! ```
 58 //!                                                55 //!
 59 //! This example includes the most common and      56 //! This example includes the most common and important features of the pin-init API.
 60 //!                                                57 //!
 61 //! Below you can find individual section abou     58 //! Below you can find individual section about the different macro invocations. Here are some
 62 //! general things we need to take into accoun     59 //! general things we need to take into account when designing macros:
 63 //! - use global paths, similarly to file path     60 //! - use global paths, similarly to file paths, these start with the separator: `::core::panic!()`
 64 //!   this ensures that the correct item is us     61 //!   this ensures that the correct item is used, since users could define their own `mod core {}`
 65 //!   and then their own `panic!` inside to ex     62 //!   and then their own `panic!` inside to execute arbitrary code inside of our macro.
 66 //! - macro `unsafe` hygiene: we need to ensur     63 //! - macro `unsafe` hygiene: we need to ensure that we do not expand arbitrary, user-supplied
 67 //!   expressions inside of an `unsafe` block      64 //!   expressions inside of an `unsafe` block in the macro, because this would allow users to do
 68 //!   `unsafe` operations without an associate     65 //!   `unsafe` operations without an associated `unsafe` block.
 69 //!                                                66 //!
 70 //! ## `#[pin_data]` on `Bar`                      67 //! ## `#[pin_data]` on `Bar`
 71 //!                                                68 //!
 72 //! This macro is used to specify which fields     69 //! This macro is used to specify which fields are structurally pinned and which fields are not. It
 73 //! is placed on the struct definition and all     70 //! is placed on the struct definition and allows `#[pin]` to be placed on the fields.
 74 //!                                                71 //!
 75 //! Here is the definition of `Bar` from our e     72 //! Here is the definition of `Bar` from our example:
 76 //!                                                73 //!
 77 //! ```rust,ignore                             !!  74 //! ```rust
 78 //! # use kernel::init::*;                         75 //! # use kernel::init::*;
 79 //! #[pin_data]                                    76 //! #[pin_data]
 80 //! #[repr(C)]                                     77 //! #[repr(C)]
 81 //! struct Bar<T> {                                78 //! struct Bar<T> {
 82 //!     #[pin]                                 << 
 83 //!     t: T,                                      79 //!     t: T,
 84 //!     pub x: usize,                              80 //!     pub x: usize,
 85 //! }                                              81 //! }
 86 //! ```                                            82 //! ```
 87 //!                                                83 //!
 88 //! This expands to the following code:            84 //! This expands to the following code:
 89 //!                                                85 //!
 90 //! ```rust,ignore                             !!  86 //! ```rust
 91 //! // Firstly the normal definition of the st     87 //! // Firstly the normal definition of the struct, attributes are preserved:
 92 //! #[repr(C)]                                     88 //! #[repr(C)]
 93 //! struct Bar<T> {                                89 //! struct Bar<T> {
 94 //!     t: T,                                      90 //!     t: T,
 95 //!     pub x: usize,                              91 //!     pub x: usize,
 96 //! }                                              92 //! }
 97 //! // Then an anonymous constant is defined,      93 //! // Then an anonymous constant is defined, this is because we do not want any code to access the
 98 //! // types that we define inside:                94 //! // types that we define inside:
 99 //! const _: () = {                                95 //! const _: () = {
100 //!     // We define the pin-data carrying str     96 //!     // We define the pin-data carrying struct, it is a ZST and needs to have the same generics,
101 //!     // since we need to implement access f     97 //!     // since we need to implement access functions for each field and thus need to know its
102 //!     // type.                                   98 //!     // type.
103 //!     struct __ThePinData<T> {                   99 //!     struct __ThePinData<T> {
104 //!         __phantom: ::core::marker::Phantom    100 //!         __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
105 //!     }                                         101 //!     }
106 //!     // We implement `Copy` for the pin-dat    102 //!     // We implement `Copy` for the pin-data struct, since all functions it defines will take
107 //!     // `self` by value.                       103 //!     // `self` by value.
108 //!     impl<T> ::core::clone::Clone for __The    104 //!     impl<T> ::core::clone::Clone for __ThePinData<T> {
109 //!         fn clone(&self) -> Self {             105 //!         fn clone(&self) -> Self {
110 //!             *self                             106 //!             *self
111 //!         }                                     107 //!         }
112 //!     }                                         108 //!     }
113 //!     impl<T> ::core::marker::Copy for __The    109 //!     impl<T> ::core::marker::Copy for __ThePinData<T> {}
114 //!     // For every field of `Bar`, the pin-d    110 //!     // For every field of `Bar`, the pin-data struct will define a function with the same name
115 //!     // and accessor (`pub` or `pub(crate)`    111 //!     // and accessor (`pub` or `pub(crate)` etc.). This function will take a pointer to the
116 //!     // field (`slot`) and a `PinInit` or `    112 //!     // field (`slot`) and a `PinInit` or `Init` depending on the projection kind of the field
117 //!     // (if pinning is structural for the f    113 //!     // (if pinning is structural for the field, then `PinInit` otherwise `Init`).
118 //!     #[allow(dead_code)]                       114 //!     #[allow(dead_code)]
119 //!     impl<T> __ThePinData<T> {                 115 //!     impl<T> __ThePinData<T> {
120 //!         unsafe fn t<E>(                       116 //!         unsafe fn t<E>(
121 //!             self,                             117 //!             self,
122 //!             slot: *mut T,                     118 //!             slot: *mut T,
123 //!             // Since `t` is `#[pin]`, this !! 119 //!             init: impl ::kernel::init::Init<T, E>,
124 //!             init: impl ::kernel::init::Pin << 
125 //!         ) -> ::core::result::Result<(), E>    120 //!         ) -> ::core::result::Result<(), E> {
126 //!             unsafe { ::kernel::init::PinIn !! 121 //!             unsafe { ::kernel::init::Init::__init(init, slot) }
127 //!         }                                     122 //!         }
128 //!         pub unsafe fn x<E>(                   123 //!         pub unsafe fn x<E>(
129 //!             self,                             124 //!             self,
130 //!             slot: *mut usize,                 125 //!             slot: *mut usize,
131 //!             // Since `x` is not `#[pin]`,  << 
132 //!             init: impl ::kernel::init::Ini    126 //!             init: impl ::kernel::init::Init<usize, E>,
133 //!         ) -> ::core::result::Result<(), E>    127 //!         ) -> ::core::result::Result<(), E> {
134 //!             unsafe { ::kernel::init::Init:    128 //!             unsafe { ::kernel::init::Init::__init(init, slot) }
135 //!         }                                     129 //!         }
136 //!     }                                         130 //!     }
137 //!     // Implement the internal `HasPinData`    131 //!     // Implement the internal `HasPinData` trait that associates `Bar` with the pin-data struct
138 //!     // that we constructed above.          !! 132 //!     // that we constructed beforehand.
139 //!     unsafe impl<T> ::kernel::init::__inter    133 //!     unsafe impl<T> ::kernel::init::__internal::HasPinData for Bar<T> {
140 //!         type PinData = __ThePinData<T>;       134 //!         type PinData = __ThePinData<T>;
141 //!         unsafe fn __pin_data() -> Self::Pi    135 //!         unsafe fn __pin_data() -> Self::PinData {
142 //!             __ThePinData {                    136 //!             __ThePinData {
143 //!                 __phantom: ::core::marker:    137 //!                 __phantom: ::core::marker::PhantomData,
144 //!             }                                 138 //!             }
145 //!         }                                     139 //!         }
146 //!     }                                         140 //!     }
147 //!     // Implement the internal `PinData` tr    141 //!     // Implement the internal `PinData` trait that marks the pin-data struct as a pin-data
148 //!     // struct. This is important to ensure !! 142 //!     // struct. This is important to ensure that no user can implement a rouge `__pin_data`
149 //!     // function without using `unsafe`.       143 //!     // function without using `unsafe`.
150 //!     unsafe impl<T> ::kernel::init::__inter    144 //!     unsafe impl<T> ::kernel::init::__internal::PinData for __ThePinData<T> {
151 //!         type Datee = Bar<T>;                  145 //!         type Datee = Bar<T>;
152 //!     }                                         146 //!     }
153 //!     // Now we only want to implement `Unpi    147 //!     // Now we only want to implement `Unpin` for `Bar` when every structurally pinned field is
154 //!     // `Unpin`. In other words, whether `B    148 //!     // `Unpin`. In other words, whether `Bar` is `Unpin` only depends on structurally pinned
155 //!     // fields (those marked with `#[pin]`)    149 //!     // fields (those marked with `#[pin]`). These fields will be listed in this struct, in our
156 //!     // case no such fields exist, hence th    150 //!     // case no such fields exist, hence this is almost empty. The two phantomdata fields exist
157 //!     // for two reasons:                       151 //!     // for two reasons:
158 //!     // - `__phantom`: every generic must b    152 //!     // - `__phantom`: every generic must be used, since we cannot really know which generics
159 //!     //   are used, we declare all and then !! 153 //!     //   are used, we declere all and then use everything here once.
160 //!     // - `__phantom_pin`: uses the `'__pin    154 //!     // - `__phantom_pin`: uses the `'__pin` lifetime and ensures that this struct is invariant
161 //!     //   over it. The lifetime is needed t    155 //!     //   over it. The lifetime is needed to work around the limitation that trait bounds must
162 //!     //   not be trivial, e.g. the user has    156 //!     //   not be trivial, e.g. the user has a `#[pin] PhantomPinned` field -- this is
163 //!     //   unconditionally `!Unpin` and resu    157 //!     //   unconditionally `!Unpin` and results in an error. The lifetime tricks the compiler
164 //!     //   into accepting these bounds regar    158 //!     //   into accepting these bounds regardless.
165 //!     #[allow(dead_code)]                       159 //!     #[allow(dead_code)]
166 //!     struct __Unpin<'__pin, T> {               160 //!     struct __Unpin<'__pin, T> {
167 //!         __phantom_pin: ::core::marker::Pha    161 //!         __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
168 //!         __phantom: ::core::marker::Phantom    162 //!         __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
169 //!         // Our only `#[pin]` field is `t`. << 
170 //!         t: T,                              << 
171 //!     }                                         163 //!     }
172 //!     #[doc(hidden)]                            164 //!     #[doc(hidden)]
173 //!     impl<'__pin, T> ::core::marker::Unpin  !! 165 //!     impl<'__pin, T>
174 //!     where                                  !! 166 //!         ::core::marker::Unpin for Bar<T> where __Unpin<'__pin, T>: ::core::marker::Unpin {}
175 //!         __Unpin<'__pin, T>: ::core::marker << 
176 //!     {}                                     << 
177 //!     // Now we need to ensure that `Bar` do    167 //!     // Now we need to ensure that `Bar` does not implement `Drop`, since that would give users
178 //!     // access to `&mut self` inside of `dr    168 //!     // access to `&mut self` inside of `drop` even if the struct was pinned. This could lead to
179 //!     // UB with only safe code, so we disal    169 //!     // UB with only safe code, so we disallow this by giving a trait implementation error using
180 //!     // a direct impl and a blanket impleme    170 //!     // a direct impl and a blanket implementation.
181 //!     trait MustNotImplDrop {}                  171 //!     trait MustNotImplDrop {}
182 //!     // Normally `Drop` bounds do not have     172 //!     // Normally `Drop` bounds do not have the correct semantics, but for this purpose they do
183 //!     // (normally people want to know if a     173 //!     // (normally people want to know if a type has any kind of drop glue at all, here we want
184 //!     // to know if it has any kind of custo    174 //!     // to know if it has any kind of custom drop glue, which is exactly what this bound does).
185 //!     #[allow(drop_bounds)]                     175 //!     #[allow(drop_bounds)]
186 //!     impl<T: ::core::ops::Drop> MustNotImpl    176 //!     impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
187 //!     impl<T> MustNotImplDrop for Bar<T> {}     177 //!     impl<T> MustNotImplDrop for Bar<T> {}
188 //!     // Here comes a convenience check, if     178 //!     // Here comes a convenience check, if one implemented `PinnedDrop`, but forgot to add it to
189 //!     // `#[pin_data]`, then this will error    179 //!     // `#[pin_data]`, then this will error with the same mechanic as above, this is not needed
190 //!     // for safety, but a good sanity check    180 //!     // for safety, but a good sanity check, since no normal code calls `PinnedDrop::drop`.
191 //!     #[allow(non_camel_case_types)]            181 //!     #[allow(non_camel_case_types)]
192 //!     trait UselessPinnedDropImpl_you_need_t    182 //!     trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
193 //!     impl<                                  !! 183 //!     impl<T: ::kernel::init::PinnedDrop>
194 //!         T: ::kernel::init::PinnedDrop,     !! 184 //!         UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
195 //!     > UselessPinnedDropImpl_you_need_to_sp << 
196 //!     impl<T> UselessPinnedDropImpl_you_need    185 //!     impl<T> UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for Bar<T> {}
197 //! };                                            186 //! };
198 //! ```                                           187 //! ```
199 //!                                               188 //!
200 //! ## `pin_init!` in `impl Bar`                  189 //! ## `pin_init!` in `impl Bar`
201 //!                                               190 //!
202 //! This macro creates an pin-initializer for     191 //! This macro creates an pin-initializer for the given struct. It requires that the struct is
203 //! annotated by `#[pin_data]`.                   192 //! annotated by `#[pin_data]`.
204 //!                                               193 //!
205 //! Here is the impl on `Bar` defining the new    194 //! Here is the impl on `Bar` defining the new function:
206 //!                                               195 //!
207 //! ```rust,ignore                             !! 196 //! ```rust
208 //! impl<T> Bar<T> {                              197 //! impl<T> Bar<T> {
209 //!     fn new(t: T) -> impl PinInit<Self> {      198 //!     fn new(t: T) -> impl PinInit<Self> {
210 //!         pin_init!(Self { t, x: 0 })           199 //!         pin_init!(Self { t, x: 0 })
211 //!     }                                         200 //!     }
212 //! }                                             201 //! }
213 //! ```                                           202 //! ```
214 //!                                               203 //!
215 //! This expands to the following code:           204 //! This expands to the following code:
216 //!                                               205 //!
217 //! ```rust,ignore                             !! 206 //! ```rust
218 //! impl<T> Bar<T> {                              207 //! impl<T> Bar<T> {
219 //!     fn new(t: T) -> impl PinInit<Self> {      208 //!     fn new(t: T) -> impl PinInit<Self> {
220 //!         {                                     209 //!         {
221 //!             // We do not want to allow arb    210 //!             // We do not want to allow arbitrary returns, so we declare this type as the `Ok`
222 //!             // return type and shadow it l    211 //!             // return type and shadow it later when we insert the arbitrary user code. That way
223 //!             // there will be no possibilit    212 //!             // there will be no possibility of returning without `unsafe`.
224 //!             struct __InitOk;                  213 //!             struct __InitOk;
225 //!             // Get the data about fields f !! 214 //!             // Get the pin-data type from the initialized type.
226 //!             // - the function is unsafe, h    215 //!             // - the function is unsafe, hence the unsafe block
227 //!             // - we `use` the `HasPinData`    216 //!             // - we `use` the `HasPinData` trait in the block, it is only available in that
228 //!             //   scope.                       217 //!             //   scope.
229 //!             let data = unsafe {               218 //!             let data = unsafe {
230 //!                 use ::kernel::init::__inte    219 //!                 use ::kernel::init::__internal::HasPinData;
231 //!                 Self::__pin_data()            220 //!                 Self::__pin_data()
232 //!             };                                221 //!             };
233 //!             // Ensure that `data` really i !! 222 //!             // Use `data` to help with type inference, the closure supplied will have the type
                                                   >> 223 //!             // `FnOnce(*mut Self) -> Result<__InitOk, Infallible>`.
234 //!             let init = ::kernel::init::__i    224 //!             let init = ::kernel::init::__internal::PinData::make_closure::<
235 //!                 _,                            225 //!                 _,
236 //!                 __InitOk,                     226 //!                 __InitOk,
237 //!                 ::core::convert::Infallibl    227 //!                 ::core::convert::Infallible,
238 //!             >(data, move |slot| {             228 //!             >(data, move |slot| {
239 //!                 {                             229 //!                 {
240 //!                     // Shadow the structur    230 //!                     // Shadow the structure so it cannot be used to return early. If a user
241 //!                     // tries to write `ret !! 231 //!                     // tries to write `return Ok(__InitOk)`, then they get a type error, since
242 //!                     // since that will ref !! 232 //!                     // that will refer to this struct instead of the one defined above.
243 //!                     // above.              << 
244 //!                     struct __InitOk;          233 //!                     struct __InitOk;
245 //!                     // This is the expansi    234 //!                     // This is the expansion of `t,`, which is syntactic sugar for `t: t,`.
246 //!                     {                      !! 235 //!                     unsafe { ::core::ptr::write(&raw mut (*slot).t, t) };
247 //!                         unsafe { ::core::p !! 236 //!                     // Since initialization could fail later (not in this case, since the error
248 //!                     }                      !! 237 //!                     // type is `Infallible`) we will need to drop this field if it fails. This
249 //!                     // Since initializatio !! 238 //!                     // `DropGuard` will drop the field when it gets dropped and has not yet
250 //!                     // error type is `Infa !! 239 //!                     // been forgotten. We make a reference to it, so users cannot `mem::forget`
251 //!                     // is an error later.  !! 240 //!                     // it from the initializer, since the name is the same as the field.
252 //!                     // dropped and has not !! 241 //!                     let t = &unsafe {
253 //!                     let __t_guard = unsafe !! 242 //!                         ::kernel::init::__internal::DropGuard::new(&raw mut (*slot).t)
254 //!                         ::pinned_init::__i << 
255 //!                     };                        243 //!                     };
256 //!                     // Expansion of `x: 0,    244 //!                     // Expansion of `x: 0,`:
257 //!                     // Since this can be a !! 245 //!                     // Since this can be an arbitrary expression we cannot place it inside of
258 //!                     // of the `unsafe` blo !! 246 //!                     // the `unsafe` block, so we bind it here.
259 //!                     {                      !! 247 //!                     let x = 0;
260 //!                         let x = 0;         !! 248 //!                     unsafe { ::core::ptr::write(&raw mut (*slot).x, x) };
261 //!                         unsafe { ::core::p !! 249 //!                     let x = &unsafe {
262 //!                     }                      !! 250 //!                         ::kernel::init::__internal::DropGuard::new(&raw mut (*slot).x)
263 //!                     // We again create a ` << 
264 //!                     let __x_guard = unsafe << 
265 //!                         ::kernel::init::__ << 
266 //!                     };                        251 //!                     };
267 //!                     // Since initializatio !! 252 //!
268 //!                     // the guards. This is !! 253 //!                     // Here we use the type checker to ensuer that every field has been
269 //!                     // `&DropGuard`.       << 
270 //!                     ::core::mem::forget(__ << 
271 //!                     ::core::mem::forget(__ << 
272 //!                     // Here we use the typ << 
273 //!                     // initialized exactly    254 //!                     // initialized exactly once, since this is `if false` it will never get
274 //!                     // executed, but still    255 //!                     // executed, but still type-checked.
275 //!                     // Additionally we abu !! 256 //!                     // Additionally we abuse `slot` to automatically infer the correct type for
276 //!                     // for the struct. Thi !! 257 //!                     // the struct. This is also another check that every field is accessible
277 //!                     // accessible from thi !! 258 //!                     // from this scope.
278 //!                     #[allow(unreachable_co    259 //!                     #[allow(unreachable_code, clippy::diverging_sub_expression)]
279 //!                     let _ = || {           !! 260 //!                     if false {
280 //!                         unsafe {              261 //!                         unsafe {
281 //!                             ::core::ptr::w    262 //!                             ::core::ptr::write(
282 //!                                 slot,         263 //!                                 slot,
283 //!                                 Self {        264 //!                                 Self {
284 //!                                     // We  !! 265 //!                                     // We only care about typecheck finding every field here,
285 //!                                     // her !! 266 //!                                     // the expression does not matter, just conjure one using
286 //!                                     // one !! 267 //!                                     // `panic!()`:
287 //!                                     t: ::c    268 //!                                     t: ::core::panic!(),
288 //!                                     x: ::c    269 //!                                     x: ::core::panic!(),
289 //!                                 },            270 //!                                 },
290 //!                             );                271 //!                             );
291 //!                         };                    272 //!                         };
292 //!                     };                     !! 273 //!                     }
                                                   >> 274 //!                     // Since initialization has successfully completed, we can now forget the
                                                   >> 275 //!                     // guards.
                                                   >> 276 //!                     unsafe { ::kernel::init::__internal::DropGuard::forget(t) };
                                                   >> 277 //!                     unsafe { ::kernel::init::__internal::DropGuard::forget(x) };
293 //!                 }                             278 //!                 }
294 //!                 // We leave the scope abov    279 //!                 // We leave the scope above and gain access to the previously shadowed
295 //!                 // `__InitOk` that we need    280 //!                 // `__InitOk` that we need to return.
296 //!                 Ok(__InitOk)                  281 //!                 Ok(__InitOk)
297 //!             });                               282 //!             });
298 //!             // Change the return type from !! 283 //!             // Change the return type of the closure.
299 //!             let init = move |              !! 284 //!             let init = move |slot| -> ::core::result::Result<(), ::core::convert::Infallible> {
300 //!                 slot,                      << 
301 //!             | -> ::core::result::Result<() << 
302 //!                 init(slot).map(|__InitOk|     285 //!                 init(slot).map(|__InitOk| ())
303 //!             };                                286 //!             };
304 //!             // Construct the initializer.     287 //!             // Construct the initializer.
305 //!             let init = unsafe {               288 //!             let init = unsafe {
306 //!                 ::kernel::init::pin_init_f !! 289 //!                 ::kernel::init::pin_init_from_closure::<_, ::core::convert::Infallible>(init)
307 //!                     _,                     << 
308 //!                     ::core::convert::Infal << 
309 //!                 >(init)                    << 
310 //!             };                                290 //!             };
311 //!             init                              291 //!             init
312 //!         }                                     292 //!         }
313 //!     }                                         293 //!     }
314 //! }                                             294 //! }
315 //! ```                                           295 //! ```
316 //!                                               296 //!
317 //! ## `#[pin_data]` on `Foo`                     297 //! ## `#[pin_data]` on `Foo`
318 //!                                               298 //!
319 //! Since we already took a look at `#[pin_dat    299 //! Since we already took a look at `#[pin_data]` on `Bar`, this section will only explain the
320 //! differences/new things in the expansion of    300 //! differences/new things in the expansion of the `Foo` definition:
321 //!                                               301 //!
322 //! ```rust,ignore                             !! 302 //! ```rust
323 //! #[pin_data(PinnedDrop)]                       303 //! #[pin_data(PinnedDrop)]
324 //! struct Foo {                                  304 //! struct Foo {
325 //!     a: usize,                                 305 //!     a: usize,
326 //!     #[pin]                                    306 //!     #[pin]
327 //!     b: Bar<u32>,                              307 //!     b: Bar<u32>,
328 //! }                                             308 //! }
329 //! ```                                           309 //! ```
330 //!                                               310 //!
331 //! This expands to the following code:           311 //! This expands to the following code:
332 //!                                               312 //!
333 //! ```rust,ignore                             !! 313 //! ```rust
334 //! struct Foo {                                  314 //! struct Foo {
335 //!     a: usize,                                 315 //!     a: usize,
336 //!     b: Bar<u32>,                              316 //!     b: Bar<u32>,
337 //! }                                             317 //! }
338 //! const _: () = {                               318 //! const _: () = {
339 //!     struct __ThePinData {                     319 //!     struct __ThePinData {
340 //!         __phantom: ::core::marker::Phantom    320 //!         __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
341 //!     }                                         321 //!     }
342 //!     impl ::core::clone::Clone for __ThePin    322 //!     impl ::core::clone::Clone for __ThePinData {
343 //!         fn clone(&self) -> Self {             323 //!         fn clone(&self) -> Self {
344 //!             *self                             324 //!             *self
345 //!         }                                     325 //!         }
346 //!     }                                         326 //!     }
347 //!     impl ::core::marker::Copy for __ThePin    327 //!     impl ::core::marker::Copy for __ThePinData {}
348 //!     #[allow(dead_code)]                       328 //!     #[allow(dead_code)]
349 //!     impl __ThePinData {                       329 //!     impl __ThePinData {
350 //!         unsafe fn b<E>(                       330 //!         unsafe fn b<E>(
351 //!             self,                             331 //!             self,
352 //!             slot: *mut Bar<u32>,              332 //!             slot: *mut Bar<u32>,
                                                   >> 333 //!             // Note that this is `PinInit` instead of `Init`, this is because `b` is
                                                   >> 334 //!             // structurally pinned, as marked by the `#[pin]` attribute.
353 //!             init: impl ::kernel::init::Pin    335 //!             init: impl ::kernel::init::PinInit<Bar<u32>, E>,
354 //!         ) -> ::core::result::Result<(), E>    336 //!         ) -> ::core::result::Result<(), E> {
355 //!             unsafe { ::kernel::init::PinIn    337 //!             unsafe { ::kernel::init::PinInit::__pinned_init(init, slot) }
356 //!         }                                     338 //!         }
357 //!         unsafe fn a<E>(                       339 //!         unsafe fn a<E>(
358 //!             self,                             340 //!             self,
359 //!             slot: *mut usize,                 341 //!             slot: *mut usize,
360 //!             init: impl ::kernel::init::Ini    342 //!             init: impl ::kernel::init::Init<usize, E>,
361 //!         ) -> ::core::result::Result<(), E>    343 //!         ) -> ::core::result::Result<(), E> {
362 //!             unsafe { ::kernel::init::Init:    344 //!             unsafe { ::kernel::init::Init::__init(init, slot) }
363 //!         }                                     345 //!         }
364 //!     }                                         346 //!     }
365 //!     unsafe impl ::kernel::init::__internal    347 //!     unsafe impl ::kernel::init::__internal::HasPinData for Foo {
366 //!         type PinData = __ThePinData;          348 //!         type PinData = __ThePinData;
367 //!         unsafe fn __pin_data() -> Self::Pi    349 //!         unsafe fn __pin_data() -> Self::PinData {
368 //!             __ThePinData {                    350 //!             __ThePinData {
369 //!                 __phantom: ::core::marker:    351 //!                 __phantom: ::core::marker::PhantomData,
370 //!             }                                 352 //!             }
371 //!         }                                     353 //!         }
372 //!     }                                         354 //!     }
373 //!     unsafe impl ::kernel::init::__internal    355 //!     unsafe impl ::kernel::init::__internal::PinData for __ThePinData {
374 //!         type Datee = Foo;                     356 //!         type Datee = Foo;
375 //!     }                                         357 //!     }
376 //!     #[allow(dead_code)]                       358 //!     #[allow(dead_code)]
377 //!     struct __Unpin<'__pin> {                  359 //!     struct __Unpin<'__pin> {
378 //!         __phantom_pin: ::core::marker::Pha    360 //!         __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
379 //!         __phantom: ::core::marker::Phantom    361 //!         __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
                                                   >> 362 //!         // Since this field is `#[pin]`, it is listed here.
380 //!         b: Bar<u32>,                          363 //!         b: Bar<u32>,
381 //!     }                                         364 //!     }
382 //!     #[doc(hidden)]                            365 //!     #[doc(hidden)]
383 //!     impl<'__pin> ::core::marker::Unpin for !! 366 //!     impl<'__pin> ::core::marker::Unpin for Foo where __Unpin<'__pin>: ::core::marker::Unpin {}
384 //!     where                                  << 
385 //!         __Unpin<'__pin>: ::core::marker::U << 
386 //!     {}                                     << 
387 //!     // Since we specified `PinnedDrop` as     367 //!     // Since we specified `PinnedDrop` as the argument to `#[pin_data]`, we expect `Foo` to
388 //!     // implement `PinnedDrop`. Thus we do     368 //!     // implement `PinnedDrop`. Thus we do not need to prevent `Drop` implementations like
389 //!     // before, instead we implement `Drop` !! 369 //!     // before, instead we implement it here and delegate to `PinnedDrop`.
390 //!     impl ::core::ops::Drop for Foo {          370 //!     impl ::core::ops::Drop for Foo {
391 //!         fn drop(&mut self) {                  371 //!         fn drop(&mut self) {
392 //!             // Since we are getting droppe    372 //!             // Since we are getting dropped, no one else has a reference to `self` and thus we
393 //!             // can assume that we never mo    373 //!             // can assume that we never move.
394 //!             let pinned = unsafe { ::core::    374 //!             let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
395 //!             // Create the unsafe token tha    375 //!             // Create the unsafe token that proves that we are inside of a destructor, this
396 //!             // type is only allowed to be     376 //!             // type is only allowed to be created in a destructor.
397 //!             let token = unsafe { ::kernel:    377 //!             let token = unsafe { ::kernel::init::__internal::OnlyCallFromDrop::new() };
398 //!             ::kernel::init::PinnedDrop::dr    378 //!             ::kernel::init::PinnedDrop::drop(pinned, token);
399 //!         }                                     379 //!         }
400 //!     }                                         380 //!     }
401 //! };                                            381 //! };
402 //! ```                                           382 //! ```
403 //!                                               383 //!
404 //! ## `#[pinned_drop]` on `impl PinnedDrop fo    384 //! ## `#[pinned_drop]` on `impl PinnedDrop for Foo`
405 //!                                               385 //!
406 //! This macro is used to implement the `Pinne    386 //! This macro is used to implement the `PinnedDrop` trait, since that trait is `unsafe` and has an
407 //! extra parameter that should not be used at    387 //! extra parameter that should not be used at all. The macro hides that parameter.
408 //!                                               388 //!
409 //! Here is the `PinnedDrop` impl for `Foo`:      389 //! Here is the `PinnedDrop` impl for `Foo`:
410 //!                                               390 //!
411 //! ```rust,ignore                             !! 391 //! ```rust
412 //! #[pinned_drop]                                392 //! #[pinned_drop]
413 //! impl PinnedDrop for Foo {                     393 //! impl PinnedDrop for Foo {
414 //!     fn drop(self: Pin<&mut Self>) {           394 //!     fn drop(self: Pin<&mut Self>) {
415 //!         pr_info!("{self:p} is getting drop !! 395 //!         println!("{self:p} is getting dropped.");
416 //!     }                                         396 //!     }
417 //! }                                             397 //! }
418 //! ```                                           398 //! ```
419 //!                                               399 //!
420 //! This expands to the following code:           400 //! This expands to the following code:
421 //!                                               401 //!
422 //! ```rust,ignore                             !! 402 //! ```rust
423 //! // `unsafe`, full path and the token param    403 //! // `unsafe`, full path and the token parameter are added, everything else stays the same.
424 //! unsafe impl ::kernel::init::PinnedDrop for    404 //! unsafe impl ::kernel::init::PinnedDrop for Foo {
425 //!     fn drop(self: Pin<&mut Self>, _: ::ker    405 //!     fn drop(self: Pin<&mut Self>, _: ::kernel::init::__internal::OnlyCallFromDrop) {
426 //!         pr_info!("{self:p} is getting drop !! 406 //!         println!("{self:p} is getting dropped.");
427 //!     }                                         407 //!     }
428 //! }                                             408 //! }
429 //! ```                                           409 //! ```
430 //!                                               410 //!
431 //! ## `pin_init!` on `Foo`                       411 //! ## `pin_init!` on `Foo`
432 //!                                               412 //!
433 //! Since we already took a look at `pin_init! !! 413 //! Since we already took a look at `pin_init!` on `Bar`, this section will only explain the
434 //! of `pin_init!` on `Foo`:                   !! 414 //! differences/new things in the expansion of `pin_init!` on `Foo`:
435 //!                                               415 //!
436 //! ```rust,ignore                             !! 416 //! ```rust
437 //! let a = 42;                                   417 //! let a = 42;
438 //! let initializer = pin_init!(Foo {             418 //! let initializer = pin_init!(Foo {
439 //!     a,                                        419 //!     a,
440 //!     b <- Bar::new(36),                        420 //!     b <- Bar::new(36),
441 //! });                                           421 //! });
442 //! ```                                           422 //! ```
443 //!                                               423 //!
444 //! This expands to the following code:           424 //! This expands to the following code:
445 //!                                               425 //!
446 //! ```rust,ignore                             !! 426 //! ```rust
447 //! let a = 42;                                   427 //! let a = 42;
448 //! let initializer = {                           428 //! let initializer = {
449 //!     struct __InitOk;                          429 //!     struct __InitOk;
450 //!     let data = unsafe {                       430 //!     let data = unsafe {
451 //!         use ::kernel::init::__internal::Ha    431 //!         use ::kernel::init::__internal::HasPinData;
452 //!         Foo::__pin_data()                     432 //!         Foo::__pin_data()
453 //!     };                                        433 //!     };
454 //!     let init = ::kernel::init::__internal:    434 //!     let init = ::kernel::init::__internal::PinData::make_closure::<
455 //!         _,                                    435 //!         _,
456 //!         __InitOk,                             436 //!         __InitOk,
457 //!         ::core::convert::Infallible,          437 //!         ::core::convert::Infallible,
458 //!     >(data, move |slot| {                     438 //!     >(data, move |slot| {
459 //!         {                                     439 //!         {
460 //!             struct __InitOk;                  440 //!             struct __InitOk;
461 //!             {                              !! 441 //!             unsafe { ::core::ptr::write(&raw mut (*slot).a, a) };
462 //!                 unsafe { ::core::ptr::writ !! 442 //!             let a = &unsafe { ::kernel::init::__internal::DropGuard::new(&raw mut (*slot).a) };
463 //!             }                              !! 443 //!             let b = Bar::new(36);
464 //!             let __a_guard = unsafe {       !! 444 //!             // Here we use `data` to access the correct field and require that `b` is of type
465 //!                 ::kernel::init::__internal !! 445 //!             // `PinInit<Bar<u32>, Infallible>`.
466 //!             };                             !! 446 //!             unsafe { data.b(&raw mut (*slot).b, b)? };
467 //!             let init = Bar::new(36);       !! 447 //!             let b = &unsafe { ::kernel::init::__internal::DropGuard::new(&raw mut (*slot).b) };
468 //!             unsafe { data.b(::core::addr_o !! 448 //!
469 //!             let __b_guard = unsafe {       << 
470 //!                 ::kernel::init::__internal << 
471 //!             };                             << 
472 //!             ::core::mem::forget(__b_guard) << 
473 //!             ::core::mem::forget(__a_guard) << 
474 //!             #[allow(unreachable_code, clip    449 //!             #[allow(unreachable_code, clippy::diverging_sub_expression)]
475 //!             let _ = || {                   !! 450 //!             if false {
476 //!                 unsafe {                      451 //!                 unsafe {
477 //!                     ::core::ptr::write(       452 //!                     ::core::ptr::write(
478 //!                         slot,                 453 //!                         slot,
479 //!                         Foo {                 454 //!                         Foo {
480 //!                             a: ::core::pan    455 //!                             a: ::core::panic!(),
481 //!                             b: ::core::pan    456 //!                             b: ::core::panic!(),
482 //!                         },                    457 //!                         },
483 //!                     );                        458 //!                     );
484 //!                 };                            459 //!                 };
485 //!             };                             !! 460 //!             }
                                                   >> 461 //!             unsafe { ::kernel::init::__internal::DropGuard::forget(a) };
                                                   >> 462 //!             unsafe { ::kernel::init::__internal::DropGuard::forget(b) };
486 //!         }                                     463 //!         }
487 //!         Ok(__InitOk)                          464 //!         Ok(__InitOk)
488 //!     });                                       465 //!     });
489 //!     let init = move |                      !! 466 //!     let init = move |slot| -> ::core::result::Result<(), ::core::convert::Infallible> {
490 //!         slot,                              << 
491 //!     | -> ::core::result::Result<(), ::core << 
492 //!         init(slot).map(|__InitOk| ())         467 //!         init(slot).map(|__InitOk| ())
493 //!     };                                        468 //!     };
494 //!     let init = unsafe {                       469 //!     let init = unsafe {
495 //!         ::kernel::init::pin_init_from_clos    470 //!         ::kernel::init::pin_init_from_closure::<_, ::core::convert::Infallible>(init)
496 //!     };                                        471 //!     };
497 //!     init                                      472 //!     init
498 //! };                                            473 //! };
499 //! ```                                           474 //! ```
500                                                   475 
501 /// Creates a `unsafe impl<...> PinnedDrop for    476 /// Creates a `unsafe impl<...> PinnedDrop for $type` block.
502 ///                                               477 ///
503 /// See [`PinnedDrop`] for more information.      478 /// See [`PinnedDrop`] for more information.
504 #[doc(hidden)]                                    479 #[doc(hidden)]
505 #[macro_export]                                   480 #[macro_export]
506 macro_rules! __pinned_drop {                      481 macro_rules! __pinned_drop {
507     (                                             482     (
508         @impl_sig($($impl_sig:tt)*),              483         @impl_sig($($impl_sig:tt)*),
509         @impl_body(                               484         @impl_body(
510             $(#[$($attr:tt)*])*                   485             $(#[$($attr:tt)*])*
511             fn drop($($sig:tt)*) {                486             fn drop($($sig:tt)*) {
512                 $($inner:tt)*                     487                 $($inner:tt)*
513             }                                     488             }
514         ),                                        489         ),
515     ) => {                                        490     ) => {
516         unsafe $($impl_sig)* {                    491         unsafe $($impl_sig)* {
517             // Inherit all attributes and the     492             // Inherit all attributes and the type/ident tokens for the signature.
518             $(#[$($attr)*])*                      493             $(#[$($attr)*])*
519             fn drop($($sig)*, _: $crate::init:    494             fn drop($($sig)*, _: $crate::init::__internal::OnlyCallFromDrop) {
520                 $($inner)*                        495                 $($inner)*
521             }                                     496             }
522         }                                         497         }
523     }                                             498     }
524 }                                                 499 }
525                                                   500 
526 /// This macro first parses the struct definit    501 /// This macro first parses the struct definition such that it separates pinned and not pinned
527 /// fields. Afterwards it declares the struct     502 /// fields. Afterwards it declares the struct and implement the `PinData` trait safely.
528 #[doc(hidden)]                                    503 #[doc(hidden)]
529 #[macro_export]                                   504 #[macro_export]
530 macro_rules! __pin_data {                         505 macro_rules! __pin_data {
531     // Proc-macro entry point, this is supplie    506     // Proc-macro entry point, this is supplied by the proc-macro pre-parsing.
532     (parse_input:                                 507     (parse_input:
533         @args($($pinned_drop:ident)?),            508         @args($($pinned_drop:ident)?),
534         @sig(                                     509         @sig(
535             $(#[$($struct_attr:tt)*])*            510             $(#[$($struct_attr:tt)*])*
536             $vis:vis struct $name:ident           511             $vis:vis struct $name:ident
537             $(where $($whr:tt)*)?                 512             $(where $($whr:tt)*)?
538         ),                                        513         ),
539         @impl_generics($($impl_generics:tt)*),    514         @impl_generics($($impl_generics:tt)*),
540         @ty_generics($($ty_generics:tt)*),        515         @ty_generics($($ty_generics:tt)*),
541         @decl_generics($($decl_generics:tt)*), << 
542         @body({ $($fields:tt)* }),                516         @body({ $($fields:tt)* }),
543     ) => {                                        517     ) => {
544         // We now use token munching to iterat    518         // We now use token munching to iterate through all of the fields. While doing this we
545         // identify fields marked with `#[pin]    519         // identify fields marked with `#[pin]`, these fields are the 'pinned fields'. The user
546         // wants these to be structurally pinn    520         // wants these to be structurally pinned. The rest of the fields are the
547         // 'not pinned fields'. Additionally w    521         // 'not pinned fields'. Additionally we collect all fields, since we need them in the right
548         // order to declare the struct.           522         // order to declare the struct.
549         //                                        523         //
550         // In this call we also put some expla    524         // In this call we also put some explaining comments for the parameters.
551         $crate::__pin_data!(find_pinned_fields    525         $crate::__pin_data!(find_pinned_fields:
552             // Attributes on the struct itself    526             // Attributes on the struct itself, these will just be propagated to be put onto the
553             // struct definition.                 527             // struct definition.
554             @struct_attrs($(#[$($struct_attr)*    528             @struct_attrs($(#[$($struct_attr)*])*),
555             // The visibility of the struct.      529             // The visibility of the struct.
556             @vis($vis),                           530             @vis($vis),
557             // The name of the struct.            531             // The name of the struct.
558             @name($name),                         532             @name($name),
559             // The 'impl generics', the generi    533             // The 'impl generics', the generics that will need to be specified on the struct inside
560             // of an `impl<$ty_generics>` bloc    534             // of an `impl<$ty_generics>` block.
561             @impl_generics($($impl_generics)*)    535             @impl_generics($($impl_generics)*),
562             // The 'ty generics', the generics    536             // The 'ty generics', the generics that will need to be specified on the impl blocks.
563             @ty_generics($($ty_generics)*),       537             @ty_generics($($ty_generics)*),
564             // The 'decl generics', the generi << 
565             // definition.                     << 
566             @decl_generics($($decl_generics)*) << 
567             // The where clause of any impl bl    538             // The where clause of any impl block and the declaration.
568             @where($($($whr)*)?),                 539             @where($($($whr)*)?),
569             // The remaining fields tokens tha    540             // The remaining fields tokens that need to be processed.
570             // We add a `,` at the end to ensu    541             // We add a `,` at the end to ensure correct parsing.
571             @fields_munch($($fields)* ,),         542             @fields_munch($($fields)* ,),
572             // The pinned fields.                 543             // The pinned fields.
573             @pinned(),                            544             @pinned(),
574             // The not pinned fields.             545             // The not pinned fields.
575             @not_pinned(),                        546             @not_pinned(),
576             // All fields.                        547             // All fields.
577             @fields(),                            548             @fields(),
578             // The accumulator containing all     549             // The accumulator containing all attributes already parsed.
579             @accum(),                             550             @accum(),
580             // Contains `yes` or `` to indicat    551             // Contains `yes` or `` to indicate if `#[pin]` was found on the current field.
581             @is_pinned(),                         552             @is_pinned(),
582             // The proc-macro argument, this s    553             // The proc-macro argument, this should be `PinnedDrop` or ``.
583             @pinned_drop($($pinned_drop)?),       554             @pinned_drop($($pinned_drop)?),
584         );                                        555         );
585     };                                            556     };
586     (find_pinned_fields:                          557     (find_pinned_fields:
587         @struct_attrs($($struct_attrs:tt)*),      558         @struct_attrs($($struct_attrs:tt)*),
588         @vis($vis:vis),                           559         @vis($vis:vis),
589         @name($name:ident),                       560         @name($name:ident),
590         @impl_generics($($impl_generics:tt)*),    561         @impl_generics($($impl_generics:tt)*),
591         @ty_generics($($ty_generics:tt)*),        562         @ty_generics($($ty_generics:tt)*),
592         @decl_generics($($decl_generics:tt)*), << 
593         @where($($whr:tt)*),                      563         @where($($whr:tt)*),
594         // We found a PhantomPinned field, thi    564         // We found a PhantomPinned field, this should generally be pinned!
595         @fields_munch($field:ident : $($($(::)    565         @fields_munch($field:ident : $($($(::)?core::)?marker::)?PhantomPinned, $($rest:tt)*),
596         @pinned($($pinned:tt)*),                  566         @pinned($($pinned:tt)*),
597         @not_pinned($($not_pinned:tt)*),          567         @not_pinned($($not_pinned:tt)*),
598         @fields($($fields:tt)*),                  568         @fields($($fields:tt)*),
599         @accum($($accum:tt)*),                    569         @accum($($accum:tt)*),
600         // This field is not pinned.              570         // This field is not pinned.
601         @is_pinned(),                             571         @is_pinned(),
602         @pinned_drop($($pinned_drop:ident)?),     572         @pinned_drop($($pinned_drop:ident)?),
603     ) => {                                        573     ) => {
604         ::core::compile_error!(concat!(           574         ::core::compile_error!(concat!(
605             "The field `",                        575             "The field `",
606             stringify!($field),                   576             stringify!($field),
607             "` of type `PhantomPinned` only ha    577             "` of type `PhantomPinned` only has an effect, if it has the `#[pin]` attribute.",
608         ));                                       578         ));
609         $crate::__pin_data!(find_pinned_fields    579         $crate::__pin_data!(find_pinned_fields:
610             @struct_attrs($($struct_attrs)*),     580             @struct_attrs($($struct_attrs)*),
611             @vis($vis),                           581             @vis($vis),
612             @name($name),                         582             @name($name),
613             @impl_generics($($impl_generics)*)    583             @impl_generics($($impl_generics)*),
614             @ty_generics($($ty_generics)*),       584             @ty_generics($($ty_generics)*),
615             @decl_generics($($decl_generics)*) << 
616             @where($($whr)*),                     585             @where($($whr)*),
617             @fields_munch($($rest)*),             586             @fields_munch($($rest)*),
618             @pinned($($pinned)* $($accum)* $fi    587             @pinned($($pinned)* $($accum)* $field: ::core::marker::PhantomPinned,),
619             @not_pinned($($not_pinned)*),         588             @not_pinned($($not_pinned)*),
620             @fields($($fields)* $($accum)* $fi    589             @fields($($fields)* $($accum)* $field: ::core::marker::PhantomPinned,),
621             @accum(),                             590             @accum(),
622             @is_pinned(),                         591             @is_pinned(),
623             @pinned_drop($($pinned_drop)?),       592             @pinned_drop($($pinned_drop)?),
624         );                                        593         );
625     };                                            594     };
626     (find_pinned_fields:                          595     (find_pinned_fields:
627         @struct_attrs($($struct_attrs:tt)*),      596         @struct_attrs($($struct_attrs:tt)*),
628         @vis($vis:vis),                           597         @vis($vis:vis),
629         @name($name:ident),                       598         @name($name:ident),
630         @impl_generics($($impl_generics:tt)*),    599         @impl_generics($($impl_generics:tt)*),
631         @ty_generics($($ty_generics:tt)*),        600         @ty_generics($($ty_generics:tt)*),
632         @decl_generics($($decl_generics:tt)*), << 
633         @where($($whr:tt)*),                      601         @where($($whr:tt)*),
634         // We reached the field declaration.      602         // We reached the field declaration.
635         @fields_munch($field:ident : $type:ty,    603         @fields_munch($field:ident : $type:ty, $($rest:tt)*),
636         @pinned($($pinned:tt)*),                  604         @pinned($($pinned:tt)*),
637         @not_pinned($($not_pinned:tt)*),          605         @not_pinned($($not_pinned:tt)*),
638         @fields($($fields:tt)*),                  606         @fields($($fields:tt)*),
639         @accum($($accum:tt)*),                    607         @accum($($accum:tt)*),
640         // This field is pinned.                  608         // This field is pinned.
641         @is_pinned(yes),                          609         @is_pinned(yes),
642         @pinned_drop($($pinned_drop:ident)?),     610         @pinned_drop($($pinned_drop:ident)?),
643     ) => {                                        611     ) => {
644         $crate::__pin_data!(find_pinned_fields    612         $crate::__pin_data!(find_pinned_fields:
645             @struct_attrs($($struct_attrs)*),     613             @struct_attrs($($struct_attrs)*),
646             @vis($vis),                           614             @vis($vis),
647             @name($name),                         615             @name($name),
648             @impl_generics($($impl_generics)*)    616             @impl_generics($($impl_generics)*),
649             @ty_generics($($ty_generics)*),       617             @ty_generics($($ty_generics)*),
650             @decl_generics($($decl_generics)*) << 
651             @where($($whr)*),                     618             @where($($whr)*),
652             @fields_munch($($rest)*),             619             @fields_munch($($rest)*),
653             @pinned($($pinned)* $($accum)* $fi    620             @pinned($($pinned)* $($accum)* $field: $type,),
654             @not_pinned($($not_pinned)*),         621             @not_pinned($($not_pinned)*),
655             @fields($($fields)* $($accum)* $fi    622             @fields($($fields)* $($accum)* $field: $type,),
656             @accum(),                             623             @accum(),
657             @is_pinned(),                         624             @is_pinned(),
658             @pinned_drop($($pinned_drop)?),       625             @pinned_drop($($pinned_drop)?),
659         );                                        626         );
660     };                                            627     };
661     (find_pinned_fields:                          628     (find_pinned_fields:
662         @struct_attrs($($struct_attrs:tt)*),      629         @struct_attrs($($struct_attrs:tt)*),
663         @vis($vis:vis),                           630         @vis($vis:vis),
664         @name($name:ident),                       631         @name($name:ident),
665         @impl_generics($($impl_generics:tt)*),    632         @impl_generics($($impl_generics:tt)*),
666         @ty_generics($($ty_generics:tt)*),        633         @ty_generics($($ty_generics:tt)*),
667         @decl_generics($($decl_generics:tt)*), << 
668         @where($($whr:tt)*),                      634         @where($($whr:tt)*),
669         // We reached the field declaration.      635         // We reached the field declaration.
670         @fields_munch($field:ident : $type:ty,    636         @fields_munch($field:ident : $type:ty, $($rest:tt)*),
671         @pinned($($pinned:tt)*),                  637         @pinned($($pinned:tt)*),
672         @not_pinned($($not_pinned:tt)*),          638         @not_pinned($($not_pinned:tt)*),
673         @fields($($fields:tt)*),                  639         @fields($($fields:tt)*),
674         @accum($($accum:tt)*),                    640         @accum($($accum:tt)*),
675         // This field is not pinned.              641         // This field is not pinned.
676         @is_pinned(),                             642         @is_pinned(),
677         @pinned_drop($($pinned_drop:ident)?),     643         @pinned_drop($($pinned_drop:ident)?),
678     ) => {                                        644     ) => {
679         $crate::__pin_data!(find_pinned_fields    645         $crate::__pin_data!(find_pinned_fields:
680             @struct_attrs($($struct_attrs)*),     646             @struct_attrs($($struct_attrs)*),
681             @vis($vis),                           647             @vis($vis),
682             @name($name),                         648             @name($name),
683             @impl_generics($($impl_generics)*)    649             @impl_generics($($impl_generics)*),
684             @ty_generics($($ty_generics)*),       650             @ty_generics($($ty_generics)*),
685             @decl_generics($($decl_generics)*) << 
686             @where($($whr)*),                     651             @where($($whr)*),
687             @fields_munch($($rest)*),             652             @fields_munch($($rest)*),
688             @pinned($($pinned)*),                 653             @pinned($($pinned)*),
689             @not_pinned($($not_pinned)* $($acc    654             @not_pinned($($not_pinned)* $($accum)* $field: $type,),
690             @fields($($fields)* $($accum)* $fi    655             @fields($($fields)* $($accum)* $field: $type,),
691             @accum(),                             656             @accum(),
692             @is_pinned(),                         657             @is_pinned(),
693             @pinned_drop($($pinned_drop)?),       658             @pinned_drop($($pinned_drop)?),
694         );                                        659         );
695     };                                            660     };
696     (find_pinned_fields:                          661     (find_pinned_fields:
697         @struct_attrs($($struct_attrs:tt)*),      662         @struct_attrs($($struct_attrs:tt)*),
698         @vis($vis:vis),                           663         @vis($vis:vis),
699         @name($name:ident),                       664         @name($name:ident),
700         @impl_generics($($impl_generics:tt)*),    665         @impl_generics($($impl_generics:tt)*),
701         @ty_generics($($ty_generics:tt)*),        666         @ty_generics($($ty_generics:tt)*),
702         @decl_generics($($decl_generics:tt)*), << 
703         @where($($whr:tt)*),                      667         @where($($whr:tt)*),
704         // We found the `#[pin]` attr.            668         // We found the `#[pin]` attr.
705         @fields_munch(#[pin] $($rest:tt)*),       669         @fields_munch(#[pin] $($rest:tt)*),
706         @pinned($($pinned:tt)*),                  670         @pinned($($pinned:tt)*),
707         @not_pinned($($not_pinned:tt)*),          671         @not_pinned($($not_pinned:tt)*),
708         @fields($($fields:tt)*),                  672         @fields($($fields:tt)*),
709         @accum($($accum:tt)*),                    673         @accum($($accum:tt)*),
710         @is_pinned($($is_pinned:ident)?),         674         @is_pinned($($is_pinned:ident)?),
711         @pinned_drop($($pinned_drop:ident)?),     675         @pinned_drop($($pinned_drop:ident)?),
712     ) => {                                        676     ) => {
713         $crate::__pin_data!(find_pinned_fields    677         $crate::__pin_data!(find_pinned_fields:
714             @struct_attrs($($struct_attrs)*),     678             @struct_attrs($($struct_attrs)*),
715             @vis($vis),                           679             @vis($vis),
716             @name($name),                         680             @name($name),
717             @impl_generics($($impl_generics)*)    681             @impl_generics($($impl_generics)*),
718             @ty_generics($($ty_generics)*),       682             @ty_generics($($ty_generics)*),
719             @decl_generics($($decl_generics)*) << 
720             @where($($whr)*),                     683             @where($($whr)*),
721             @fields_munch($($rest)*),             684             @fields_munch($($rest)*),
722             // We do not include `#[pin]` in t    685             // We do not include `#[pin]` in the list of attributes, since it is not actually an
723             // attribute that is defined somew    686             // attribute that is defined somewhere.
724             @pinned($($pinned)*),                 687             @pinned($($pinned)*),
725             @not_pinned($($not_pinned)*),         688             @not_pinned($($not_pinned)*),
726             @fields($($fields)*),                 689             @fields($($fields)*),
727             @accum($($accum)*),                   690             @accum($($accum)*),
728             // Set this to `yes`.                 691             // Set this to `yes`.
729             @is_pinned(yes),                      692             @is_pinned(yes),
730             @pinned_drop($($pinned_drop)?),       693             @pinned_drop($($pinned_drop)?),
731         );                                        694         );
732     };                                            695     };
733     (find_pinned_fields:                          696     (find_pinned_fields:
734         @struct_attrs($($struct_attrs:tt)*),      697         @struct_attrs($($struct_attrs:tt)*),
735         @vis($vis:vis),                           698         @vis($vis:vis),
736         @name($name:ident),                       699         @name($name:ident),
737         @impl_generics($($impl_generics:tt)*),    700         @impl_generics($($impl_generics:tt)*),
738         @ty_generics($($ty_generics:tt)*),        701         @ty_generics($($ty_generics:tt)*),
739         @decl_generics($($decl_generics:tt)*), << 
740         @where($($whr:tt)*),                      702         @where($($whr:tt)*),
741         // We reached the field declaration wi    703         // We reached the field declaration with visibility, for simplicity we only munch the
742         // visibility and put it into `$accum`    704         // visibility and put it into `$accum`.
743         @fields_munch($fvis:vis $field:ident $    705         @fields_munch($fvis:vis $field:ident $($rest:tt)*),
744         @pinned($($pinned:tt)*),                  706         @pinned($($pinned:tt)*),
745         @not_pinned($($not_pinned:tt)*),          707         @not_pinned($($not_pinned:tt)*),
746         @fields($($fields:tt)*),                  708         @fields($($fields:tt)*),
747         @accum($($accum:tt)*),                    709         @accum($($accum:tt)*),
748         @is_pinned($($is_pinned:ident)?),         710         @is_pinned($($is_pinned:ident)?),
749         @pinned_drop($($pinned_drop:ident)?),     711         @pinned_drop($($pinned_drop:ident)?),
750     ) => {                                        712     ) => {
751         $crate::__pin_data!(find_pinned_fields    713         $crate::__pin_data!(find_pinned_fields:
752             @struct_attrs($($struct_attrs)*),     714             @struct_attrs($($struct_attrs)*),
753             @vis($vis),                           715             @vis($vis),
754             @name($name),                         716             @name($name),
755             @impl_generics($($impl_generics)*)    717             @impl_generics($($impl_generics)*),
756             @ty_generics($($ty_generics)*),       718             @ty_generics($($ty_generics)*),
757             @decl_generics($($decl_generics)*) << 
758             @where($($whr)*),                     719             @where($($whr)*),
759             @fields_munch($field $($rest)*),      720             @fields_munch($field $($rest)*),
760             @pinned($($pinned)*),                 721             @pinned($($pinned)*),
761             @not_pinned($($not_pinned)*),         722             @not_pinned($($not_pinned)*),
762             @fields($($fields)*),                 723             @fields($($fields)*),
763             @accum($($accum)* $fvis),             724             @accum($($accum)* $fvis),
764             @is_pinned($($is_pinned)?),           725             @is_pinned($($is_pinned)?),
765             @pinned_drop($($pinned_drop)?),       726             @pinned_drop($($pinned_drop)?),
766         );                                        727         );
767     };                                            728     };
768     (find_pinned_fields:                          729     (find_pinned_fields:
769         @struct_attrs($($struct_attrs:tt)*),      730         @struct_attrs($($struct_attrs:tt)*),
770         @vis($vis:vis),                           731         @vis($vis:vis),
771         @name($name:ident),                       732         @name($name:ident),
772         @impl_generics($($impl_generics:tt)*),    733         @impl_generics($($impl_generics:tt)*),
773         @ty_generics($($ty_generics:tt)*),        734         @ty_generics($($ty_generics:tt)*),
774         @decl_generics($($decl_generics:tt)*), << 
775         @where($($whr:tt)*),                      735         @where($($whr:tt)*),
776         // Some other attribute, just put it i    736         // Some other attribute, just put it into `$accum`.
777         @fields_munch(#[$($attr:tt)*] $($rest:    737         @fields_munch(#[$($attr:tt)*] $($rest:tt)*),
778         @pinned($($pinned:tt)*),                  738         @pinned($($pinned:tt)*),
779         @not_pinned($($not_pinned:tt)*),          739         @not_pinned($($not_pinned:tt)*),
780         @fields($($fields:tt)*),                  740         @fields($($fields:tt)*),
781         @accum($($accum:tt)*),                    741         @accum($($accum:tt)*),
782         @is_pinned($($is_pinned:ident)?),         742         @is_pinned($($is_pinned:ident)?),
783         @pinned_drop($($pinned_drop:ident)?),     743         @pinned_drop($($pinned_drop:ident)?),
784     ) => {                                        744     ) => {
785         $crate::__pin_data!(find_pinned_fields    745         $crate::__pin_data!(find_pinned_fields:
786             @struct_attrs($($struct_attrs)*),     746             @struct_attrs($($struct_attrs)*),
787             @vis($vis),                           747             @vis($vis),
788             @name($name),                         748             @name($name),
789             @impl_generics($($impl_generics)*)    749             @impl_generics($($impl_generics)*),
790             @ty_generics($($ty_generics)*),       750             @ty_generics($($ty_generics)*),
791             @decl_generics($($decl_generics)*) << 
792             @where($($whr)*),                     751             @where($($whr)*),
793             @fields_munch($($rest)*),             752             @fields_munch($($rest)*),
794             @pinned($($pinned)*),                 753             @pinned($($pinned)*),
795             @not_pinned($($not_pinned)*),         754             @not_pinned($($not_pinned)*),
796             @fields($($fields)*),                 755             @fields($($fields)*),
797             @accum($($accum)* #[$($attr)*]),      756             @accum($($accum)* #[$($attr)*]),
798             @is_pinned($($is_pinned)?),           757             @is_pinned($($is_pinned)?),
799             @pinned_drop($($pinned_drop)?),       758             @pinned_drop($($pinned_drop)?),
800         );                                        759         );
801     };                                            760     };
802     (find_pinned_fields:                          761     (find_pinned_fields:
803         @struct_attrs($($struct_attrs:tt)*),      762         @struct_attrs($($struct_attrs:tt)*),
804         @vis($vis:vis),                           763         @vis($vis:vis),
805         @name($name:ident),                       764         @name($name:ident),
806         @impl_generics($($impl_generics:tt)*),    765         @impl_generics($($impl_generics:tt)*),
807         @ty_generics($($ty_generics:tt)*),        766         @ty_generics($($ty_generics:tt)*),
808         @decl_generics($($decl_generics:tt)*), << 
809         @where($($whr:tt)*),                      767         @where($($whr:tt)*),
810         // We reached the end of the fields, p    768         // We reached the end of the fields, plus an optional additional comma, since we added one
811         // before and the user is also allowed    769         // before and the user is also allowed to put a trailing comma.
812         @fields_munch($(,)?),                     770         @fields_munch($(,)?),
813         @pinned($($pinned:tt)*),                  771         @pinned($($pinned:tt)*),
814         @not_pinned($($not_pinned:tt)*),          772         @not_pinned($($not_pinned:tt)*),
815         @fields($($fields:tt)*),                  773         @fields($($fields:tt)*),
816         @accum(),                                 774         @accum(),
817         @is_pinned(),                             775         @is_pinned(),
818         @pinned_drop($($pinned_drop:ident)?),     776         @pinned_drop($($pinned_drop:ident)?),
819     ) => {                                        777     ) => {
820         // Declare the struct with all fields     778         // Declare the struct with all fields in the correct order.
821         $($struct_attrs)*                         779         $($struct_attrs)*
822         $vis struct $name <$($decl_generics)*> !! 780         $vis struct $name <$($impl_generics)*>
823         where $($whr)*                            781         where $($whr)*
824         {                                         782         {
825             $($fields)*                           783             $($fields)*
826         }                                         784         }
827                                                   785 
828         // We put the rest into this const ite    786         // We put the rest into this const item, because it then will not be accessible to anything
829         // outside.                               787         // outside.
830         const _: () = {                           788         const _: () = {
831             // We declare this struct which wi    789             // We declare this struct which will host all of the projection function for our type.
832             // it will be invariant over all g    790             // it will be invariant over all generic parameters which are inherited from the
833             // struct.                            791             // struct.
834             $vis struct __ThePinData<$($impl_g    792             $vis struct __ThePinData<$($impl_generics)*>
835             where $($whr)*                        793             where $($whr)*
836             {                                     794             {
837                 __phantom: ::core::marker::Pha    795                 __phantom: ::core::marker::PhantomData<
838                     fn($name<$($ty_generics)*>    796                     fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
839                 >,                                797                 >,
840             }                                     798             }
841                                                   799 
842             impl<$($impl_generics)*> ::core::c    800             impl<$($impl_generics)*> ::core::clone::Clone for __ThePinData<$($ty_generics)*>
843             where $($whr)*                        801             where $($whr)*
844             {                                     802             {
845                 fn clone(&self) -> Self { *sel    803                 fn clone(&self) -> Self { *self }
846             }                                     804             }
847                                                   805 
848             impl<$($impl_generics)*> ::core::m    806             impl<$($impl_generics)*> ::core::marker::Copy for __ThePinData<$($ty_generics)*>
849             where $($whr)*                        807             where $($whr)*
850             {}                                    808             {}
851                                                   809 
852             // Make all projection functions.     810             // Make all projection functions.
853             $crate::__pin_data!(make_pin_data:    811             $crate::__pin_data!(make_pin_data:
854                 @pin_data(__ThePinData),          812                 @pin_data(__ThePinData),
855                 @impl_generics($($impl_generic    813                 @impl_generics($($impl_generics)*),
856                 @ty_generics($($ty_generics)*)    814                 @ty_generics($($ty_generics)*),
857                 @where($($whr)*),                 815                 @where($($whr)*),
858                 @pinned($($pinned)*),             816                 @pinned($($pinned)*),
859                 @not_pinned($($not_pinned)*),     817                 @not_pinned($($not_pinned)*),
860             );                                    818             );
861                                                   819 
862             // SAFETY: We have added the corre    820             // SAFETY: We have added the correct projection functions above to `__ThePinData` and
863             // we also use the least restricti    821             // we also use the least restrictive generics possible.
864             unsafe impl<$($impl_generics)*>       822             unsafe impl<$($impl_generics)*>
865                 $crate::init::__internal::HasP    823                 $crate::init::__internal::HasPinData for $name<$($ty_generics)*>
866             where $($whr)*                        824             where $($whr)*
867             {                                     825             {
868                 type PinData = __ThePinData<$(    826                 type PinData = __ThePinData<$($ty_generics)*>;
869                                                   827 
870                 unsafe fn __pin_data() -> Self    828                 unsafe fn __pin_data() -> Self::PinData {
871                     __ThePinData { __phantom:     829                     __ThePinData { __phantom: ::core::marker::PhantomData }
872                 }                                 830                 }
873             }                                     831             }
874                                                   832 
875             unsafe impl<$($impl_generics)*>       833             unsafe impl<$($impl_generics)*>
876                 $crate::init::__internal::PinD    834                 $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>
877             where $($whr)*                        835             where $($whr)*
878             {                                     836             {
879                 type Datee = $name<$($ty_gener    837                 type Datee = $name<$($ty_generics)*>;
880             }                                     838             }
881                                                   839 
882             // This struct will be used for th    840             // This struct will be used for the unpin analysis. Since only structurally pinned
883             // fields are relevant whether the    841             // fields are relevant whether the struct should implement `Unpin`.
884             #[allow(dead_code)]                   842             #[allow(dead_code)]
885             struct __Unpin <'__pin, $($impl_ge    843             struct __Unpin <'__pin, $($impl_generics)*>
886             where $($whr)*                        844             where $($whr)*
887             {                                     845             {
888                 __phantom_pin: ::core::marker:    846                 __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
889                 __phantom: ::core::marker::Pha    847                 __phantom: ::core::marker::PhantomData<
890                     fn($name<$($ty_generics)*>    848                     fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
891                 >,                                849                 >,
892                 // Only the pinned fields.        850                 // Only the pinned fields.
893                 $($pinned)*                       851                 $($pinned)*
894             }                                     852             }
895                                                   853 
896             #[doc(hidden)]                        854             #[doc(hidden)]
897             impl<'__pin, $($impl_generics)*> :    855             impl<'__pin, $($impl_generics)*> ::core::marker::Unpin for $name<$($ty_generics)*>
898             where                                 856             where
899                 __Unpin<'__pin, $($ty_generics    857                 __Unpin<'__pin, $($ty_generics)*>: ::core::marker::Unpin,
900                 $($whr)*                          858                 $($whr)*
901             {}                                    859             {}
902                                                   860 
903             // We need to disallow normal `Dro    861             // We need to disallow normal `Drop` implementation, the exact behavior depends on
904             // whether `PinnedDrop` was specif    862             // whether `PinnedDrop` was specified as the parameter.
905             $crate::__pin_data!(drop_preventio    863             $crate::__pin_data!(drop_prevention:
906                 @name($name),                     864                 @name($name),
907                 @impl_generics($($impl_generic    865                 @impl_generics($($impl_generics)*),
908                 @ty_generics($($ty_generics)*)    866                 @ty_generics($($ty_generics)*),
909                 @where($($whr)*),                 867                 @where($($whr)*),
910                 @pinned_drop($($pinned_drop)?)    868                 @pinned_drop($($pinned_drop)?),
911             );                                    869             );
912         };                                        870         };
913     };                                            871     };
914     // When no `PinnedDrop` was specified, the    872     // When no `PinnedDrop` was specified, then we have to prevent implementing drop.
915     (drop_prevention:                             873     (drop_prevention:
916         @name($name:ident),                       874         @name($name:ident),
917         @impl_generics($($impl_generics:tt)*),    875         @impl_generics($($impl_generics:tt)*),
918         @ty_generics($($ty_generics:tt)*),        876         @ty_generics($($ty_generics:tt)*),
919         @where($($whr:tt)*),                      877         @where($($whr:tt)*),
920         @pinned_drop(),                           878         @pinned_drop(),
921     ) => {                                        879     ) => {
922         // We prevent this by creating a trait    880         // We prevent this by creating a trait that will be implemented for all types implementing
923         // `Drop`. Additionally we will implem    881         // `Drop`. Additionally we will implement this trait for the struct leading to a conflict,
924         // if it also implements `Drop`           882         // if it also implements `Drop`
925         trait MustNotImplDrop {}                  883         trait MustNotImplDrop {}
926         #[allow(drop_bounds)]                     884         #[allow(drop_bounds)]
927         impl<T: ::core::ops::Drop> MustNotImpl    885         impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
928         impl<$($impl_generics)*> MustNotImplDr    886         impl<$($impl_generics)*> MustNotImplDrop for $name<$($ty_generics)*>
929         where $($whr)* {}                         887         where $($whr)* {}
930         // We also take care to prevent users     888         // We also take care to prevent users from writing a useless `PinnedDrop` implementation.
931         // They might implement `PinnedDrop` c    889         // They might implement `PinnedDrop` correctly for the struct, but forget to give
932         // `PinnedDrop` as the parameter to `#    890         // `PinnedDrop` as the parameter to `#[pin_data]`.
933         #[allow(non_camel_case_types)]            891         #[allow(non_camel_case_types)]
934         trait UselessPinnedDropImpl_you_need_t    892         trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
935         impl<T: $crate::init::PinnedDrop>         893         impl<T: $crate::init::PinnedDrop>
936             UselessPinnedDropImpl_you_need_to_    894             UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
937         impl<$($impl_generics)*>                  895         impl<$($impl_generics)*>
938             UselessPinnedDropImpl_you_need_to_    896             UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for $name<$($ty_generics)*>
939         where $($whr)* {}                         897         where $($whr)* {}
940     };                                            898     };
941     // When `PinnedDrop` was specified we just    899     // When `PinnedDrop` was specified we just implement `Drop` and delegate.
942     (drop_prevention:                             900     (drop_prevention:
943         @name($name:ident),                       901         @name($name:ident),
944         @impl_generics($($impl_generics:tt)*),    902         @impl_generics($($impl_generics:tt)*),
945         @ty_generics($($ty_generics:tt)*),        903         @ty_generics($($ty_generics:tt)*),
946         @where($($whr:tt)*),                      904         @where($($whr:tt)*),
947         @pinned_drop(PinnedDrop),                 905         @pinned_drop(PinnedDrop),
948     ) => {                                        906     ) => {
949         impl<$($impl_generics)*> ::core::ops::    907         impl<$($impl_generics)*> ::core::ops::Drop for $name<$($ty_generics)*>
950         where $($whr)*                            908         where $($whr)*
951         {                                         909         {
952             fn drop(&mut self) {                  910             fn drop(&mut self) {
953                 // SAFETY: Since this is a des    911                 // SAFETY: Since this is a destructor, `self` will not move after this function
954                 // terminates, since it is ina    912                 // terminates, since it is inaccessible.
955                 let pinned = unsafe { ::core::    913                 let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
956                 // SAFETY: Since this is a dro    914                 // SAFETY: Since this is a drop function, we can create this token to call the
957                 // pinned destructor of this t    915                 // pinned destructor of this type.
958                 let token = unsafe { $crate::i    916                 let token = unsafe { $crate::init::__internal::OnlyCallFromDrop::new() };
959                 $crate::init::PinnedDrop::drop    917                 $crate::init::PinnedDrop::drop(pinned, token);
960             }                                     918             }
961         }                                         919         }
962     };                                            920     };
963     // If some other parameter was specified,     921     // If some other parameter was specified, we emit a readable error.
964     (drop_prevention:                             922     (drop_prevention:
965         @name($name:ident),                       923         @name($name:ident),
966         @impl_generics($($impl_generics:tt)*),    924         @impl_generics($($impl_generics:tt)*),
967         @ty_generics($($ty_generics:tt)*),        925         @ty_generics($($ty_generics:tt)*),
968         @where($($whr:tt)*),                      926         @where($($whr:tt)*),
969         @pinned_drop($($rest:tt)*),               927         @pinned_drop($($rest:tt)*),
970     ) => {                                        928     ) => {
971         compile_error!(                           929         compile_error!(
972             "Wrong parameters to `#[pin_data]`    930             "Wrong parameters to `#[pin_data]`, expected nothing or `PinnedDrop`, got '{}'.",
973             stringify!($($rest)*),                931             stringify!($($rest)*),
974         );                                        932         );
975     };                                            933     };
976     (make_pin_data:                               934     (make_pin_data:
977         @pin_data($pin_data:ident),               935         @pin_data($pin_data:ident),
978         @impl_generics($($impl_generics:tt)*),    936         @impl_generics($($impl_generics:tt)*),
979         @ty_generics($($ty_generics:tt)*),        937         @ty_generics($($ty_generics:tt)*),
980         @where($($whr:tt)*),                      938         @where($($whr:tt)*),
981         @pinned($($(#[$($p_attr:tt)*])* $pvis:    939         @pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
982         @not_pinned($($(#[$($attr:tt)*])* $fvi    940         @not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
983     ) => {                                        941     ) => {
984         // For every field, we create a projec    942         // For every field, we create a projection function according to its projection type. If a
985         // field is structurally pinned, then     943         // field is structurally pinned, then it must be initialized via `PinInit`, if it is not
986         // structurally pinned, then it can be    944         // structurally pinned, then it can be initialized via `Init`.
987         //                                        945         //
988         // The functions are `unsafe` to preve    946         // The functions are `unsafe` to prevent accidentally calling them.
989         #[allow(dead_code)]                       947         #[allow(dead_code)]
990         impl<$($impl_generics)*> $pin_data<$($    948         impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
991         where $($whr)*                            949         where $($whr)*
992         {                                         950         {
993             $(                                    951             $(
994                 $(#[$($p_attr)*])*             << 
995                 $pvis unsafe fn $p_field<E>(      952                 $pvis unsafe fn $p_field<E>(
996                     self,                         953                     self,
997                     slot: *mut $p_type,           954                     slot: *mut $p_type,
998                     init: impl $crate::init::P    955                     init: impl $crate::init::PinInit<$p_type, E>,
999                 ) -> ::core::result::Result<()    956                 ) -> ::core::result::Result<(), E> {
1000                     unsafe { $crate::init::Pi    957                     unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
1001                 }                                958                 }
1002             )*                                   959             )*
1003             $(                                   960             $(
1004                 $(#[$($attr)*])*              << 
1005                 $fvis unsafe fn $field<E>(       961                 $fvis unsafe fn $field<E>(
1006                     self,                        962                     self,
1007                     slot: *mut $type,            963                     slot: *mut $type,
1008                     init: impl $crate::init::    964                     init: impl $crate::init::Init<$type, E>,
1009                 ) -> ::core::result::Result<(    965                 ) -> ::core::result::Result<(), E> {
1010                     unsafe { $crate::init::In    966                     unsafe { $crate::init::Init::__init(init, slot) }
1011                 }                                967                 }
1012             )*                                   968             )*
1013         }                                        969         }
1014     };                                        << 
1015 }                                             << 
1016                                               << 
1017 /// The internal init macro. Do not call manu << 
1018 ///                                           << 
1019 /// This is called by the `{try_}{pin_}init!` << 
1020 ///                                           << 
1021 /// This macro has multiple internal call con << 
1022 /// - nothing: this is the base case and call << 
1023 /// - `with_update_parsed`: when the `..Zeroa << 
1024 /// - `init_slot`: recursively creates the co << 
1025 /// - `make_initializer`: recursively create  << 
1026 ///   field has been initialized exactly once << 
1027 #[doc(hidden)]                                << 
1028 #[macro_export]                               << 
1029 macro_rules! __init_internal {                << 
1030     (                                         << 
1031         @this($($this:ident)?),               << 
1032         @typ($t:path),                        << 
1033         @fields($($fields:tt)*),              << 
1034         @error($err:ty),                      << 
1035         // Either `PinData` or `InitData`, `$ << 
1036         // case.                              << 
1037         @data($data:ident, $($use_data:ident) << 
1038         // `HasPinData` or `HasInitData`.     << 
1039         @has_data($has_data:ident, $get_data: << 
1040         // `pin_init_from_closure` or `init_f << 
1041         @construct_closure($construct_closure << 
1042         @munch_fields(),                      << 
1043     ) => {                                    << 
1044         $crate::__init_internal!(with_update_ << 
1045             @this($($this)?),                 << 
1046             @typ($t),                         << 
1047             @fields($($fields)*),             << 
1048             @error($err),                     << 
1049             @data($data, $($use_data)?),      << 
1050             @has_data($has_data, $get_data),  << 
1051             @construct_closure($construct_clo << 
1052             @zeroed(), // Nothing means defau << 
1053         )                                     << 
1054     };                                        << 
1055     (                                         << 
1056         @this($($this:ident)?),               << 
1057         @typ($t:path),                        << 
1058         @fields($($fields:tt)*),              << 
1059         @error($err:ty),                      << 
1060         // Either `PinData` or `InitData`, `$ << 
1061         // case.                              << 
1062         @data($data:ident, $($use_data:ident) << 
1063         // `HasPinData` or `HasInitData`.     << 
1064         @has_data($has_data:ident, $get_data: << 
1065         // `pin_init_from_closure` or `init_f << 
1066         @construct_closure($construct_closure << 
1067         @munch_fields(..Zeroable::zeroed()),  << 
1068     ) => {                                    << 
1069         $crate::__init_internal!(with_update_ << 
1070             @this($($this)?),                 << 
1071             @typ($t),                         << 
1072             @fields($($fields)*),             << 
1073             @error($err),                     << 
1074             @data($data, $($use_data)?),      << 
1075             @has_data($has_data, $get_data),  << 
1076             @construct_closure($construct_clo << 
1077             @zeroed(()), // `()` means zero a << 
1078         )                                     << 
1079     };                                        << 
1080     (                                         << 
1081         @this($($this:ident)?),               << 
1082         @typ($t:path),                        << 
1083         @fields($($fields:tt)*),              << 
1084         @error($err:ty),                      << 
1085         // Either `PinData` or `InitData`, `$ << 
1086         // case.                              << 
1087         @data($data:ident, $($use_data:ident) << 
1088         // `HasPinData` or `HasInitData`.     << 
1089         @has_data($has_data:ident, $get_data: << 
1090         // `pin_init_from_closure` or `init_f << 
1091         @construct_closure($construct_closure << 
1092         @munch_fields($ignore:tt $($rest:tt)* << 
1093     ) => {                                    << 
1094         $crate::__init_internal!(             << 
1095             @this($($this)?),                 << 
1096             @typ($t),                         << 
1097             @fields($($fields)*),             << 
1098             @error($err),                     << 
1099             @data($data, $($use_data)?),      << 
1100             @has_data($has_data, $get_data),  << 
1101             @construct_closure($construct_clo << 
1102             @munch_fields($($rest)*),         << 
1103         )                                     << 
1104     };                                        << 
1105     (with_update_parsed:                      << 
1106         @this($($this:ident)?),               << 
1107         @typ($t:path),                        << 
1108         @fields($($fields:tt)*),              << 
1109         @error($err:ty),                      << 
1110         // Either `PinData` or `InitData`, `$ << 
1111         // case.                              << 
1112         @data($data:ident, $($use_data:ident) << 
1113         // `HasPinData` or `HasInitData`.     << 
1114         @has_data($has_data:ident, $get_data: << 
1115         // `pin_init_from_closure` or `init_f << 
1116         @construct_closure($construct_closure << 
1117         @zeroed($($init_zeroed:expr)?),       << 
1118     ) => {{                                   << 
1119         // We do not want to allow arbitrary  << 
1120         // type and shadow it later when we i << 
1121         // no possibility of returning withou << 
1122         struct __InitOk;                      << 
1123         // Get the data about fields from the << 
1124         let data = unsafe {                   << 
1125             use $crate::init::__internal::$ha << 
1126             // Here we abuse `paste!` to reto << 
1127             // information that is associated << 
1128             // cannot be used in this positio << 
1129             // code.                          << 
1130             ::kernel::macros::paste!($t::$get << 
1131         };                                    << 
1132         // Ensure that `data` really is of ty << 
1133         let init = $crate::init::__internal:: << 
1134             data,                             << 
1135             move |slot| {                     << 
1136                 {                             << 
1137                     // Shadow the structure s << 
1138                     struct __InitOk;          << 
1139                     // If `$init_zeroed` is p << 
1140                     // error when fields are  << 
1141                     // check that the type ac << 
1142                     $({                       << 
1143                         fn assert_zeroable<T: << 
1144                         // Ensure that the st << 
1145                         assert_zeroable(slot) << 
1146                         // SAFETY: The type i << 
1147                         unsafe { ::core::ptr: << 
1148                         $init_zeroed // This  << 
1149                     })?                       << 
1150                     // Create the `this` so i << 
1151                     // expressions creating t << 
1152                     $(let $this = unsafe { :: << 
1153                     // Initialize every field << 
1154                     $crate::__init_internal!( << 
1155                         @data(data),          << 
1156                         @slot(slot),          << 
1157                         @guards(),            << 
1158                         @munch_fields($($fiel << 
1159                     );                        << 
1160                     // We use unreachable cod << 
1161                     // once, this struct init << 
1162                     // very natural error mes << 
1163                     #[allow(unreachable_code, << 
1164                     let _ = || {              << 
1165                         $crate::__init_intern << 
1166                             @slot(slot),      << 
1167                             @type_name($t),   << 
1168                             @munch_fields($($ << 
1169                             @acc(),           << 
1170                         );                    << 
1171                     };                        << 
1172                 }                             << 
1173                 Ok(__InitOk)                  << 
1174             }                                 << 
1175         );                                    << 
1176         let init = move |slot| -> ::core::res << 
1177             init(slot).map(|__InitOk| ())     << 
1178         };                                    << 
1179         let init = unsafe { $crate::init::$co << 
1180         init                                  << 
1181     }};                                       << 
1182     (init_slot($($use_data:ident)?):          << 
1183         @data($data:ident),                   << 
1184         @slot($slot:ident),                   << 
1185         @guards($($guards:ident,)*),          << 
1186         @munch_fields($(..Zeroable::zeroed()) << 
1187     ) => {                                    << 
1188         // Endpoint of munching, no fields ar << 
1189         // have been initialized. Therefore w << 
1190         $(::core::mem::forget($guards);)*     << 
1191     };                                        << 
1192     (init_slot($use_data:ident): // `use_data << 
1193         @data($data:ident),                   << 
1194         @slot($slot:ident),                   << 
1195         @guards($($guards:ident,)*),          << 
1196         // In-place initialization syntax.    << 
1197         @munch_fields($field:ident <- $val:ex << 
1198     ) => {                                    << 
1199         let init = $val;                      << 
1200         // Call the initializer.              << 
1201         //                                    << 
1202         // SAFETY: `slot` is valid, because w << 
1203         // return when an error/panic occurs. << 
1204         // We also use the `data` to require  << 
1205         unsafe { $data.$field(::core::ptr::ad << 
1206         // Create the drop guard:             << 
1207         //                                    << 
1208         // We rely on macro hygiene to make i << 
1209         // We use `paste!` to create new hygi << 
1210         ::kernel::macros::paste! {            << 
1211             // SAFETY: We forget the guard la << 
1212             let [< __ $field _guard >] = unsa << 
1213                 $crate::init::__internal::Dro << 
1214             };                                << 
1215                                               << 
1216             $crate::__init_internal!(init_slo << 
1217                 @data($data),                 << 
1218                 @slot($slot),                 << 
1219                 @guards([< __ $field _guard > << 
1220                 @munch_fields($($rest)*),     << 
1221             );                                << 
1222         }                                     << 
1223     };                                        << 
1224     (init_slot(): // No `use_data`, so we use << 
1225         @data($data:ident),                   << 
1226         @slot($slot:ident),                   << 
1227         @guards($($guards:ident,)*),          << 
1228         // In-place initialization syntax.    << 
1229         @munch_fields($field:ident <- $val:ex << 
1230     ) => {                                    << 
1231         let init = $val;                      << 
1232         // Call the initializer.              << 
1233         //                                    << 
1234         // SAFETY: `slot` is valid, because w << 
1235         // return when an error/panic occurs. << 
1236         unsafe { $crate::init::Init::__init(i << 
1237         // Create the drop guard:             << 
1238         //                                    << 
1239         // We rely on macro hygiene to make i << 
1240         // We use `paste!` to create new hygi << 
1241         ::kernel::macros::paste! {            << 
1242             // SAFETY: We forget the guard la << 
1243             let [< __ $field _guard >] = unsa << 
1244                 $crate::init::__internal::Dro << 
1245             };                                << 
1246                                               << 
1247             $crate::__init_internal!(init_slo << 
1248                 @data($data),                 << 
1249                 @slot($slot),                 << 
1250                 @guards([< __ $field _guard > << 
1251                 @munch_fields($($rest)*),     << 
1252             );                                << 
1253         }                                     << 
1254     };                                        << 
1255     (init_slot($($use_data:ident)?):          << 
1256         @data($data:ident),                   << 
1257         @slot($slot:ident),                   << 
1258         @guards($($guards:ident,)*),          << 
1259         // Init by-value.                     << 
1260         @munch_fields($field:ident $(: $val:e << 
1261     ) => {                                    << 
1262         {                                     << 
1263             $(let $field = $val;)?            << 
1264             // Initialize the field.          << 
1265             //                                << 
1266             // SAFETY: The memory at `slot` i << 
1267             unsafe { ::core::ptr::write(::cor << 
1268         }                                     << 
1269         // Create the drop guard:             << 
1270         //                                    << 
1271         // We rely on macro hygiene to make i << 
1272         // We use `paste!` to create new hygi << 
1273         ::kernel::macros::paste! {            << 
1274             // SAFETY: We forget the guard la << 
1275             let [< __ $field _guard >] = unsa << 
1276                 $crate::init::__internal::Dro << 
1277             };                                << 
1278                                               << 
1279             $crate::__init_internal!(init_slo << 
1280                 @data($data),                 << 
1281                 @slot($slot),                 << 
1282                 @guards([< __ $field _guard > << 
1283                 @munch_fields($($rest)*),     << 
1284             );                                << 
1285         }                                     << 
1286     };                                        << 
1287     (make_initializer:                        << 
1288         @slot($slot:ident),                   << 
1289         @type_name($t:path),                  << 
1290         @munch_fields(..Zeroable::zeroed() $( << 
1291         @acc($($acc:tt)*),                    << 
1292     ) => {                                    << 
1293         // Endpoint, nothing more to munch, c << 
1294         // `..Zeroable::zeroed()`, the slot w << 
1295         // not been overwritten are thus zero << 
1296         // actually accessible by using the s << 
1297         // We are inside of a closure that is << 
1298         // get the correct type inference her << 
1299         #[allow(unused_assignments)]          << 
1300         unsafe {                              << 
1301             let mut zeroed = ::core::mem::zer << 
1302             // We have to use type inference  << 
1303             // not get executed, so it has no << 
1304             ::core::ptr::write($slot, zeroed) << 
1305             zeroed = ::core::mem::zeroed();   << 
1306             // Here we abuse `paste!` to reto << 
1307             // information that is associated << 
1308             // cannot be used in this positio << 
1309             // code.                          << 
1310             ::kernel::macros::paste!(         << 
1311                 ::core::ptr::write($slot, $t  << 
1312                     $($acc)*                  << 
1313                     ..zeroed                  << 
1314                 });                           << 
1315             );                                << 
1316         }                                     << 
1317     };                                        << 
1318     (make_initializer:                        << 
1319         @slot($slot:ident),                   << 
1320         @type_name($t:path),                  << 
1321         @munch_fields($(,)?),                 << 
1322         @acc($($acc:tt)*),                    << 
1323     ) => {                                    << 
1324         // Endpoint, nothing more to munch, c << 
1325         // Since we are in the closure that i << 
1326         // We abuse `slot` to get the correct << 
1327         unsafe {                              << 
1328             // Here we abuse `paste!` to reto << 
1329             // information that is associated << 
1330             // cannot be used in this positio << 
1331             // code.                          << 
1332             ::kernel::macros::paste!(         << 
1333                 ::core::ptr::write($slot, $t  << 
1334                     $($acc)*                  << 
1335                 });                           << 
1336             );                                << 
1337         }                                     << 
1338     };                                        << 
1339     (make_initializer:                        << 
1340         @slot($slot:ident),                   << 
1341         @type_name($t:path),                  << 
1342         @munch_fields($field:ident <- $val:ex << 
1343         @acc($($acc:tt)*),                    << 
1344     ) => {                                    << 
1345         $crate::__init_internal!(make_initial << 
1346             @slot($slot),                     << 
1347             @type_name($t),                   << 
1348             @munch_fields($($rest)*),         << 
1349             @acc($($acc)* $field: ::core::pan << 
1350         );                                    << 
1351     };                                        << 
1352     (make_initializer:                        << 
1353         @slot($slot:ident),                   << 
1354         @type_name($t:path),                  << 
1355         @munch_fields($field:ident $(: $val:e << 
1356         @acc($($acc:tt)*),                    << 
1357     ) => {                                    << 
1358         $crate::__init_internal!(make_initial << 
1359             @slot($slot),                     << 
1360             @type_name($t),                   << 
1361             @munch_fields($($rest)*),         << 
1362             @acc($($acc)* $field: ::core::pan << 
1363         );                                    << 
1364     };                                        << 
1365 }                                             << 
1366                                               << 
1367 #[doc(hidden)]                                << 
1368 #[macro_export]                               << 
1369 macro_rules! __derive_zeroable {              << 
1370     (parse_input:                             << 
1371         @sig(                                 << 
1372             $(#[$($struct_attr:tt)*])*        << 
1373             $vis:vis struct $name:ident       << 
1374             $(where $($whr:tt)*)?             << 
1375         ),                                    << 
1376         @impl_generics($($impl_generics:tt)*) << 
1377         @ty_generics($($ty_generics:tt)*),    << 
1378         @body({                               << 
1379             $(                                << 
1380                 $(#[$($field_attr:tt)*])*     << 
1381                 $field:ident : $field_ty:ty   << 
1382             ),* $(,)?                         << 
1383         }),                                   << 
1384     ) => {                                    << 
1385         // SAFETY: Every field type implement << 
1386         #[automatically_derived]              << 
1387         unsafe impl<$($impl_generics)*> $crat << 
1388         where                                 << 
1389             $($($whr)*)?                      << 
1390         {}                                    << 
1391         const _: () = {                       << 
1392             fn assert_zeroable<T: ?::core::ma << 
1393             fn ensure_zeroable<$($impl_generi << 
1394                 where $($($whr)*)?            << 
1395             {                                 << 
1396                 $(assert_zeroable::<$field_ty << 
1397             }                                 << 
1398         };                                    << 
1399     };                                           970     };
1400 }                                                971 }
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php