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
21use pin_project::pin_project;
22
23// #[pin_project(project_replace)]
24struct Struct<T, U> {
25 // #[pin]
26 pinned: T,
27 unpinned: U,
28}
29
30const _: () = {
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> {
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 }
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 }
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 =
83 ::pin_project::__private::UnsafeOverwriteGuard::new(__self_ptr, __replacement);
84
85 let Self { pinned, unpinned } = &mut *__self_ptr;
86
87 // First, extract all the unpinned fields
88 let __result = __StructProjectionOwned {
89 pinned: ::pin_project::__private::PhantomData,
90 unpinned: ::pin_project::__private::ptr::read(unpinned),
91 };
92
93 // Now create guards to drop all the pinned fields
94 //
95 // Due to a compiler bug (https://github.com/rust-lang/rust/issues/47949)
96 // this must be in its own scope, or else `__result` will not be dropped
97 // if any of the destructors panic.
98 {
99 let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard::new(pinned);
100 }
101
102 // Finally, return the result
103 __result
104 }
105 }
106 }
107
108 // Ensure that it's impossible to use pin projections on a #[repr(packed)]
109 // struct.
110 //
111 // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34
112 // for details.
113 #[forbid(unaligned_references, safe_packed_borrows)]
114 fn __assert_not_repr_packed<T, U>(this: &Struct<T, U>) {
115 let _ = &this.pinned;
116 let _ = &this.unpinned;
117 }
118
119 // Automatically create the appropriate conditional `Unpin` implementation.
120 //
121 // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53.
122 // for details.
123 struct __Struct<'pin, T, U> {
124 __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin<
125 'pin,
126 (::pin_project::__private::PhantomData<T>, ::pin_project::__private::PhantomData<U>),
127 >,
128 __field0: T,
129 }
130 impl<'pin, T, U> ::pin_project::__private::Unpin for Struct<T, U> where
131 __Struct<'pin, T, U>: ::pin_project::__private::Unpin
132 {
133 }
134 // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it.
135 #[doc(hidden)]
136 unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Struct<T, U> where
137 __Struct<'pin, T, U>: ::pin_project::__private::Unpin
138 {
139 }
140
141 // Ensure that struct does not implement `Drop`.
142 //
143 // See ./struct-default-expanded.rs for details.
144 trait StructMustNotImplDrop {}
145 #[allow(clippy::drop_bounds, drop_bounds)]
146 impl<T: ::pin_project::__private::Drop> StructMustNotImplDrop for T {}
147 impl<T, U> StructMustNotImplDrop for Struct<T, U> {}
148 // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally
149 // write a non-functional `PinnedDrop` impls.
150 #[doc(hidden)]
151 impl<T, U> ::pin_project::__private::PinnedDrop for Struct<T, U> {
152 unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {}
153 }
154};
155
156fn main() {}
157