1 //! A lightweight version of [pin-project] written with declarative macros. 2 //! 3 //! # Examples 4 //! 5 //! [`pin_project!`] macro creates a projection type covering all the fields of struct. 6 //! 7 //! ```rust 8 //! use pin_project_lite::pin_project; 9 //! use std::pin::Pin; 10 //! 11 //! pin_project! { 12 //! struct Struct<T, U> { 13 //! #[pin] 14 //! pinned: T, 15 //! unpinned: U, 16 //! } 17 //! } 18 //! 19 //! impl<T, U> Struct<T, U> { 20 //! fn method(self: Pin<&mut Self>) { 21 //! let this = self.project(); 22 //! let _: Pin<&mut T> = this.pinned; // Pinned reference to the field 23 //! let _: &mut U = this.unpinned; // Normal reference to the field 24 //! } 25 //! } 26 //! ``` 27 //! 28 //! To use [`pin_project!`] on enums, you need to name the projection type 29 //! returned from the method. 30 //! 31 //! ```rust 32 //! use pin_project_lite::pin_project; 33 //! use std::pin::Pin; 34 //! 35 //! pin_project! { 36 //! #[project = EnumProj] 37 //! enum Enum<T, U> { 38 //! Variant { #[pin] pinned: T, unpinned: U }, 39 //! } 40 //! } 41 //! 42 //! impl<T, U> Enum<T, U> { 43 //! fn method(self: Pin<&mut Self>) { 44 //! match self.project() { 45 //! EnumProj::Variant { pinned, unpinned } => { 46 //! let _: Pin<&mut T> = pinned; 47 //! let _: &mut U = unpinned; 48 //! } 49 //! } 50 //! } 51 //! } 52 //! ``` 53 //! 54 //! # [pin-project] vs pin-project-lite 55 //! 56 //! Here are some similarities and differences compared to [pin-project]. 57 //! 58 //! ## Similar: Safety 59 //! 60 //! pin-project-lite guarantees safety in much the same way as [pin-project]. 61 //! Both are completely safe unless you write other unsafe code. 62 //! 63 //! ## Different: Minimal design 64 //! 65 //! This library does not tackle as expansive of a range of use cases as 66 //! [pin-project] does. If your use case is not already covered, please use 67 //! [pin-project]. 68 //! 69 //! ## Different: No proc-macro related dependencies 70 //! 71 //! This is the **only** reason to use this crate. However, **if you already 72 //! have proc-macro related dependencies in your crate's dependency graph, there 73 //! is no benefit from using this crate.** (Note: There is almost no difference 74 //! in the amount of code generated between [pin-project] and pin-project-lite.) 75 //! 76 //! ## Different: No useful error messages 77 //! 78 //! This macro does not handle any invalid input. So error messages are not to 79 //! be useful in most cases. If you do need useful error messages, then upon 80 //! error you can pass the same input to [pin-project] to receive a helpful 81 //! description of the compile error. 82 //! 83 //! ## Different: No support for custom Drop implementation 84 //! 85 //! pin-project supports this by [`#[pinned_drop]`][pinned-drop]. 86 //! 87 //! ## Different: No support for custom Unpin implementation 88 //! 89 //! pin-project supports this by [`UnsafeUnpin`][unsafe-unpin] and [`!Unpin`][not-unpin]. 90 //! 91 //! ## Different: No support for tuple structs and tuple variants 92 //! 93 //! pin-project supports this. 94 //! 95 //! [not-unpin]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#unpin 96 //! [pin-project]: https://github.com/taiki-e/pin-project 97 //! [pinned-drop]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#pinned_drop 98 //! [unsafe-unpin]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#unsafeunpin 99 100 #![no_std] 101 #![doc(test( 102 no_crate_inject, 103 attr( 104 deny(warnings, rust_2018_idioms, single_use_lifetimes), 105 allow(dead_code, unused_variables) 106 ) 107 ))] 108 #![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)] 109 #![warn(clippy::all, clippy::default_trait_access)] 110 111 // ANDROID: Use std to allow building as a dylib. 112 extern crate std; 113 114 /// A macro that creates a projection type covering all the fields of struct. 115 /// 116 /// This macro creates a projection type according to the following rules: 117 /// 118 /// * For the field that uses `#[pin]` attribute, makes the pinned reference to the field. 119 /// * For the other fields, makes the unpinned reference to the field. 120 /// 121 /// And the following methods are implemented on the original type: 122 /// 123 /// ```rust 124 /// # use std::pin::Pin; 125 /// # type Projection<'a> = &'a (); 126 /// # type ProjectionRef<'a> = &'a (); 127 /// # trait Dox { 128 /// fn project(self: Pin<&mut Self>) -> Projection<'_>; 129 /// fn project_ref(self: Pin<&Self>) -> ProjectionRef<'_>; 130 /// # } 131 /// ``` 132 /// 133 /// By passing an attribute with the same name as the method to the macro, 134 /// you can name the projection type returned from the method. This allows you 135 /// to use pattern matching on the projected types. 136 /// 137 /// ```rust 138 /// # use pin_project_lite::pin_project; 139 /// # use std::pin::Pin; 140 /// pin_project! { 141 /// #[project = EnumProj] 142 /// enum Enum<T> { 143 /// Variant { #[pin] field: T }, 144 /// } 145 /// } 146 /// 147 /// impl<T> Enum<T> { 148 /// fn method(self: Pin<&mut Self>) { 149 /// let this: EnumProj<'_, T> = self.project(); 150 /// match this { 151 /// EnumProj::Variant { field } => { 152 /// let _: Pin<&mut T> = field; 153 /// } 154 /// } 155 /// } 156 /// } 157 /// ``` 158 /// 159 /// By passing the `#[project_replace = MyProjReplace]` attribute you may create an additional 160 /// method which allows the contents of `Pin<&mut Self>` to be replaced while simultaneously moving 161 /// out all unpinned fields in `Self`. 162 /// 163 /// ```rust 164 /// # use std::pin::Pin; 165 /// # type MyProjReplace = (); 166 /// # trait Dox { 167 /// fn project_replace(self: Pin<&mut Self>, replacement: Self) -> MyProjReplace; 168 /// # } 169 /// ``` 170 /// 171 /// The `#[project]` (and `#[project_ref]`) attribute must precede the other 172 /// attributes except for `#[doc]`. For example, the following code will not be compiled: 173 /// 174 /// ```rust,compile_fail 175 /// # use pin_project_lite::pin_project; 176 /// # use std::pin::Pin; 177 /// pin_project! { 178 /// /// documents (`#[doc]`) can be placed before `#[project]`. 179 /// #[derive(Clone)] // <--- ERROR 180 /// #[project = EnumProj] 181 /// #[derive(Debug)] // <--- Ok 182 /// enum Enum<T> { 183 /// Variant { #[pin] field: T }, 184 /// } 185 /// } 186 /// ``` 187 /// 188 /// Also, note that the projection types returned by `project` and `project_ref` have 189 /// an additional lifetime at the beginning of generics. 190 /// 191 /// ```text 192 /// let this: EnumProj<'_, T> = self.project(); 193 /// ^^ 194 /// ``` 195 /// 196 /// The visibility of the projected types and projection methods is based on the 197 /// original type. However, if the visibility of the original type is `pub`, the 198 /// visibility of the projected types and the projection methods is downgraded 199 /// to `pub(crate)`. 200 /// 201 /// # Safety 202 /// 203 /// `pin_project!` macro guarantees safety in much the same way as [pin-project] crate. 204 /// Both are completely safe unless you write other unsafe code. 205 /// 206 /// See [pin-project] crate for more details. 207 /// 208 /// # Examples 209 /// 210 /// ```rust 211 /// use pin_project_lite::pin_project; 212 /// use std::pin::Pin; 213 /// 214 /// pin_project! { 215 /// struct Struct<T, U> { 216 /// #[pin] 217 /// pinned: T, 218 /// unpinned: U, 219 /// } 220 /// } 221 /// 222 /// impl<T, U> Struct<T, U> { 223 /// fn method(self: Pin<&mut Self>) { 224 /// let this = self.project(); 225 /// let _: Pin<&mut T> = this.pinned; // Pinned reference to the field 226 /// let _: &mut U = this.unpinned; // Normal reference to the field 227 /// } 228 /// } 229 /// ``` 230 /// 231 /// To use `pin_project!` on enums, you need to name the projection type 232 /// returned from the method. 233 /// 234 /// ```rust 235 /// use pin_project_lite::pin_project; 236 /// use std::pin::Pin; 237 /// 238 /// pin_project! { 239 /// #[project = EnumProj] 240 /// enum Enum<T> { 241 /// Struct { 242 /// #[pin] 243 /// field: T, 244 /// }, 245 /// Unit, 246 /// } 247 /// } 248 /// 249 /// impl<T> Enum<T> { 250 /// fn method(self: Pin<&mut Self>) { 251 /// match self.project() { 252 /// EnumProj::Struct { field } => { 253 /// let _: Pin<&mut T> = field; 254 /// } 255 /// EnumProj::Unit => {} 256 /// } 257 /// } 258 /// } 259 /// ``` 260 /// 261 /// If you want to call the `project()` method multiple times or later use the 262 /// original [`Pin`] type, it needs to use [`.as_mut()`][`Pin::as_mut`] to avoid 263 /// consuming the [`Pin`]. 264 /// 265 /// ```rust 266 /// use pin_project_lite::pin_project; 267 /// use std::pin::Pin; 268 /// 269 /// pin_project! { 270 /// struct Struct<T> { 271 /// #[pin] 272 /// field: T, 273 /// } 274 /// } 275 /// 276 /// impl<T> Struct<T> { 277 /// fn call_project_twice(mut self: Pin<&mut Self>) { 278 /// // `project` consumes `self`, so reborrow the `Pin<&mut Self>` via `as_mut`. 279 /// self.as_mut().project(); 280 /// self.as_mut().project(); 281 /// } 282 /// } 283 /// ``` 284 /// 285 /// # `!Unpin` 286 /// 287 /// If you want to ensure that [`Unpin`] is not implemented, use `#[pin]` 288 /// attribute for a [`PhantomPinned`] field. 289 /// 290 /// ```rust 291 /// use pin_project_lite::pin_project; 292 /// use std::marker::PhantomPinned; 293 /// 294 /// pin_project! { 295 /// struct Struct<T> { 296 /// field: T, 297 /// #[pin] // <------ This `#[pin]` is required to make `Struct` to `!Unpin`. 298 /// _pin: PhantomPinned, 299 /// } 300 /// } 301 /// ``` 302 /// 303 /// Note that using [`PhantomPinned`] without `#[pin]` attribute has no effect. 304 /// 305 /// [`PhantomPinned`]: core::marker::PhantomPinned 306 /// [`Pin::as_mut`]: core::pin::Pin::as_mut 307 /// [`Pin`]: core::pin::Pin 308 /// [pin-project]: https://github.com/taiki-e/pin-project 309 #[macro_export] 310 macro_rules! pin_project { 311 // Parses options 312 ( 313 $(#[doc $($doc:tt)*])* 314 #[project = $proj_mut_ident:ident] 315 #[project_ref = $proj_ref_ident:ident] 316 #[project_replace = $proj_replace_ident:ident] 317 $($tt:tt)* 318 ) => { 319 $crate::__pin_project_internal! { 320 [$proj_mut_ident][$proj_ref_ident][$proj_replace_ident] 321 $(#[doc $($doc)*])* 322 $($tt)* 323 } 324 }; 325 ( 326 $(#[doc $($doc:tt)*])* 327 #[project = $proj_mut_ident:ident] 328 #[project_ref = $proj_ref_ident:ident] 329 $($tt:tt)* 330 ) => { 331 $crate::__pin_project_internal! { 332 [$proj_mut_ident][$proj_ref_ident][] 333 $(#[doc $($doc)*])* 334 $($tt)* 335 } 336 }; 337 ( 338 $(#[doc $($doc:tt)*])* 339 #[project = $proj_mut_ident:ident] 340 #[project_replace = $proj_replace_ident:ident] 341 $($tt:tt)* 342 ) => { 343 $crate::__pin_project_internal! { 344 [$proj_mut_ident][][$proj_replace_ident] 345 $(#[doc $($doc)*])* 346 $($tt)* 347 } 348 }; 349 ( 350 $(#[doc $($doc:tt)*])* 351 #[project_ref = $proj_ref_ident:ident] 352 #[project_replace = $proj_replace_ident:ident] 353 $($tt:tt)* 354 ) => { 355 $crate::__pin_project_internal! { 356 [][$proj_ref_ident][$proj_replace_ident] 357 $(#[doc $($doc)*])* 358 $($tt)* 359 } 360 }; 361 ( 362 $(#[doc $($doc:tt)*])* 363 #[project = $proj_mut_ident:ident] 364 $($tt:tt)* 365 ) => { 366 $crate::__pin_project_internal! { 367 [$proj_mut_ident][][] 368 $(#[doc $($doc)*])* 369 $($tt)* 370 } 371 }; 372 ( 373 $(#[doc $($doc:tt)*])* 374 #[project_ref = $proj_ref_ident:ident] 375 $($tt:tt)* 376 ) => { 377 $crate::__pin_project_internal! { 378 [][$proj_ref_ident][] 379 $(#[doc $($doc)*])* 380 $($tt)* 381 } 382 }; 383 ( 384 $(#[doc $($doc:tt)*])* 385 #[project_replace = $proj_replace_ident:ident] 386 $($tt:tt)* 387 ) => { 388 $crate::__pin_project_internal! { 389 [][][$proj_replace_ident] 390 $(#[doc $($doc)*])* 391 $($tt)* 392 } 393 }; 394 ( 395 $($tt:tt)* 396 ) => { 397 $crate::__pin_project_internal! { 398 [][][] 399 $($tt)* 400 } 401 }; 402 } 403 404 // limitations: 405 // * no support for tuple structs and tuple variant (wontfix). 406 // * no support for multiple trait/lifetime bounds. 407 // * no support for `Self` in where clauses. (wontfix) 408 // * no support for overlapping lifetime names. (wontfix) 409 // * no interoperability with other field attributes. 410 // * no useful error messages. (wontfix) 411 // etc... 412 413 // Not public API. 414 #[doc(hidden)] 415 #[macro_export] 416 macro_rules! __pin_project_internal { 417 // ============================================================================================= 418 // struct:main 419 (@struct=>internal; 420 [$($proj_mut_ident:ident)?] 421 [$($proj_ref_ident:ident)?] 422 [$($proj_replace_ident:ident)?] 423 [$proj_vis:vis] 424 [$(#[$attrs:meta])* $vis:vis struct $ident:ident] 425 [$($def_generics:tt)*] 426 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?] 427 { 428 $( 429 $(#[$pin:ident])? 430 $field_vis:vis $field:ident: $field_ty:ty 431 ),+ 432 } 433 ) => { 434 $(#[$attrs])* 435 $vis struct $ident $($def_generics)* 436 $(where 437 $($where_clause)*)? 438 { 439 $( 440 $field_vis $field: $field_ty 441 ),+ 442 } 443 444 $crate::__pin_project_internal! { @struct=>make_proj_ty=>named; 445 [$proj_vis] 446 [$($proj_mut_ident)?] 447 [make_proj_field_mut] 448 [$ident] 449 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 450 { 451 $( 452 $(#[$pin])? 453 $field_vis $field: $field_ty 454 ),+ 455 } 456 } 457 $crate::__pin_project_internal! { @struct=>make_proj_ty=>named; 458 [$proj_vis] 459 [$($proj_ref_ident)?] 460 [make_proj_field_ref] 461 [$ident] 462 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 463 { 464 $( 465 $(#[$pin])? 466 $field_vis $field: $field_ty 467 ),+ 468 } 469 } 470 $crate::__pin_project_internal! { @struct=>make_proj_replace_ty=>named; 471 [$proj_vis] 472 [$($proj_replace_ident)?] 473 [make_proj_field_replace] 474 [$ident] 475 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 476 { 477 $( 478 $(#[$pin])? 479 $field_vis $field: $field_ty 480 ),+ 481 } 482 } 483 484 #[allow(explicit_outlives_requirements)] // https://github.com/rust-lang/rust/issues/60993 485 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 486 // This lint warns of `clippy::*` generated by external macros. 487 // We allow this lint for compatibility with older compilers. 488 #[allow(clippy::unknown_clippy_lints)] 489 #[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct. 490 #[allow(clippy::used_underscore_binding)] 491 const _: () = { 492 $crate::__pin_project_internal! { @struct=>make_proj_ty=>unnamed; 493 [$proj_vis] 494 [$($proj_mut_ident)?][Projection] 495 [make_proj_field_mut] 496 [$ident] 497 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 498 { 499 $( 500 $(#[$pin])? 501 $field_vis $field: $field_ty 502 ),+ 503 } 504 } 505 $crate::__pin_project_internal! { @struct=>make_proj_ty=>unnamed; 506 [$proj_vis] 507 [$($proj_ref_ident)?][ProjectionRef] 508 [make_proj_field_ref] 509 [$ident] 510 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 511 { 512 $( 513 $(#[$pin])? 514 $field_vis $field: $field_ty 515 ),+ 516 } 517 } 518 $crate::__pin_project_internal! { @struct=>make_proj_replace_ty=>unnamed; 519 [$proj_vis] 520 [$($proj_replace_ident)?][ProjectionReplace] 521 [make_proj_field_replace] 522 [$ident] 523 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 524 { 525 $( 526 $(#[$pin])? 527 $field_vis $field: $field_ty 528 ),+ 529 } 530 } 531 532 impl <$($impl_generics)*> $ident <$($ty_generics)*> 533 $(where 534 $($where_clause)*)? 535 { 536 $crate::__pin_project_internal! { @struct=>make_proj_method; 537 [$proj_vis] 538 [$($proj_mut_ident)?][Projection] 539 [project get_unchecked_mut mut] 540 [$($ty_generics)*] 541 { 542 $( 543 $(#[$pin])? 544 $field_vis $field 545 ),+ 546 } 547 } 548 $crate::__pin_project_internal! { @struct=>make_proj_method; 549 [$proj_vis] 550 [$($proj_ref_ident)?][ProjectionRef] 551 [project_ref get_ref] 552 [$($ty_generics)*] 553 { 554 $( 555 $(#[$pin])? 556 $field_vis $field 557 ),+ 558 } 559 } 560 $crate::__pin_project_internal! { @struct=>make_proj_replace_method; 561 [$proj_vis] 562 [$($proj_replace_ident)?][ProjectionReplace] 563 [$($ty_generics)*] 564 { 565 $( 566 $(#[$pin])? 567 $field_vis $field 568 ),+ 569 } 570 } 571 } 572 573 $crate::__pin_project_internal! { @make_unpin_impl; 574 [$vis $ident] 575 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 576 $( 577 $field: $crate::__pin_project_internal!(@make_unpin_bound; 578 $(#[$pin])? $field_ty 579 ) 580 ),+ 581 } 582 583 $crate::__pin_project_internal! { @make_drop_impl; 584 [$ident] 585 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 586 } 587 588 // Ensure that it's impossible to use pin projections on a #[repr(packed)] struct. 589 // 590 // Taking a reference to a packed field is unsafe, amd appplying 591 // #[forbid(safe_packed_borrows)] makes sure that doing this without 592 // an 'unsafe' block (which we deliberately do not generate) 593 // is a hard error. 594 // 595 // If the struct ends up having #[repr(packed)] applied somehow, 596 // this will generate an (unfriendly) error message. Under all reasonable 597 // circumstances, we'll detect the #[repr(packed)] attribute, and generate 598 // a much nicer error above. 599 // 600 // See https://github.com/taiki-e/pin-project/pull/34 for more details. 601 #[forbid(safe_packed_borrows)] 602 fn __assert_not_repr_packed <$($impl_generics)*> (this: &$ident <$($ty_generics)*>) 603 $(where 604 $($where_clause)*)? 605 { 606 $( 607 let _ = &this.$field; 608 )+ 609 } 610 }; 611 }; 612 // ============================================================================================= 613 // enum:main 614 (@enum=>internal; 615 [$($proj_mut_ident:ident)?] 616 [$($proj_ref_ident:ident)?] 617 [$($proj_replace_ident:ident)?] 618 [$proj_vis:vis] 619 [$(#[$attrs:meta])* $vis:vis enum $ident:ident] 620 [$($def_generics:tt)*] 621 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?] 622 { 623 $( 624 $(#[$variant_attrs:meta])* 625 $variant:ident $({ 626 $( 627 $(#[$pin:ident])? 628 $field:ident: $field_ty:ty 629 ),+ 630 })? 631 ),+ 632 } 633 ) => { 634 $(#[$attrs])* 635 $vis enum $ident $($def_generics)* 636 $(where 637 $($where_clause)*)? 638 { 639 $( 640 $(#[$variant_attrs])* 641 $variant $({ 642 $( 643 $field: $field_ty 644 ),+ 645 })? 646 ),+ 647 } 648 649 $crate::__pin_project_internal! { @enum=>make_proj_ty; 650 [$proj_vis] 651 [$($proj_mut_ident)?] 652 [make_proj_field_mut] 653 [$ident] 654 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 655 { 656 $( 657 $variant $({ 658 $( 659 $(#[$pin])? 660 $field: $field_ty 661 ),+ 662 })? 663 ),+ 664 } 665 } 666 $crate::__pin_project_internal! { @enum=>make_proj_ty; 667 [$proj_vis] 668 [$($proj_ref_ident)?] 669 [make_proj_field_ref] 670 [$ident] 671 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 672 { 673 $( 674 $variant $({ 675 $( 676 $(#[$pin])? 677 $field: $field_ty 678 ),+ 679 })? 680 ),+ 681 } 682 } 683 $crate::__pin_project_internal! { @enum=>make_proj_replace_ty; 684 [$proj_vis] 685 [$($proj_replace_ident)?] 686 [make_proj_field_replace] 687 [$ident] 688 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 689 { 690 $( 691 $variant $({ 692 $( 693 $(#[$pin])? 694 $field: $field_ty 695 ),+ 696 })? 697 ),+ 698 } 699 } 700 701 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 702 // This lint warns of `clippy::*` generated by external macros. 703 // We allow this lint for compatibility with older compilers. 704 #[allow(clippy::unknown_clippy_lints)] 705 #[allow(clippy::used_underscore_binding)] 706 const _: () = { 707 impl <$($impl_generics)*> $ident <$($ty_generics)*> 708 $(where 709 $($where_clause)*)? 710 { 711 $crate::__pin_project_internal! { @enum=>make_proj_method; 712 [$proj_vis] 713 [$($proj_mut_ident)?] 714 [project get_unchecked_mut mut] 715 [$($ty_generics)*] 716 { 717 $( 718 $variant $({ 719 $( 720 $(#[$pin])? 721 $field 722 ),+ 723 })? 724 ),+ 725 } 726 } 727 $crate::__pin_project_internal! { @enum=>make_proj_method; 728 [$proj_vis] 729 [$($proj_ref_ident)?] 730 [project_ref get_ref] 731 [$($ty_generics)*] 732 { 733 $( 734 $variant $({ 735 $( 736 $(#[$pin])? 737 $field 738 ),+ 739 })? 740 ),+ 741 } 742 } 743 $crate::__pin_project_internal! { @enum=>make_proj_replace_method; 744 [$proj_vis] 745 [$($proj_replace_ident)?] 746 [$($ty_generics)*] 747 { 748 $( 749 $variant $({ 750 $( 751 $(#[$pin])? 752 $field 753 ),+ 754 })? 755 ),+ 756 } 757 } 758 } 759 760 $crate::__pin_project_internal! { @make_unpin_impl; 761 [$vis $ident] 762 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 763 $( 764 $variant: ($( 765 $( 766 $crate::__pin_project_internal!(@make_unpin_bound; 767 $(#[$pin])? $field_ty 768 ) 769 ),+ 770 )?) 771 ),+ 772 } 773 774 $crate::__pin_project_internal! { @make_drop_impl; 775 [$ident] 776 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 777 } 778 779 // We don't need to check for '#[repr(packed)]', 780 // since it does not apply to enums. 781 }; 782 }; 783 784 // ============================================================================================= 785 // struct:make_proj_ty 786 (@struct=>make_proj_ty=>unnamed; 787 [$proj_vis:vis] 788 [$_proj_ty_ident:ident][$proj_ty_ident:ident] 789 [$make_proj_field:ident] 790 [$ident:ident] 791 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 792 $($field:tt)* 793 ) => {}; 794 (@struct=>make_proj_ty=>unnamed; 795 [$proj_vis:vis] 796 [][$proj_ty_ident:ident] 797 [$make_proj_field:ident] 798 [$ident:ident] 799 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 800 $($field:tt)* 801 ) => { 802 $crate::__pin_project_internal! { @struct=>make_proj_ty=>named; 803 [$proj_vis] 804 [$proj_ty_ident] 805 [$make_proj_field] 806 [$ident] 807 [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] 808 $($field)* 809 } 810 }; 811 (@struct=>make_proj_ty=>named; 812 [$proj_vis:vis] 813 [$proj_ty_ident:ident] 814 [$make_proj_field:ident] 815 [$ident:ident] 816 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 817 { 818 $( 819 $(#[$pin:ident])? 820 $field_vis:vis $field:ident: $field_ty:ty 821 ),+ 822 } 823 ) => { 824 #[allow(dead_code)] // This lint warns unused fields/variants. 825 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 826 // This lint warns of `clippy::*` generated by external macros. 827 // We allow this lint for compatibility with older compilers. 828 #[allow(clippy::unknown_clippy_lints)] 829 #[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`. (only needed for project) 830 #[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct. 831 #[allow(clippy::ref_option_ref)] // This lint warns `&Option<&<ty>>`. (only needed for project_ref) 832 #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 833 $proj_vis struct $proj_ty_ident <'__pin, $($impl_generics)*> 834 where 835 $ident <$($ty_generics)*>: '__pin 836 $(, $($where_clause)*)? 837 { 838 $( 839 $field_vis $field: $crate::__pin_project_internal!(@$make_proj_field; 840 $(#[$pin])? $field_ty 841 ) 842 ),+ 843 } 844 }; 845 (@struct=>make_proj_ty=>named; 846 [$proj_vis:vis] 847 [] 848 [$make_proj_field:ident] 849 [$ident:ident] 850 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 851 $($field:tt)* 852 ) => {}; 853 854 (@struct=>make_proj_replace_ty=>unnamed; 855 [$proj_vis:vis] 856 [$_proj_ty_ident:ident][$proj_ty_ident:ident] 857 [$make_proj_field:ident] 858 [$ident:ident] 859 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 860 $($field:tt)* 861 ) => {}; 862 (@struct=>make_proj_replace_ty=>unnamed; 863 [$proj_vis:vis] 864 [][$proj_ty_ident:ident] 865 [$make_proj_field:ident] 866 [$ident:ident] 867 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 868 $($field:tt)* 869 ) => { 870 }; 871 (@struct=>make_proj_replace_ty=>named; 872 [$proj_vis:vis] 873 [$proj_ty_ident:ident] 874 [$make_proj_field:ident] 875 [$ident:ident] 876 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 877 { 878 $( 879 $(#[$pin:ident])? 880 $field_vis:vis $field:ident: $field_ty:ty 881 ),+ 882 } 883 ) => { 884 #[allow(dead_code)] // This lint warns unused fields/variants. 885 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 886 #[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`. (only needed for project) 887 #[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct. 888 #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 889 $proj_vis struct $proj_ty_ident <$($impl_generics)*> 890 where 891 $($($where_clause)*)? 892 { 893 $( 894 $field_vis $field: $crate::__pin_project_internal!(@$make_proj_field; 895 $(#[$pin])? $field_ty 896 ) 897 ),+ 898 } 899 }; 900 (@struct=>make_proj_replace_ty=>named; 901 [$proj_vis:vis] 902 [] 903 [$make_proj_field:ident] 904 [$ident:ident] 905 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 906 $($field:tt)* 907 ) => {}; 908 // ============================================================================================= 909 // enum:make_proj_ty 910 (@enum=>make_proj_ty; 911 [$proj_vis:vis] 912 [$proj_ty_ident:ident] 913 [$make_proj_field:ident] 914 [$ident:ident] 915 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 916 { 917 $( 918 $variant:ident $({ 919 $( 920 $(#[$pin:ident])? 921 $field:ident: $field_ty:ty 922 ),+ 923 })? 924 ),+ 925 } 926 ) => { 927 #[allow(dead_code)] // This lint warns unused fields/variants. 928 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 929 // This lint warns of `clippy::*` generated by external macros. 930 // We allow this lint for compatibility with older compilers. 931 #[allow(clippy::unknown_clippy_lints)] 932 #[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`. (only needed for project) 933 #[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct. 934 #[allow(clippy::ref_option_ref)] // This lint warns `&Option<&<ty>>`. (only needed for project_ref) 935 #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 936 $proj_vis enum $proj_ty_ident <'__pin, $($impl_generics)*> 937 where 938 $ident <$($ty_generics)*>: '__pin 939 $(, $($where_clause)*)? 940 { 941 $( 942 $variant $({ 943 $( 944 $field: $crate::__pin_project_internal!(@$make_proj_field; 945 $(#[$pin])? $field_ty 946 ) 947 ),+ 948 })? 949 ),+ 950 } 951 }; 952 (@enum=>make_proj_ty; 953 [$proj_vis:vis] 954 [] 955 [$make_proj_field:ident] 956 [$ident:ident] 957 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 958 $($variant:tt)* 959 ) => {}; 960 961 (@enum=>make_proj_replace_ty; 962 [$proj_vis:vis] 963 [$proj_ty_ident:ident] 964 [$make_proj_field:ident] 965 [$ident:ident] 966 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 967 { 968 $( 969 $variant:ident $({ 970 $( 971 $(#[$pin:ident])? 972 $field:ident: $field_ty:ty 973 ),+ 974 })? 975 ),+ 976 } 977 ) => { 978 #[allow(dead_code)] // This lint warns unused fields/variants. 979 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 980 #[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`. (only needed for project) 981 #[allow(clippy::redundant_pub_crate)] // This lint warns `pub(crate)` field in private struct. 982 #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 983 $proj_vis enum $proj_ty_ident <$($impl_generics)*> 984 where 985 $($($where_clause)*)? 986 { 987 $( 988 $variant $({ 989 $( 990 $field: $crate::__pin_project_internal!(@$make_proj_field; 991 $(#[$pin])? $field_ty 992 ) 993 ),+ 994 })? 995 ),+ 996 } 997 }; 998 (@enum=>make_proj_replace_ty; 999 [$proj_vis:vis] 1000 [] 1001 [$make_proj_field:ident] 1002 [$ident:ident] 1003 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 1004 $($variant:tt)* 1005 ) => {}; 1006 1007 // ============================================================================================= 1008 (@make_proj_replace_block; 1009 [$($proj_path: tt)+] 1010 { 1011 $( 1012 $(#[$pin:ident])? 1013 $field_vis:vis $field:ident 1014 ),+ 1015 } 1016 ) => { 1017 let result = $($proj_path)* { 1018 $( 1019 $field: $crate::__pin_project_internal!(@make_replace_field_proj; 1020 $(#[$pin])? $field 1021 ) 1022 ),+ 1023 }; 1024 1025 { 1026 ( $( 1027 $crate::__pin_project_internal!(@make_unsafe_drop_in_place_guard; 1028 $(#[$pin])? $field 1029 ), 1030 )* ); 1031 } 1032 1033 result 1034 }; 1035 (@make_proj_replace_block; 1036 [$($proj_path: tt)+] 1037 ) => { 1038 $($proj_path)* 1039 }; 1040 1041 // ============================================================================================= 1042 // struct:make_proj_method 1043 (@struct=>make_proj_method; 1044 [$proj_vis:vis] 1045 [$proj_ty_ident:ident][$_proj_ty_ident:ident] 1046 [$method_ident:ident $get_method:ident $($mut:ident)?] 1047 [$($ty_generics:tt)*] 1048 { 1049 $( 1050 $(#[$pin:ident])? 1051 $field_vis:vis $field:ident 1052 ),+ 1053 } 1054 ) => { 1055 $proj_vis fn $method_ident<'__pin>( 1056 self: $crate::__private::Pin<&'__pin $($mut)? Self>, 1057 ) -> $proj_ty_ident <'__pin, $($ty_generics)*> { 1058 unsafe { 1059 let Self { $($field),* } = self.$get_method(); 1060 $proj_ty_ident { 1061 $( 1062 $field: $crate::__pin_project_internal!(@make_unsafe_field_proj; 1063 $(#[$pin])? $field 1064 ) 1065 ),+ 1066 } 1067 } 1068 } 1069 }; 1070 (@struct=>make_proj_method; 1071 [$proj_vis:vis] 1072 [][$proj_ty_ident:ident] 1073 [$method_ident:ident $get_method:ident $($mut:ident)?] 1074 [$($ty_generics:tt)*] 1075 $($variant:tt)* 1076 ) => { 1077 $crate::__pin_project_internal! { @struct=>make_proj_method; 1078 [$proj_vis] 1079 [$proj_ty_ident][$proj_ty_ident] 1080 [$method_ident $get_method $($mut)?] 1081 [$($ty_generics)*] 1082 $($variant)* 1083 } 1084 }; 1085 1086 (@struct=>make_proj_replace_method; 1087 [$proj_vis:vis] 1088 [$proj_ty_ident:ident][$_proj_ty_ident:ident] 1089 [$($ty_generics:tt)*] 1090 { 1091 $( 1092 $(#[$pin:ident])? 1093 $field_vis:vis $field:ident 1094 ),+ 1095 } 1096 ) => { 1097 $proj_vis fn project_replace( 1098 self: $crate::__private::Pin<&mut Self>, 1099 replacement: Self, 1100 ) -> $proj_ty_ident <$($ty_generics)*> { 1101 unsafe { 1102 let __self_ptr: *mut Self = self.get_unchecked_mut(); 1103 1104 // Destructors will run in reverse order, so next create a guard to overwrite 1105 // `self` with the replacement value without calling destructors. 1106 let __guard = $crate::__private::UnsafeOverwriteGuard { 1107 target: __self_ptr, 1108 value: $crate::__private::ManuallyDrop::new(replacement), 1109 }; 1110 1111 let Self { $($field),* } = &mut *__self_ptr; 1112 1113 $crate::__pin_project_internal!{@make_proj_replace_block; 1114 [$proj_ty_ident] 1115 { 1116 $( 1117 $(#[$pin])? 1118 $field 1119 ),+ 1120 } 1121 } 1122 } 1123 } 1124 }; 1125 (@struct=>make_proj_replace_method; 1126 [$proj_vis:vis] 1127 [][$proj_ty_ident:ident] 1128 [$($ty_generics:tt)*] 1129 $($variant:tt)* 1130 ) => { 1131 }; 1132 1133 // ============================================================================================= 1134 // enum:make_proj_method 1135 (@enum=>make_proj_method; 1136 [$proj_vis:vis] 1137 [$proj_ty_ident:ident] 1138 [$method_ident:ident $get_method:ident $($mut:ident)?] 1139 [$($ty_generics:tt)*] 1140 { 1141 $( 1142 $variant:ident $({ 1143 $( 1144 $(#[$pin:ident])? 1145 $field:ident 1146 ),+ 1147 })? 1148 ),+ 1149 } 1150 ) => { 1151 $proj_vis fn $method_ident<'__pin>( 1152 self: $crate::__private::Pin<&'__pin $($mut)? Self>, 1153 ) -> $proj_ty_ident <'__pin, $($ty_generics)*> { 1154 unsafe { 1155 match self.$get_method() { 1156 $( 1157 Self::$variant $({ 1158 $($field),+ 1159 })? => { 1160 $proj_ty_ident::$variant $({ 1161 $( 1162 $field: $crate::__pin_project_internal!( 1163 @make_unsafe_field_proj; 1164 $(#[$pin])? $field 1165 ) 1166 ),+ 1167 })? 1168 } 1169 ),+ 1170 } 1171 } 1172 } 1173 }; 1174 (@enum=>make_proj_method; 1175 [$proj_vis:vis] 1176 [] 1177 [$method_ident:ident $get_method:ident $($mut:ident)?] 1178 [$($ty_generics:tt)*] 1179 $($variant:tt)* 1180 ) => {}; 1181 1182 (@enum=>make_proj_replace_method; 1183 [$proj_vis:vis] 1184 [$proj_ty_ident:ident] 1185 [$($ty_generics:tt)*] 1186 { 1187 $( 1188 $variant:ident $({ 1189 $( 1190 $(#[$pin:ident])? 1191 $field:ident 1192 ),+ 1193 })? 1194 ),+ 1195 } 1196 ) => { 1197 $proj_vis fn project_replace( 1198 self: $crate::__private::Pin<&mut Self>, 1199 replacement: Self, 1200 ) -> $proj_ty_ident <$($ty_generics)*> { 1201 unsafe { 1202 let __self_ptr: *mut Self = self.get_unchecked_mut(); 1203 1204 // Destructors will run in reverse order, so next create a guard to overwrite 1205 // `self` with the replacement value without calling destructors. 1206 let __guard = $crate::__private::UnsafeOverwriteGuard { 1207 target: __self_ptr, 1208 value: $crate::__private::ManuallyDrop::new(replacement), 1209 }; 1210 1211 match &mut *__self_ptr { 1212 $( 1213 Self::$variant $({ 1214 $($field),+ 1215 })? => { 1216 $crate::__pin_project_internal!{@make_proj_replace_block; 1217 [$proj_ty_ident :: $variant] 1218 $({ 1219 $( 1220 $(#[$pin])? 1221 $field 1222 ),+ 1223 })? 1224 } 1225 } 1226 ),+ 1227 } 1228 } 1229 } 1230 }; 1231 (@enum=>make_proj_replace_method; 1232 [$proj_vis:vis] 1233 [] 1234 [$($ty_generics:tt)*] 1235 $($variant:tt)* 1236 ) => {}; 1237 1238 // ============================================================================================= 1239 // make_unpin_impl 1240 (@make_unpin_impl; 1241 [$vis:vis $ident:ident] 1242 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 1243 $($field:tt)* 1244 ) => { 1245 // Automatically create the appropriate conditional `Unpin` implementation. 1246 // 1247 // Basically this is equivalent to the following code: 1248 // ```rust 1249 // impl<T, U> Unpin for Struct<T, U> where T: Unpin {} 1250 // ``` 1251 // 1252 // However, if struct is public and there is a private type field, 1253 // this would cause an E0446 (private type in public interface). 1254 // 1255 // When RFC 2145 is implemented (rust-lang/rust#48054), 1256 // this will become a lint, rather then a hard error. 1257 // 1258 // As a workaround for this, we generate a new struct, containing all of the pinned 1259 // fields from our #[pin_project] type. This struct is delcared within 1260 // a function, which makes it impossible to be named by user code. 1261 // This guarnatees that it will use the default auto-trait impl for Unpin - 1262 // that is, it will implement Unpin iff all of its fields implement Unpin. 1263 // This type can be safely declared as 'public', satisfiying the privacy 1264 // checker without actually allowing user code to access it. 1265 // 1266 // This allows users to apply the #[pin_project] attribute to types 1267 // regardless of the privacy of the types of their fields. 1268 // 1269 // See also https://github.com/taiki-e/pin-project/pull/53. 1270 #[allow(non_snake_case)] 1271 $vis struct __Origin <'__pin, $($impl_generics)*> 1272 $(where 1273 $($where_clause)*)? 1274 { 1275 __dummy_lifetime: $crate::__private::PhantomData<&'__pin ()>, 1276 $($field)* 1277 } 1278 impl <'__pin, $($impl_generics)*> $crate::__private::Unpin for $ident <$($ty_generics)*> 1279 where 1280 __Origin <'__pin, $($ty_generics)*>: $crate::__private::Unpin 1281 $(, $($where_clause)*)? 1282 { 1283 } 1284 }; 1285 1286 // ============================================================================================= 1287 // make_drop_impl 1288 (@make_drop_impl; 1289 [$ident:ident] 1290 [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] 1291 ) => { 1292 // Ensure that struct does not implement `Drop`. 1293 // 1294 // There are two possible cases: 1295 // 1. The user type does not implement Drop. In this case, 1296 // the first blanked impl will not apply to it. This code 1297 // will compile, as there is only one impl of MustNotImplDrop for the user type 1298 // 2. The user type does impl Drop. This will make the blanket impl applicable, 1299 // which will then comflict with the explicit MustNotImplDrop impl below. 1300 // This will result in a compilation error, which is exactly what we want. 1301 trait MustNotImplDrop {} 1302 #[allow(clippy::drop_bounds, drop_bounds)] 1303 impl<T: $crate::__private::Drop> MustNotImplDrop for T {} 1304 impl <$($impl_generics)*> MustNotImplDrop for $ident <$($ty_generics)*> 1305 $(where 1306 $($where_clause)*)? 1307 { 1308 } 1309 }; 1310 1311 // ============================================================================================= 1312 // make_unpin_bound 1313 (@make_unpin_bound; 1314 #[pin] 1315 $field_ty:ty 1316 ) => { 1317 $field_ty 1318 }; 1319 (@make_unpin_bound; 1320 $field_ty:ty 1321 ) => { 1322 $crate::__private::AlwaysUnpin<$field_ty> 1323 }; 1324 1325 // ============================================================================================= 1326 // make_unsafe_field_proj 1327 (@make_unsafe_field_proj; 1328 #[pin] 1329 $field:ident 1330 ) => { 1331 $crate::__private::Pin::new_unchecked($field) 1332 }; 1333 (@make_unsafe_field_proj; 1334 $field:ident 1335 ) => { 1336 $field 1337 }; 1338 1339 // ============================================================================================= 1340 // make_replace_field_proj 1341 (@make_replace_field_proj; 1342 #[pin] 1343 $field:ident 1344 ) => { 1345 $crate::__private::PhantomData 1346 }; 1347 (@make_replace_field_proj; 1348 $field:ident 1349 ) => { 1350 $crate::__private::ptr::read($field) 1351 }; 1352 1353 1354 // ============================================================================================= 1355 // make_unsafe_drop_in_place_guard 1356 (@make_unsafe_drop_in_place_guard; 1357 #[pin] 1358 $field:ident 1359 ) => { 1360 $crate::__private::UnsafeDropInPlaceGuard($field) 1361 }; 1362 (@make_unsafe_drop_in_place_guard; 1363 $field:ident 1364 ) => { 1365 () 1366 }; 1367 1368 // ============================================================================================= 1369 // make_proj_field 1370 (@make_proj_field_mut; 1371 #[pin] 1372 $field_ty:ty 1373 ) => { 1374 $crate::__private::Pin<&'__pin mut ($field_ty)> 1375 }; 1376 (@make_proj_field_mut; 1377 $field_ty:ty 1378 ) => { 1379 &'__pin mut ($field_ty) 1380 }; 1381 (@make_proj_field_ref; 1382 #[pin] 1383 $field_ty:ty 1384 ) => { 1385 $crate::__private::Pin<&'__pin ($field_ty)> 1386 }; 1387 (@make_proj_field_ref; 1388 $field_ty:ty 1389 ) => { 1390 &'__pin ($field_ty) 1391 }; 1392 1393 (@make_proj_field_replace; 1394 #[pin] 1395 $field_ty:ty 1396 ) => { 1397 $crate::__private::PhantomData<$field_ty> 1398 }; 1399 (@make_proj_field_replace; 1400 $field_ty:ty 1401 ) => { 1402 $field_ty 1403 }; 1404 1405 // ============================================================================================= 1406 // Parses input and determines visibility 1407 // struct 1408 ( 1409 [$($proj_mut_ident:ident)?] 1410 [$($proj_ref_ident:ident)?] 1411 [$($proj_replace_ident:ident)?] 1412 1413 $(#[$attrs:meta])* 1414 pub struct $ident:ident $(< 1415 $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? 1416 $( $generics:ident 1417 $(: $generics_bound:path)? 1418 $(: ?$generics_unsized_bound:path)? 1419 $(: $generics_lifetime_bound:lifetime)? 1420 $(= $generics_default:ty)? 1421 ),* $(,)? 1422 >)? 1423 $(where 1424 $( $where_clause_ty:ty 1425 $(: $where_clause_bound:path)? 1426 $(: ?$where_clause_unsized_bound:path)? 1427 $(: $where_clause_lifetime_bound:lifetime)? 1428 ),* $(,)? 1429 )? 1430 { 1431 $( 1432 $(#[$pin:ident])? 1433 $field_vis:vis $field:ident: $field_ty:ty 1434 ),+ $(,)? 1435 } 1436 ) => { 1437 $crate::__pin_project_internal! { @struct=>internal; 1438 [$($proj_mut_ident)?] 1439 [$($proj_ref_ident)?] 1440 [$($proj_replace_ident)?] 1441 [pub(crate)] 1442 [$(#[$attrs])* pub struct $ident] 1443 [$(< 1444 $( $lifetime $(: $lifetime_bound)? ,)* 1445 $( $generics 1446 $(: $generics_bound)? 1447 $(: ?$generics_unsized_bound)? 1448 $(: $generics_lifetime_bound)? 1449 $(= $generics_default)? 1450 ),* 1451 >)?] 1452 [$( 1453 $( $lifetime $(: $lifetime_bound)? ,)* 1454 $( $generics 1455 $(: $generics_bound)? 1456 $(: ?$generics_unsized_bound)? 1457 $(: $generics_lifetime_bound)? 1458 ),* 1459 )?] 1460 [$( $( $lifetime ,)* $( $generics ),* )?] 1461 [$(where $( $where_clause_ty 1462 $(: $where_clause_bound)? 1463 $(: ?$where_clause_unsized_bound)? 1464 $(: $where_clause_lifetime_bound)? 1465 ),* )?] 1466 { 1467 $( 1468 $(#[$pin])? 1469 $field_vis $field: $field_ty 1470 ),+ 1471 } 1472 } 1473 }; 1474 ( 1475 [$($proj_mut_ident:ident)?] 1476 [$($proj_ref_ident:ident)?] 1477 [$($proj_replace_ident:ident)?] 1478 1479 $(#[$attrs:meta])* 1480 $vis:vis struct $ident:ident $(< 1481 $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? 1482 $( $generics:ident 1483 $(: $generics_bound:path)? 1484 $(: ?$generics_unsized_bound:path)? 1485 $(: $generics_lifetime_bound:lifetime)? 1486 $(= $generics_default:ty)? 1487 ),* $(,)? 1488 >)? 1489 $(where 1490 $( $where_clause_ty:ty 1491 $(: $where_clause_bound:path)? 1492 $(: ?$where_clause_unsized_bound:path)? 1493 $(: $where_clause_lifetime_bound:lifetime)? 1494 ),* $(,)? 1495 )? 1496 { 1497 $( 1498 $(#[$pin:ident])? 1499 $field_vis:vis $field:ident: $field_ty:ty 1500 ),+ $(,)? 1501 } 1502 ) => { 1503 $crate::__pin_project_internal! { @struct=>internal; 1504 [$($proj_mut_ident)?] 1505 [$($proj_ref_ident)?] 1506 [$($proj_replace_ident)?] 1507 [$vis] 1508 [$(#[$attrs])* $vis struct $ident] 1509 [$(< 1510 $( $lifetime $(: $lifetime_bound)? ,)* 1511 $( $generics 1512 $(: $generics_bound)? 1513 $(: ?$generics_unsized_bound)? 1514 $(: $generics_lifetime_bound)? 1515 $(= $generics_default)? 1516 ),* 1517 >)?] 1518 [$( 1519 $( $lifetime $(: $lifetime_bound)? ,)* 1520 $( $generics 1521 $(: $generics_bound)? 1522 $(: ?$generics_unsized_bound)? 1523 $(: $generics_lifetime_bound)? 1524 ),* 1525 )?] 1526 [$( $( $lifetime ,)* $( $generics ),* )?] 1527 [$(where $( $where_clause_ty 1528 $(: $where_clause_bound)? 1529 $(: ?$where_clause_unsized_bound)? 1530 $(: $where_clause_lifetime_bound)? 1531 ),* )?] 1532 { 1533 $( 1534 $(#[$pin])? 1535 $field_vis $field: $field_ty 1536 ),+ 1537 } 1538 } 1539 }; 1540 // enum 1541 ( 1542 [$($proj_mut_ident:ident)?] 1543 [$($proj_ref_ident:ident)?] 1544 [$($proj_replace_ident:ident)?] 1545 1546 $(#[$attrs:meta])* 1547 pub enum $ident:ident $(< 1548 $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? 1549 $( $generics:ident 1550 $(: $generics_bound:path)? 1551 $(: ?$generics_unsized_bound:path)? 1552 $(: $generics_lifetime_bound:lifetime)? 1553 $(= $generics_default:ty)? 1554 ),* $(,)? 1555 >)? 1556 $(where 1557 $( $where_clause_ty:ty 1558 $(: $where_clause_bound:path)? 1559 $(: ?$where_clause_unsized_bound:path)? 1560 $(: $where_clause_lifetime_bound:lifetime)? 1561 ),* $(,)? 1562 )? 1563 { 1564 $( 1565 $(#[$variant_attrs:meta])* 1566 $variant:ident $({ 1567 $( 1568 $(#[$pin:ident])? 1569 $field:ident: $field_ty:ty 1570 ),+ $(,)? 1571 })? 1572 ),+ $(,)? 1573 } 1574 ) => { 1575 $crate::__pin_project_internal! { @enum=>internal; 1576 [$($proj_mut_ident)?] 1577 [$($proj_ref_ident)?] 1578 [$($proj_replace_ident)?] 1579 [pub(crate)] 1580 [$(#[$attrs])* pub enum $ident] 1581 [$(< 1582 $( $lifetime $(: $lifetime_bound)? ,)* 1583 $( $generics 1584 $(: $generics_bound)? 1585 $(: ?$generics_unsized_bound)? 1586 $(: $generics_lifetime_bound)? 1587 $(= $generics_default)? 1588 ),* 1589 >)?] 1590 [$( 1591 $( $lifetime $(: $lifetime_bound)? ,)* 1592 $( $generics 1593 $(: $generics_bound)? 1594 $(: ?$generics_unsized_bound)? 1595 $(: $generics_lifetime_bound)? 1596 ),* 1597 )?] 1598 [$( $( $lifetime ,)* $( $generics ),* )?] 1599 [$(where $( $where_clause_ty 1600 $(: $where_clause_bound)? 1601 $(: ?$where_clause_unsized_bound)? 1602 $(: $where_clause_lifetime_bound)? 1603 ),* )?] 1604 { 1605 $( 1606 $(#[$variant_attrs])* 1607 $variant $({ 1608 $( 1609 $(#[$pin])? 1610 $field: $field_ty 1611 ),+ 1612 })? 1613 ),+ 1614 } 1615 } 1616 }; 1617 ( 1618 [$($proj_mut_ident:ident)?] 1619 [$($proj_ref_ident:ident)?] 1620 [$($proj_replace_ident:ident)?] 1621 1622 $(#[$attrs:meta])* 1623 $vis:vis enum $ident:ident $(< 1624 $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? 1625 $( $generics:ident 1626 $(: $generics_bound:path)? 1627 $(: ?$generics_unsized_bound:path)? 1628 $(: $generics_lifetime_bound:lifetime)? 1629 $(= $generics_default:ty)? 1630 ),* $(,)? 1631 >)? 1632 $(where 1633 $( $where_clause_ty:ty 1634 $(: $where_clause_bound:path)? 1635 $(: ?$where_clause_unsized_bound:path)? 1636 $(: $where_clause_lifetime_bound:lifetime)? 1637 ),* $(,)? 1638 )? 1639 { 1640 $( 1641 $(#[$variant_attrs:meta])* 1642 $variant:ident $({ 1643 $( 1644 $(#[$pin:ident])? 1645 $field:ident: $field_ty:ty 1646 ),+ $(,)? 1647 })? 1648 ),+ $(,)? 1649 } 1650 ) => { 1651 $crate::__pin_project_internal! { @enum=>internal; 1652 [$($proj_mut_ident)?] 1653 [$($proj_ref_ident)?] 1654 [$($proj_replace_ident)?] 1655 [$vis] 1656 [$(#[$attrs])* $vis enum $ident] 1657 [$(< 1658 $( $lifetime $(: $lifetime_bound)? ,)* 1659 $( $generics 1660 $(: $generics_bound)? 1661 $(: ?$generics_unsized_bound)? 1662 $(: $generics_lifetime_bound)? 1663 $(= $generics_default)? 1664 ),* 1665 >)?] 1666 [$( 1667 $( $lifetime $(: $lifetime_bound)? ,)* 1668 $( $generics 1669 $(: $generics_bound)? 1670 $(: ?$generics_unsized_bound)? 1671 $(: $generics_lifetime_bound)? 1672 ),* 1673 )?] 1674 [$( $( $lifetime ,)* $( $generics ),* )?] 1675 [$(where $( $where_clause_ty 1676 $(: $where_clause_bound)? 1677 $(: ?$where_clause_unsized_bound)? 1678 $(: $where_clause_lifetime_bound)? 1679 ),* )?] 1680 { 1681 $( 1682 $(#[$variant_attrs])* 1683 $variant $({ 1684 $( 1685 $(#[$pin])? 1686 $field: $field_ty 1687 ),+ 1688 })? 1689 ),+ 1690 } 1691 } 1692 }; 1693 } 1694 1695 // Not public API. 1696 #[doc(hidden)] 1697 pub mod __private { 1698 #[doc(hidden)] 1699 pub use core::{ 1700 marker::{PhantomData, Unpin}, 1701 mem::ManuallyDrop, 1702 ops::Drop, 1703 pin::Pin, 1704 ptr, 1705 }; 1706 1707 // This is an internal helper struct used by `pin_project!`. 1708 #[doc(hidden)] 1709 pub struct AlwaysUnpin<T: ?Sized>(PhantomData<T>); 1710 1711 impl<T: ?Sized> Unpin for AlwaysUnpin<T> {} 1712 1713 // This is an internal helper used to ensure a value is dropped. 1714 #[doc(hidden)] 1715 pub struct UnsafeDropInPlaceGuard<T: ?Sized>(pub *mut T); 1716 1717 impl<T: ?Sized> Drop for UnsafeDropInPlaceGuard<T> { drop(&mut self)1718 fn drop(&mut self) { 1719 unsafe { 1720 ptr::drop_in_place(self.0); 1721 } 1722 } 1723 } 1724 1725 // This is an internal helper used to ensure a value is overwritten without 1726 // its destructor being called. 1727 #[doc(hidden)] 1728 pub struct UnsafeOverwriteGuard<T> { 1729 pub value: ManuallyDrop<T>, 1730 pub target: *mut T, 1731 } 1732 1733 impl<T> Drop for UnsafeOverwriteGuard<T> { drop(&mut self)1734 fn drop(&mut self) { 1735 unsafe { 1736 ptr::write(self.target, ptr::read(&*self.value)); 1737 } 1738 } 1739 } 1740 } 1741