Skip to content

Commit da71932

Browse files
committed
futures-util: Migrate from pin-project to pin-project-lite
1 parent 1439cfe commit da71932

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1092
-977
lines changed

futures-util/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ memchr = { version = "2.2", optional = true }
4646
futures_01 = { version = "0.1.25", optional = true, package = "futures" }
4747
tokio-io = { version = "0.1.9", optional = true }
4848
pin-utils = "0.1.0"
49-
pin-project = "1.0.1"
49+
pin-project-lite = "0.2"
5050

5151
[dev-dependencies]
5252
futures = { path = "../futures", version = "0.3.8", features = ["async-await", "thread-pool"] }

futures-util/src/future/abortable.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use core::fmt;
55
use core::pin::Pin;
66
use core::sync::atomic::{AtomicBool, Ordering};
77
use alloc::sync::Arc;
8-
use pin_project::pin_project;
9-
10-
/// A future which can be remotely short-circuited using an `AbortHandle`.
11-
#[pin_project]
12-
#[derive(Debug, Clone)]
13-
#[must_use = "futures do nothing unless you `.await` or poll them"]
14-
pub struct Abortable<Fut> {
15-
#[pin]
16-
future: Fut,
17-
inner: Arc<AbortInner>,
8+
use pin_project_lite::pin_project;
9+
10+
pin_project! {
11+
/// A future which can be remotely short-circuited using an `AbortHandle`.
12+
#[derive(Debug, Clone)]
13+
#[must_use = "futures do nothing unless you `.await` or poll them"]
14+
pub struct Abortable<Fut> {
15+
#[pin]
16+
future: Fut,
17+
inner: Arc<AbortInner>,
18+
}
1819
}
1920

2021
impl<Fut> Abortable<Fut> where Fut: Future {

futures-util/src/future/either.rs

+43-34
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@ use futures_core::future::{FusedFuture, Future};
44
use futures_core::stream::{FusedStream, Stream};
55
#[cfg(feature = "sink")]
66
use futures_sink::Sink;
7-
use pin_project::pin_project;
87

98
/// Combines two different futures, streams, or sinks having the same associated types into a single
109
/// type.
11-
#[pin_project(project = EitherProj)]
1210
#[derive(Debug, Clone)]
1311
pub enum Either<A, B> {
1412
/// First branch of the type
15-
Left(#[pin] A),
13+
Left(A),
1614
/// Second branch of the type
17-
Right(#[pin] B),
15+
Right(B),
16+
}
17+
18+
impl<A, B> Either<A, B> {
19+
fn project(self: Pin<&mut Self>) -> Either<Pin<&mut A>, Pin<&mut B>> {
20+
unsafe {
21+
match self.get_unchecked_mut() {
22+
Either::Left(a) => Either::Left(Pin::new_unchecked(a)),
23+
Either::Right(b) => Either::Right(Pin::new_unchecked(b)),
24+
}
25+
}
26+
}
1827
}
1928

2029
impl<A, B, T> Either<(T, A), (T, B)> {
@@ -60,8 +69,8 @@ where
6069

6170
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
6271
match self.project() {
63-
EitherProj::Left(x) => x.poll(cx),
64-
EitherProj::Right(x) => x.poll(cx),
72+
Either::Left(x) => x.poll(cx),
73+
Either::Right(x) => x.poll(cx),
6574
}
6675
}
6776
}
@@ -88,8 +97,8 @@ where
8897

8998
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
9099
match self.project() {
91-
EitherProj::Left(x) => x.poll_next(cx),
92-
EitherProj::Right(x) => x.poll_next(cx),
100+
Either::Left(x) => x.poll_next(cx),
101+
Either::Right(x) => x.poll_next(cx),
93102
}
94103
}
95104
}
@@ -117,29 +126,29 @@ where
117126

118127
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
119128
match self.project() {
120-
EitherProj::Left(x) => x.poll_ready(cx),
121-
EitherProj::Right(x) => x.poll_ready(cx),
129+
Either::Left(x) => x.poll_ready(cx),
130+
Either::Right(x) => x.poll_ready(cx),
122131
}
123132
}
124133

125134
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
126135
match self.project() {
127-
EitherProj::Left(x) => x.start_send(item),
128-
EitherProj::Right(x) => x.start_send(item),
136+
Either::Left(x) => x.start_send(item),
137+
Either::Right(x) => x.start_send(item),
129138
}
130139
}
131140

132141
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
133142
match self.project() {
134-
EitherProj::Left(x) => x.poll_flush(cx),
135-
EitherProj::Right(x) => x.poll_flush(cx),
143+
Either::Left(x) => x.poll_flush(cx),
144+
Either::Right(x) => x.poll_flush(cx),
136145
}
137146
}
138147

139148
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
140149
match self.project() {
141-
EitherProj::Left(x) => x.poll_close(cx),
142-
EitherProj::Right(x) => x.poll_close(cx),
150+
Either::Left(x) => x.poll_close(cx),
151+
Either::Right(x) => x.poll_close(cx),
143152
}
144153
}
145154
}
@@ -176,8 +185,8 @@ mod if_std {
176185
buf: &mut [u8],
177186
) -> Poll<Result<usize>> {
178187
match self.project() {
179-
EitherProj::Left(x) => x.poll_read(cx, buf),
180-
EitherProj::Right(x) => x.poll_read(cx, buf),
188+
Either::Left(x) => x.poll_read(cx, buf),
189+
Either::Right(x) => x.poll_read(cx, buf),
181190
}
182191
}
183192

@@ -187,8 +196,8 @@ mod if_std {
187196
bufs: &mut [IoSliceMut<'_>],
188197
) -> Poll<Result<usize>> {
189198
match self.project() {
190-
EitherProj::Left(x) => x.poll_read_vectored(cx, bufs),
191-
EitherProj::Right(x) => x.poll_read_vectored(cx, bufs),
199+
Either::Left(x) => x.poll_read_vectored(cx, bufs),
200+
Either::Right(x) => x.poll_read_vectored(cx, bufs),
192201
}
193202
}
194203
}
@@ -204,8 +213,8 @@ mod if_std {
204213
buf: &[u8],
205214
) -> Poll<Result<usize>> {
206215
match self.project() {
207-
EitherProj::Left(x) => x.poll_write(cx, buf),
208-
EitherProj::Right(x) => x.poll_write(cx, buf),
216+
Either::Left(x) => x.poll_write(cx, buf),
217+
Either::Right(x) => x.poll_write(cx, buf),
209218
}
210219
}
211220

@@ -215,22 +224,22 @@ mod if_std {
215224
bufs: &[IoSlice<'_>],
216225
) -> Poll<Result<usize>> {
217226
match self.project() {
218-
EitherProj::Left(x) => x.poll_write_vectored(cx, bufs),
219-
EitherProj::Right(x) => x.poll_write_vectored(cx, bufs),
227+
Either::Left(x) => x.poll_write_vectored(cx, bufs),
228+
Either::Right(x) => x.poll_write_vectored(cx, bufs),
220229
}
221230
}
222231

223232
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
224233
match self.project() {
225-
EitherProj::Left(x) => x.poll_flush(cx),
226-
EitherProj::Right(x) => x.poll_flush(cx),
234+
Either::Left(x) => x.poll_flush(cx),
235+
Either::Right(x) => x.poll_flush(cx),
227236
}
228237
}
229238

230239
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
231240
match self.project() {
232-
EitherProj::Left(x) => x.poll_close(cx),
233-
EitherProj::Right(x) => x.poll_close(cx),
241+
Either::Left(x) => x.poll_close(cx),
242+
Either::Right(x) => x.poll_close(cx),
234243
}
235244
}
236245
}
@@ -246,8 +255,8 @@ mod if_std {
246255
pos: SeekFrom,
247256
) -> Poll<Result<u64>> {
248257
match self.project() {
249-
EitherProj::Left(x) => x.poll_seek(cx, pos),
250-
EitherProj::Right(x) => x.poll_seek(cx, pos),
258+
Either::Left(x) => x.poll_seek(cx, pos),
259+
Either::Right(x) => x.poll_seek(cx, pos),
251260
}
252261
}
253262
}
@@ -259,15 +268,15 @@ mod if_std {
259268
{
260269
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
261270
match self.project() {
262-
EitherProj::Left(x) => x.poll_fill_buf(cx),
263-
EitherProj::Right(x) => x.poll_fill_buf(cx),
271+
Either::Left(x) => x.poll_fill_buf(cx),
272+
Either::Right(x) => x.poll_fill_buf(cx),
264273
}
265274
}
266275

267276
fn consume(self: Pin<&mut Self>, amt: usize) {
268277
match self.project() {
269-
EitherProj::Left(x) => x.consume(amt),
270-
EitherProj::Right(x) => x.consume(amt),
278+
Either::Left(x) => x.consume(amt),
279+
Either::Right(x) => x.consume(amt),
271280
}
272281
}
273282
}

futures-util/src/future/future/catch_unwind.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ use std::panic::{catch_unwind, UnwindSafe, AssertUnwindSafe};
44

55
use futures_core::future::Future;
66
use futures_core::task::{Context, Poll};
7-
use pin_project::pin_project;
7+
use pin_project_lite::pin_project;
88

9-
/// Future for the [`catch_unwind`](super::FutureExt::catch_unwind) method.
10-
#[pin_project]
11-
#[derive(Debug)]
12-
#[must_use = "futures do nothing unless you `.await` or poll them"]
13-
pub struct CatchUnwind<Fut>(#[pin] Fut);
9+
pin_project! {
10+
/// Future for the [`catch_unwind`](super::FutureExt::catch_unwind) method.
11+
#[derive(Debug)]
12+
#[must_use = "futures do nothing unless you `.await` or poll them"]
13+
pub struct CatchUnwind<Fut> {
14+
#[pin]
15+
future: Fut,
16+
}
17+
}
1418

1519
impl<Fut> CatchUnwind<Fut> where Fut: Future + UnwindSafe {
1620
pub(super) fn new(future: Fut) -> Self {
17-
Self(future)
21+
Self { future }
1822
}
1923
}
2024

@@ -24,7 +28,7 @@ impl<Fut> Future for CatchUnwind<Fut>
2428
type Output = Result<Fut::Output, Box<dyn Any + Send>>;
2529

2630
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
27-
let f = self.project().0;
31+
let f = self.project().future;
2832
catch_unwind(AssertUnwindSafe(|| f.poll(cx)))?.map(Ok)
2933
}
3034
}

futures-util/src/future/future/flatten.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ use futures_core::stream::{FusedStream, Stream};
55
#[cfg(feature = "sink")]
66
use futures_sink::Sink;
77
use futures_core::task::{Context, Poll};
8-
use pin_project::pin_project;
8+
use pin_project_lite::pin_project;
99

10-
#[pin_project(project = FlattenProj)]
11-
#[derive(Debug)]
12-
pub enum Flatten<Fut1, Fut2> {
13-
First(#[pin] Fut1),
14-
Second(#[pin] Fut2),
15-
Empty,
10+
pin_project! {
11+
#[project = FlattenProj]
12+
#[derive(Debug)]
13+
pub enum Flatten<Fut1, Fut2> {
14+
First { #[pin] f: Fut1 },
15+
Second { #[pin] f: Fut2 },
16+
Empty,
17+
}
1618
}
1719

1820
impl<Fut1, Fut2> Flatten<Fut1, Fut2> {
1921
pub(crate) fn new(future: Fut1) -> Self {
20-
Self::First(future)
22+
Self::First { f: future }
2123
}
2224
}
2325

@@ -42,11 +44,11 @@ impl<Fut> Future for Flatten<Fut, Fut::Output>
4244
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
4345
Poll::Ready(loop {
4446
match self.as_mut().project() {
45-
FlattenProj::First(f) => {
47+
FlattenProj::First { f } => {
4648
let f = ready!(f.poll(cx));
47-
self.set(Self::Second(f));
49+
self.set(Self::Second { f });
4850
},
49-
FlattenProj::Second(f) => {
51+
FlattenProj::Second { f } => {
5052
let output = ready!(f.poll(cx));
5153
self.set(Self::Empty);
5254
break output;
@@ -78,11 +80,11 @@ impl<Fut> Stream for Flatten<Fut, Fut::Output>
7880
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
7981
Poll::Ready(loop {
8082
match self.as_mut().project() {
81-
FlattenProj::First(f) => {
83+
FlattenProj::First { f } => {
8284
let f = ready!(f.poll(cx));
83-
self.set(Self::Second(f));
85+
self.set(Self::Second { f });
8486
},
85-
FlattenProj::Second(f) => {
87+
FlattenProj::Second { f } => {
8688
let output = ready!(f.poll_next(cx));
8789
if output.is_none() {
8890
self.set(Self::Empty);
@@ -110,11 +112,11 @@ where
110112
) -> Poll<Result<(), Self::Error>> {
111113
Poll::Ready(loop {
112114
match self.as_mut().project() {
113-
FlattenProj::First(f) => {
115+
FlattenProj::First { f } => {
114116
let f = ready!(f.poll(cx));
115-
self.set(Self::Second(f));
117+
self.set(Self::Second { f });
116118
},
117-
FlattenProj::Second(f) => {
119+
FlattenProj::Second { f } => {
118120
break ready!(f.poll_ready(cx));
119121
},
120122
FlattenProj::Empty => panic!("poll_ready called after eof"),
@@ -124,16 +126,16 @@ where
124126

125127
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
126128
match self.project() {
127-
FlattenProj::First(_) => panic!("poll_ready not called first"),
128-
FlattenProj::Second(f) => f.start_send(item),
129+
FlattenProj::First { .. } => panic!("poll_ready not called first"),
130+
FlattenProj::Second { f } => f.start_send(item),
129131
FlattenProj::Empty => panic!("start_send called after eof"),
130132
}
131133
}
132134

133135
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
134136
match self.project() {
135-
FlattenProj::First(_) => Poll::Ready(Ok(())),
136-
FlattenProj::Second(f) => f.poll_flush(cx),
137+
FlattenProj::First { .. } => Poll::Ready(Ok(())),
138+
FlattenProj::Second { f } => f.poll_flush(cx),
137139
FlattenProj::Empty => panic!("poll_flush called after eof"),
138140
}
139141
}
@@ -143,7 +145,7 @@ where
143145
cx: &mut Context<'_>,
144146
) -> Poll<Result<(), Self::Error>> {
145147
let res = match self.as_mut().project() {
146-
FlattenProj::Second(f) => f.poll_close(cx),
148+
FlattenProj::Second { f } => f.poll_close(cx),
147149
_ => Poll::Ready(Ok(())),
148150
};
149151
if res.is_ready() {

0 commit comments

Comments
 (0)