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