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