1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! #### Create structural pin projections without unsafe or macros.
//!
//! This like
//! [pin-project-lite](https://docs.rs/pin-project-lite/latest/pin_project_lite/)
//! but more lite.
//!
//! # Why Another Pin Projection Crate?
//!
//! Because you want to create structural pin projections without macros or
//! unsafe for some reason (perhaps for fun?). If you need functionality not
//! supported by this crate, it's worth considering using `pin-project-lite` or
//! `pin-project` instead.
//!
//! # Differences To `pin-project-lite`
//!
//! `pin-project-lite` only supports structs with named fields; `projections`
//! only supports wrapped tuple structs (up to an arity of 7).
//!
//! `pin-project-lite` only projects fields annotated with `#[pin]`;
//! `projections` always projects all fields.
//!
//! `pin-project-lite` might not have the best error messages; `projections`
//! error messages should be relatively good.
//!
//! # Getting Started
//!
//! Structurally pin a tuple inside of an [`Sp`]:
//!
//! ```rust
//! use std::pin::{pin, Pin};
//!
//! use projections::Sp;
//!
//! // Create structurally-pinned type
//! let mut sp: Pin<&mut Sp<(u32, String)>> = pin!(
//! Sp::new((12u32, "Hi".to_string())),
//! );
//!
//! // Project entire inner tuple
//! let _inner: Pin<&(u32, String)> = Sp::get(sp.as_ref());
//! let _inner: Pin<&mut (u32, String)> = Sp::get_mut(sp.as_mut());
//!
//! // Immutable projection of tuple elements
//! let (int, string): (Pin<&u32>, Pin<&String>) = Sp::project(sp.as_ref());
//!
//! assert_eq!(*int.get_ref(), 12);
//! assert_eq!(*string.get_ref(), "Hi");
//!
//! // Mutable projection of tuple elements
//! let (int, string): (Pin<&mut u32>, Pin<&mut String>) = Sp::project_mut(sp);
//!
//! assert_eq!(*int.get_mut(), 12);
//! assert_eq!(string.get_mut(), "Hi");
//! ```
//!
//! ## Orphan Rule: Alloc
//!
//! Due to the orphan rule, either alloc, unsafe, or macros are required to
//! implement [`Future`] or other traits usually requiring pinned references
//! into a structurally-pinned type on a newtype.
//!
//! ```rust
//! use std::{pin::Pin, task::{Context, Poll}};
//!
//! use projections::Sp;
//!
//! pub struct MyFuture<F>(Pin<Box<Sp<(F,)>>>);
//!
//! impl<F> Future for MyFuture<F>
//! where
//! F: Future
//! {
//! type Output = F::Output;
//!
//! fn poll(
//! mut self: Pin<&mut Self>,
//! cx: &mut Context<'_>,
//! ) -> Poll<F::Output> {
//! Sp::project_mut(self.0.as_mut()).0.poll(cx)
//! }
//! }
//!
//! # pasts::Executor::default().block_on(async {
//! let output = MyFuture(Box::pin(Sp::new((async { "uwu" },)))).await;
//!
//! assert_eq!(output, "uwu");
//! # });
//! ```
//!
//! ## Orphan Rule: Unsafe
//!
//! Using `unsafe` to get around the orphan rule (with the [`as_repr`] crate):
//!
//! ```rust
//! use std::{pin::Pin, task::{Context, Poll}};
//!
//! use as_repr::AsRepr;
//! use projections::Sp;
//!
//! // Marker to prevent consumers from invalidating invariants
//! struct PrivateMarker;
//!
//! #[repr(transparent)]
//! pub struct MyFuture<F>(Sp<(F, PrivateMarker)>);
//!
//! // SAFETY: `MyFuture` is `repr(transparent)`
//! unsafe impl<F> AsRepr<Pin<&mut Sp<(F, PrivateMarker)>>>
//! for Pin<&mut MyFuture<F>>
//! {}
//!
//! impl<F> Future for MyFuture<F>
//! where
//! F: Future
//! {
//! type Output = F::Output;
//!
//! fn poll(
//! mut self: Pin<&mut Self>,
//! cx: &mut Context<'_>,
//! ) -> Poll<F::Output> {
//! let mut sp: Pin<&mut Sp<(F, PrivateMarker)>>
//! = as_repr::as_repr(self);
//!
//! Sp::project_mut(sp.as_mut()).0.poll(cx)
//! }
//! }
//!
//! # pasts::Executor::default().block_on(async {
//! let output = MyFuture(Sp::new((async { "uwu" }, PrivateMarker))).await;
//!
//! assert_eq!(output, "uwu");
//! # });
//! ```
//!
//! ## Orphan Rule: Macros
//!
//! Using macros to get around the orphan rule (with the [`as_repr`] crate):
//!
//! ```rust
//! use std::{pin::Pin, task::{Context, Poll}};
//!
//! use as_repr::AsRepr;
//! use projections::Sp;
//!
//! // Marker to prevent consumers from invalidating invariants
//! struct PrivateMarker;
//!
//! as_repr::transparent_newtype! {
//! pub struct MyFuture<F>(Sp<(F, PrivateMarker)>);
//! }
//!
//! impl<F> Future for MyFuture<F>
//! where
//! F: Future
//! {
//! type Output = F::Output;
//!
//! fn poll(
//! mut self: Pin<&mut Self>,
//! cx: &mut Context<'_>,
//! ) -> Poll<F::Output> {
//! let mut sp: Pin<&mut Sp<(F, PrivateMarker)>>
//! = as_repr::as_repr(self);
//!
//! Sp::project_mut(sp.as_mut()).0.poll(cx)
//! }
//! }
//!
//! # pasts::Executor::default().block_on(async {
//! let output = MyFuture(Sp::new((async { "uwu" }, PrivateMarker))).await;
//!
//! assert_eq!(output, "uwu");
//! # });
//! ```
//!
//! [`as_repr`]: https://docs.rs/as_repr
pub use ;