1 // Original code (./struct-default.rs):
2 //
3 // ```rust
4 // #![allow(dead_code)]
5 //
6 // use pin_project::pin_project;
7 //
8 // #[pin_project(project_replace)]
9 // struct Struct<T, U> {
10 //     #[pin]
11 //     pinned: T,
12 //     unpinned: U,
13 // }
14 //
15 // fn main() {}
16 // ```
17 
18 #![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)]
19 #![allow(clippy::needless_lifetimes)]
20 
21 use pin_project::pin_project;
22 
23 // #[pin_project(project_replace)]
24 struct Struct<T, U> {
25     // #[pin]
26     pinned: T,
27     unpinned: U,
28 }
29 
30 const _: () = {
31     struct __StructProjection<'pin, T, U>
32     where
33         Struct<T, U>: 'pin,
34     {
35         pinned: ::pin_project::__private::Pin<&'pin mut (T)>,
36         unpinned: &'pin mut (U),
37     }
38     struct __StructProjectionRef<'pin, T, U>
39     where
40         Struct<T, U>: 'pin,
41     {
42         pinned: ::pin_project::__private::Pin<&'pin (T)>,
43         unpinned: &'pin (U),
44     }
45     struct __StructProjectionOwned<T, U> {
46         pinned: ::pin_project::__private::PhantomData<T>,
47         unpinned: U,
48     }
49 
50     impl<T, U> Struct<T, U> {
project<'pin>( self: ::pin_project::__private::Pin<&'pin mut Self>, ) -> __StructProjection<'pin, T, U>51         fn project<'pin>(
52             self: ::pin_project::__private::Pin<&'pin mut Self>,
53         ) -> __StructProjection<'pin, T, U> {
54             unsafe {
55                 let Self { pinned, unpinned } = self.get_unchecked_mut();
56                 __StructProjection {
57                     pinned: ::pin_project::__private::Pin::new_unchecked(pinned),
58                     unpinned,
59                 }
60             }
61         }
project_ref<'pin>( self: ::pin_project::__private::Pin<&'pin Self>, ) -> __StructProjectionRef<'pin, T, U>62         fn project_ref<'pin>(
63             self: ::pin_project::__private::Pin<&'pin Self>,
64         ) -> __StructProjectionRef<'pin, T, U> {
65             unsafe {
66                 let Self { pinned, unpinned } = self.get_ref();
67                 __StructProjectionRef {
68                     pinned: ::pin_project::__private::Pin::new_unchecked(pinned),
69                     unpinned,
70                 }
71             }
72         }
project_replace( self: ::pin_project::__private::Pin<&mut Self>, __replacement: Self, ) -> __StructProjectionOwned<T, U>73         fn project_replace(
74             self: ::pin_project::__private::Pin<&mut Self>,
75             __replacement: Self,
76         ) -> __StructProjectionOwned<T, U> {
77             unsafe {
78                 let __self_ptr: *mut Self = self.get_unchecked_mut();
79 
80                 // Destructors will run in reverse order, so next create a guard to overwrite
81                 // `self` with the replacement value without calling destructors.
82                 let __guard = ::pin_project::__private::UnsafeOverwriteGuard {
83                     target: __self_ptr,
84                     value: ::pin_project::__private::ManuallyDrop::new(__replacement),
85                 };
86 
87                 let Self { pinned, unpinned } = &mut *__self_ptr;
88 
89                 // First, extract all the unpinned fields
90                 let __result = __StructProjectionOwned {
91                     pinned: ::pin_project::__private::PhantomData,
92                     unpinned: ::pin_project::__private::ptr::read(unpinned),
93                 };
94 
95                 // Now create guards to drop all the pinned fields
96                 //
97                 // Due to a compiler bug (https://github.com/rust-lang/rust/issues/47949)
98                 // this must be in its own scope, or else `__result` will not be dropped
99                 // if any of the destructors panic.
100                 {
101                     let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(pinned);
102                 }
103 
104                 // Finally, return the result
105                 __result
106             }
107         }
108     }
109 
110     // Ensure that it's impossible to use pin projections on a #[repr(packed)]
111     // struct.
112     //
113     // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34
114     // for details.
115     #[forbid(safe_packed_borrows)]
__assert_not_repr_packed<T, U>(this: &Struct<T, U>)116     fn __assert_not_repr_packed<T, U>(this: &Struct<T, U>) {
117         let _ = &this.pinned;
118         let _ = &this.unpinned;
119     }
120 
121     // Automatically create the appropriate conditional `Unpin` implementation.
122     //
123     // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53.
124     // for details.
125     struct __Struct<'pin, T, U> {
126         __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin<
127             'pin,
128             (::pin_project::__private::PhantomData<T>, ::pin_project::__private::PhantomData<U>),
129         >,
130         __field0: T,
131     }
132     impl<'pin, T, U> ::pin_project::__private::Unpin for Struct<T, U> where
133         __Struct<'pin, T, U>: ::pin_project::__private::Unpin
134     {
135     }
136     // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it.
137     #[doc(hidden)]
138     unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Struct<T, U> where
139         __Struct<'pin, T, U>: ::pin_project::__private::Unpin
140     {
141     }
142 
143     // Ensure that struct does not implement `Drop`.
144     //
145     // See ./struct-default-expanded.rs for details.
146     trait StructMustNotImplDrop {}
147     #[allow(clippy::drop_bounds, drop_bounds)]
148     impl<T: ::pin_project::__private::Drop> StructMustNotImplDrop for T {}
149     impl<T, U> StructMustNotImplDrop for Struct<T, U> {}
150     // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally
151     // write a non-functional `PinnedDrop` impls.
152     #[doc(hidden)]
153     impl<T, U> ::pin_project::__private::PinnedDrop for Struct<T, U> {
drop(self: ::pin_project::__private::Pin<&mut Self>)154         unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {}
155     }
156 };
157 
main()158 fn main() {}
159