1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 // Original code (./enum-default.rs):
4 //
5 // ```
6 // #![allow(dead_code)]
7 //
8 // use pin_project::pin_project;
9 //
10 // #[pin_project(project = EnumProj)]
11 // enum Enum<T, U> {
12 // Pinned(#[pin] T),
13 // Unpinned(U),
14 // }
15 //
16 // fn main() {}
17 // ```
18
19 #![allow(
20 dead_code,
21 unused_imports,
22 unused_parens,
23 unknown_lints,
24 renamed_and_removed_lints,
25 clippy::just_underscores_and_digits,
26 clippy::needless_lifetimes,
27 clippy::undocumented_unsafe_blocks,
28 clippy::used_underscore_binding
29 )]
30
31 use pin_project::pin_project;
32
33 // #[pin_project(project = EnumProj)]
34 enum Enum<T, U> {
35 Pinned(/* #[pin] */ T),
36 Unpinned(U),
37 }
38
39 enum EnumProj<'pin, T, U>
40 where
41 Enum<T, U>: 'pin,
42 {
43 Pinned(::pin_project::__private::Pin<&'pin mut (T)>),
44 Unpinned(&'pin mut (U)),
45 }
46
47 const _: () = {
48 // When `#[pin_project]` is used on enums, only named projection types and
49 // methods are generated because there is no way to access variants of
50 // projected types without naming it.
51 // (When `#[pin_project]` is used on structs, both methods are always generated.)
52
53 impl<T, U> Enum<T, U> {
project<'pin>( self: ::pin_project::__private::Pin<&'pin mut Self>, ) -> EnumProj<'pin, T, U>54 fn project<'pin>(
55 self: ::pin_project::__private::Pin<&'pin mut Self>,
56 ) -> EnumProj<'pin, T, U> {
57 unsafe {
58 match self.get_unchecked_mut() {
59 Self::Pinned(_0) => {
60 EnumProj::Pinned(::pin_project::__private::Pin::new_unchecked(_0))
61 }
62 Self::Unpinned(_0) => EnumProj::Unpinned(_0),
63 }
64 }
65 }
66 }
67
68 // Automatically create the appropriate conditional `Unpin` implementation.
69 //
70 // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53.
71 // for details.
72 struct __Enum<'pin, T, U> {
73 __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin<
74 'pin,
75 (::pin_project::__private::PhantomData<T>, ::pin_project::__private::PhantomData<U>),
76 >,
77 __field0: T,
78 }
79 impl<'pin, T, U> ::pin_project::__private::Unpin for Enum<T, U> where
80 ::pin_project::__private::PinnedFieldsOf<__Enum<'pin, T, U>>:
81 ::pin_project::__private::Unpin
82 {
83 }
84 // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it.
85 #[doc(hidden)]
86 unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Enum<T, U> where
87 ::pin_project::__private::PinnedFieldsOf<__Enum<'pin, T, U>>:
88 ::pin_project::__private::Unpin
89 {
90 }
91
92 // Ensure that enum does not implement `Drop`.
93 //
94 // See ./struct-default-expanded.rs for details.
95 trait EnumMustNotImplDrop {}
96 #[allow(clippy::drop_bounds, drop_bounds)]
97 impl<T: ::pin_project::__private::Drop> EnumMustNotImplDrop for T {}
98 impl<T, U> EnumMustNotImplDrop for Enum<T, U> {}
99 // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally
100 // write a non-functional `PinnedDrop` impls.
101 #[doc(hidden)]
102 impl<T, U> ::pin_project::__private::PinnedDrop for Enum<T, U> {
drop(self: ::pin_project::__private::Pin<&mut Self>)103 unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {}
104 }
105
106 // We don't need to check for `#[repr(packed)]`,
107 // since it does not apply to enums.
108 };
109
main()110 fn main() {}
111