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