Skip to content

Commit a3e6870

Browse files
author
Abhishek C. Sharma
committed
Wrap state enum in public struct
1 parent e0910be commit a3e6870

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/future/future/flatten.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ use crate::future::Future;
55
use crate::future::IntoFuture;
66
use crate::task::{Context, Poll};
77

8-
#[doc(hidden)]
98
#[derive(Debug)]
10-
pub enum FlattenFuture<Fut1, Fut2> {
9+
pub struct FlattenFuture<Fut1, Fut2> {
10+
state: State<Fut1, Fut2>,
11+
}
12+
13+
#[derive(Debug)]
14+
enum State<Fut1, Fut2> {
1115
First(Fut1),
1216
Second(Fut2),
1317
Empty,
1418
}
1519

1620
impl<Fut1, Fut2> FlattenFuture<Fut1, Fut2> {
1721
pub fn new(future: Fut1) -> FlattenFuture<Fut1, Fut2> {
18-
FlattenFuture::First(future)
22+
FlattenFuture {
23+
state: State::First(future),
24+
}
1925
}
2026
}
2127

@@ -27,19 +33,19 @@ where
2733
type Output = <Fut1::Output as IntoFuture>::Output;
2834

2935
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
30-
let this = unsafe { self.get_unchecked_mut() };
36+
let Self { state } = unsafe { self.get_unchecked_mut() };
3137
loop {
32-
match this {
33-
FlattenFuture::First(fut1) => {
38+
match state {
39+
State::First(fut1) => {
3440
let fut2 = ready!(unsafe { Pin::new_unchecked(fut1) }.poll(cx)).into_future();
35-
*this = FlattenFuture::Second(fut2);
41+
*state = State::Second(fut2);
3642
}
37-
FlattenFuture::Second(fut2) => {
43+
State::Second(fut2) => {
3844
let v = ready!(unsafe { Pin::new_unchecked(fut2) }.poll(cx));
39-
*this = FlattenFuture::Empty;
45+
*state = State::Empty;
4046
return Poll::Ready(v);
4147
}
42-
FlattenFuture::Empty => unreachable!(),
48+
State::Empty => panic!("polled a completed future"),
4349
}
4450
}
4551
}

0 commit comments

Comments
 (0)