1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
//! An internal crate to support pin_project - **do not use directly** #![recursion_limit = "256"] #![doc(html_root_url = "https://docs.rs/pin-project-internal/0.4.7")] #![doc(test( no_crate_inject, attr(deny(warnings, rust_2018_idioms, single_use_lifetimes), allow(dead_code)) ))] #![warn(unsafe_code)] #![warn(rust_2018_idioms, single_use_lifetimes, unreachable_pub)] #![warn(clippy::all)] // mem::take requires Rust 1.40 #![allow(clippy::mem_replace_with_default)] #![allow(clippy::needless_doctest_main)] // While this crate supports stable Rust, it currently requires // nightly Rust in order for rustdoc to correctly document auto-generated // `Unpin` impls. This does not affect the runtime functionality of this crate, // nor does it affect the safety of the api provided by this crate. // // This is disabled by default and can be enabled using // `--cfg pin_project_show_unpin_struct` in RUSTFLAGS. // // Refs: // * https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867 // * https://github.com/taiki-e/pin-project/pull/70 // * https://github.com/rust-lang/rust/issues/63281 #![cfg_attr(pin_project_show_unpin_struct, feature(proc_macro_def_site))] // older compilers require explicit `extern crate`. #[allow(unused_extern_crates)] extern crate proc_macro; #[macro_use] mod utils; mod pin_project; mod pinned_drop; mod project; use proc_macro::TokenStream; use utils::{Immutable, Mutable}; /// An attribute that creates a projection struct covering all the fields. /// /// This attribute creates a projection struct according to the following rules: /// /// - For the field that uses `#[pin]` attribute, makes the pinned reference to /// the field. /// - For the other fields, makes the unpinned reference to the field. /// /// The following methods are implemented on the original `#[pin_project]` type: /// /// ``` /// # #[rustversion::since(1.36)] /// # fn dox() { /// # use std::pin::Pin; /// # type Projection<'a> = &'a (); /// # type ProjectionRef<'a> = &'a (); /// # trait Dox { /// fn project(self: Pin<&mut Self>) -> Projection<'_>; /// fn project_ref(self: Pin<&Self>) -> ProjectionRef<'_>; /// # } /// # } /// ``` /// /// The visibility of the projected type and projection method is based on the /// original type. However, if the visibility of the original type is `pub`, /// the visibility of the projected type and the projection method is `pub(crate)`. /// /// If you want to call the `project` method multiple times or later use the /// original Pin type, it needs to use [`.as_mut()`][`Pin::as_mut`] to avoid /// consuming the `Pin`. /// /// ## Safety /// /// This attribute is completely safe. In the absence of other `unsafe` code *that you write*, /// it is impossible to cause undefined behavior with this attribute. /// /// This is accomplished by enforcing the four requirements for pin projection /// stated in [the Rust documentation](https://doc.rust-lang.org/nightly/std/pin/index.html#projections-and-structural-pinning): /// /// 1. The struct must only be Unpin if all the structural fields are Unpin. /// /// To enforce this, this attribute will automatically generate an `Unpin` implementation /// for you, which will require that all structurally pinned fields be `Unpin` /// If you wish to provide an manual `Unpin` impl, you can do so via the /// `UnsafeUnpin` argument. /// /// 2. The destructor of the struct must not move structural fields out of its argument. /// /// To enforce this, this attribute will generate code like this: /// /// ```rust /// struct MyStruct {} /// trait MyStructMustNotImplDrop {} /// impl<T: Drop> MyStructMustNotImplDrop for T {} /// impl MyStructMustNotImplDrop for MyStruct {} /// ``` /// /// If you attempt to provide an Drop impl, the blanket impl will /// then apply to your type, causing a compile-time error due to /// the conflict with the second impl. /// /// If you wish to provide a custom `Drop` impl, you can annotate a function /// with `#[pinned_drop]`. This function takes a pinned version of your struct - /// that is, `Pin<&mut MyStruct>` where `MyStruct` is the type of your struct. /// /// You can call `project()` on this type as usual, along with any other /// methods you have defined. Because your code is never provided with /// a `&mut MyStruct`, it is impossible to move out of pin-projectable /// fields in safe code in your destructor. /// /// 3. You must make sure that you uphold the Drop guarantee: once your struct is pinned, /// the memory that contains the content is not overwritten or deallocated without calling the content's destructors. /// /// Safe code doesn't need to worry about this - the only wait to violate this requirement /// is to manually deallocate memory (which is `unsafe`), or to overwrite a field with something else. /// Because your custom destructor takes `Pin<&mut MyStruct`, it's impossible to obtain /// a mutable reference to a pin-projected field in safe code. /// /// 4. You must not offer any other operations that could lead to data being moved out of the structural fields when your type is pinned. /// /// As with requirement 3, it is impossible for safe code to violate this. This crate ensures that safe code can never /// obtain a mutable reference to `#[pin]` fields, which prevents you from ever moving out of them in safe code. /// /// Pin projections are also incompatible with `#[repr(packed)]` structs. Attempting to use this attribute /// on a `#[repr(packed)]` struct results in a compile-time error. /// /// /// ## Examples /// /// Using `#[pin_project]` will automatically create the appropriate /// conditional [`Unpin`] implementation: /// /// ```rust /// use pin_project::pin_project; /// use std::pin::Pin; /// /// #[pin_project] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; // Pinned reference to the field /// let _: &mut U = this.field; // Normal reference to the field /// } /// } /// ``` /// /// Note that borrowing the field where `#[pin]` attribute is used multiple /// times requires using [`.as_mut()`][`Pin::as_mut`] to avoid /// consuming the `Pin`. /// /// If you want to implement [`Unpin`] manually, you must use the `UnsafeUnpin` /// argument to `#[pin_project]`. /// /// ```rust /// use pin_project::{pin_project, UnsafeUnpin}; /// use std::pin::Pin; /// /// #[pin_project(UnsafeUnpin)] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; // Pinned reference to the field /// let _: &mut U = this.field; // Normal reference to the field /// } /// } /// /// unsafe impl<T: Unpin, U> UnsafeUnpin for Foo<T, U> {} // Conditional Unpin impl /// ``` /// /// Note the usage of the unsafe [`UnsafeUnpin`] trait, instead of the usual /// [`Unpin`] trait. [`UnsafeUnpin`] behaves exactly like [`Unpin`], except that is /// unsafe to implement. This unsafety comes from the fact that pin projections /// are being used. If you implement [`UnsafeUnpin`], you must ensure that it is /// only implemented when all pin-projected fields implement [`Unpin`]. /// /// See [`UnsafeUnpin`] trait for more details. /// /// ### `#[pinned_drop]` /// /// In order to correctly implement pin projections, a type's `Drop` impl must /// not move out of any structurally pinned fields. Unfortunately, [`Drop::drop`] /// takes `&mut Self`, not `Pin<&mut Self>`. /// /// To ensure that this requirement is upheld, the `#[pin_project]` attribute will /// provide a [`Drop`] impl for you. This `Drop` impl will delegate to an impl /// block annotated with `#[pinned_drop]` if you use the `PinnedDrop` argument /// to `#[pin_project]`. This impl block acts just like a normal [`Drop`] impl, /// except for the following two: /// /// * `drop` method takes `Pin<&mut Self>` /// * Name of the trait is `PinnedDrop`. /// /// `#[pin_project]` implements the actual [`Drop`] trait via `PinnedDrop` you /// implemented. To drop a type that implements `PinnedDrop`, use the [`drop`] /// function just like dropping a type that directly implements [`Drop`]. /// /// In particular, it will never be called more than once, just like [`Drop::drop`]. /// /// For example: /// /// ```rust /// use pin_project::{pin_project, pinned_drop}; /// use std::{fmt::Debug, pin::Pin}; /// /// #[pin_project(PinnedDrop)] /// pub struct Foo<T: Debug, U: Debug> { /// #[pin] /// pinned_field: T, /// unpin_field: U, /// } /// /// #[pinned_drop] /// impl<T: Debug, U: Debug> PinnedDrop for Foo<T, U> { /// fn drop(self: Pin<&mut Self>) { /// println!("Dropping pinned field: {:?}", self.pinned_field); /// println!("Dropping unpin field: {:?}", self.unpin_field); /// } /// } /// /// fn main() { /// let _x = Foo { pinned_field: true, unpin_field: 40 }; /// } /// ``` /// /// See also [`pinned_drop`] attribute. /// /// ## Supported Items /// /// The current pin-project supports the following types of items. /// /// ### Structs (structs with named fields): /// /// ```rust /// use pin_project::pin_project; /// use std::pin::Pin; /// /// #[pin_project] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; /// let _: &mut U = this.field; /// } /// } /// ``` /// /// ### Tuple structs (structs with unnamed fields): /// /// ```rust /// use pin_project::pin_project; /// use std::pin::Pin; /// /// #[pin_project] /// struct Foo<T, U>(#[pin] T, U); /// /// impl<T, U> Foo<T, U> { /// fn baz(self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.0; /// let _: &mut U = this.1; /// } /// } /// ``` /// /// Structs without fields (unit-like struct and zero fields struct) are not /// supported. /// /// ### Enums /// /// `pin_project` also supports enums, but to use it, you need to use with the /// [`project`] attribute. /// /// The attribute at the expression position is not stable, so you need to use /// a dummy `#[project]` attribute for the function. /// /// ```rust /// use pin_project::{pin_project, project}; /// use std::pin::Pin; /// /// #[pin_project] /// enum Foo<A, B, C> { /// Tuple(#[pin] A, B), /// Struct { field: C }, /// Unit, /// } /// /// impl<A, B, C> Foo<A, B, C> { /// #[project] // Nightly does not need a dummy attribute to the function. /// fn baz(self: Pin<&mut Self>) { /// #[project] /// match self.project() { /// Foo::Tuple(x, y) => { /// let _: Pin<&mut A> = x; /// let _: &mut B = y; /// } /// Foo::Struct { field } => { /// let _: &mut C = field; /// } /// Foo::Unit => {} /// } /// } /// } /// ``` /// /// Enums without variants (zero-variant enums) are not supported. /// /// See also [`project`] and [`project_ref`] attributes. /// /// [`Pin::as_mut`]: core::pin::Pin::as_mut /// [`Pin::set`]: core::pin::Pin::set /// [`drop`]: Drop::drop /// [`UnsafeUnpin`]: https://docs.rs/pin-project/0.4/pin_project/trait.UnsafeUnpin.html /// [`project`]: ./attr.project.html /// [`project_ref`]: ./attr.project_ref.html /// [`pinned_drop`]: ./attr.pinned_drop.html #[proc_macro_attribute] pub fn pin_project(args: TokenStream, input: TokenStream) -> TokenStream { pin_project::attribute(&args.into(), input.into()).into() } /// An attribute for annotating an impl block that implements [`Drop`]. /// /// This attribute is only needed when you wish to provide a [`Drop`] /// impl for your type. The impl block annotated with `#[pinned_drop]` acts just /// like a normal [`Drop`] impl, except for the fact that `drop` method takes /// `Pin<&mut Self>`. In particular, it will never be called more than once, /// just like [`Drop::drop`]. /// /// ## Example /// /// ```rust /// use pin_project::{pin_project, pinned_drop}; /// use std::pin::Pin; /// /// #[pin_project(PinnedDrop)] /// struct Foo { /// #[pin] /// field: u8, /// } /// /// #[pinned_drop] /// impl PinnedDrop for Foo { /// fn drop(self: Pin<&mut Self>) { /// println!("Dropping: {}", self.field); /// } /// } /// /// fn main() { /// let _x = Foo { field: 50 }; /// } /// ``` /// /// See ["pinned-drop" section of `pin_project` attribute][pinned-drop] for more details. /// /// [pinned-drop]: ./attr.pin_project.html#pinned_drop #[proc_macro_attribute] pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(input); pinned_drop::attribute(&args.into(), input).into() } /// An attribute to provide way to refer to the projected type returned by /// `project` method. /// /// The following syntaxes are supported. /// /// ## `impl` blocks /// /// All methods (and associated functions) in `#[project] impl` block become /// methods of the projected type. If you want to implement methods on the /// original type, you need to create another (non-`#[project]`) `impl` block. /// /// To call a method implemented in `#[project] impl` block, you need to first /// get the projected-type with `let this = self.project();`. /// /// ### Examples /// /// ```rust /// use pin_project::{pin_project, project}; /// use std::pin::Pin; /// /// #[pin_project] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// // impl for the original type /// impl<T, U> Foo<T, U> { /// fn bar(self: Pin<&mut Self>) { /// self.project().baz() /// } /// } /// /// // impl for the projected type /// #[project] /// impl<T, U> Foo<T, U> { /// fn baz(self) { /// let Self { future, field } = self; /// /// let _: Pin<&mut T> = future; /// let _: &mut U = field; /// } /// } /// ``` /// /// ## `let` bindings /// /// *The attribute at the expression position is not stable, so you need to use /// a dummy `#[project]` attribute for the function.* /// /// ### Examples /// /// ```rust /// use pin_project::{pin_project, project}; /// use std::pin::Pin; /// /// #[pin_project] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// #[project] // Nightly does not need a dummy attribute to the function. /// fn baz(self: Pin<&mut Self>) { /// #[project] /// let Foo { future, field } = self.project(); /// /// let _: Pin<&mut T> = future; /// let _: &mut U = field; /// } /// } /// ``` /// /// ## `match` expressions /// /// *The attribute at the expression position is not stable, so you need to use /// a dummy `#[project]` attribute for the function.* /// /// ### Examples /// /// ```rust /// use pin_project::{pin_project, project}; /// use std::pin::Pin; /// /// #[pin_project] /// enum Foo<A, B, C> { /// Tuple(#[pin] A, B), /// Struct { field: C }, /// Unit, /// } /// /// impl<A, B, C> Foo<A, B, C> { /// #[project] // Nightly does not need a dummy attribute to the function. /// fn baz(self: Pin<&mut Self>) { /// #[project] /// match self.project() { /// Foo::Tuple(x, y) => { /// let _: Pin<&mut A> = x; /// let _: &mut B = y; /// } /// Foo::Struct { field } => { /// let _: &mut C = field; /// } /// Foo::Unit => {} /// } /// } /// } /// ``` /// /// ## `use` statements /// /// ### Examples /// /// ```rust /// # mod dox { /// use pin_project::pin_project; /// /// #[pin_project] /// struct Foo<A> { /// #[pin] /// field: A, /// } /// /// mod bar { /// use super::Foo; /// use pin_project::project; /// use std::pin::Pin; /// /// #[project] /// use super::Foo; /// /// #[project] /// fn baz<A>(foo: Pin<&mut Foo<A>>) { /// #[project] /// let Foo { field } = foo.project(); /// let _: Pin<&mut A> = field; /// } /// } /// # } /// ``` #[proc_macro_attribute] pub fn project(args: TokenStream, input: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(input); project::attribute(&args.into(), input, Mutable).into() } /// An attribute to provide way to refer to the projected type returned by /// `project_ref` method. /// /// This is the same as [`project`] attribute except it refers to the projected /// type returned by `project_ref` method. /// /// See [`project`] attribute for more details. /// /// [`project`]: ./attr.project.html #[proc_macro_attribute] pub fn project_ref(args: TokenStream, input: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(input); project::attribute(&args.into(), input, Immutable).into() } /// An internal helper macro. #[doc(hidden)] #[proc_macro_derive(__PinProjectInternalDerive, attributes(pin))] pub fn __pin_project_internal_derive(input: TokenStream) -> TokenStream { pin_project::derive(input.into()).into() }