mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-29 16:55:34 +00:00
Merge pull request #910 from taiki-e/pin-project-lite
Update pin-project-lite to 0.2.0
This commit is contained in:
commit
73035006dc
3 changed files with 38 additions and 22 deletions
|
@ -71,7 +71,7 @@ log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
|
|||
memchr = { version = "2.3.3", optional = true }
|
||||
num_cpus = { version = "1.12.0", optional = true }
|
||||
once_cell = { version = "1.3.1", optional = true }
|
||||
pin-project-lite = { version = "0.1.4", optional = true }
|
||||
pin-project-lite = { version = "0.2.0", optional = true }
|
||||
pin-utils = { version = "0.1.0-alpha.4", optional = true }
|
||||
slab = { version = "0.4.2", optional = true }
|
||||
|
||||
|
|
|
@ -1,26 +1,39 @@
|
|||
use pin_project_lite::pin_project;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use crate::future::IntoFuture;
|
||||
use crate::task::{ready, Context, Poll};
|
||||
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct FlattenFuture<Fut1, Fut2> {
|
||||
state: State<Fut1, Fut2>,
|
||||
pin_project! {
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct FlattenFuture<Fut1, Fut2> {
|
||||
#[pin]
|
||||
state: State<Fut1, Fut2>,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State<Fut1, Fut2> {
|
||||
First(Fut1),
|
||||
Second(Fut2),
|
||||
Empty,
|
||||
pin_project! {
|
||||
#[project = StateProj]
|
||||
#[derive(Debug)]
|
||||
enum State<Fut1, Fut2> {
|
||||
First {
|
||||
#[pin]
|
||||
fut1: Fut1,
|
||||
},
|
||||
Second {
|
||||
#[pin]
|
||||
fut2: Fut2,
|
||||
},
|
||||
Empty,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut1, Fut2> FlattenFuture<Fut1, Fut2> {
|
||||
pub(crate) fn new(future: Fut1) -> FlattenFuture<Fut1, Fut2> {
|
||||
pub(crate) fn new(fut1: Fut1) -> FlattenFuture<Fut1, Fut2> {
|
||||
FlattenFuture {
|
||||
state: State::First(future),
|
||||
state: State::First { fut1 },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,19 +46,19 @@ where
|
|||
type Output = <Fut1::Output as IntoFuture>::Output;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let Self { state } = unsafe { self.get_unchecked_mut() };
|
||||
let mut state = self.project().state;
|
||||
loop {
|
||||
match state {
|
||||
State::First(fut1) => {
|
||||
let fut2 = ready!(unsafe { Pin::new_unchecked(fut1) }.poll(cx)).into_future();
|
||||
*state = State::Second(fut2);
|
||||
match state.as_mut().project() {
|
||||
StateProj::First { fut1 } => {
|
||||
let fut2 = ready!(fut1.poll(cx)).into_future();
|
||||
state.set(State::Second { fut2 });
|
||||
}
|
||||
State::Second(fut2) => {
|
||||
let v = ready!(unsafe { Pin::new_unchecked(fut2) }.poll(cx));
|
||||
*state = State::Empty;
|
||||
StateProj::Second { fut2 } => {
|
||||
let v = ready!(fut2.poll(cx));
|
||||
state.set(State::Empty);
|
||||
return Poll::Ready(v);
|
||||
}
|
||||
State::Empty => panic!("polled a completed future"),
|
||||
StateProj::Empty => panic!("polled a completed future"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,6 +68,9 @@ pub enum ToSocketAddrsFuture<I> {
|
|||
Done,
|
||||
}
|
||||
|
||||
// The field of `Self::Resolving` is `Unpin`, and the field of `Self::Ready` is never pinned.
|
||||
impl<I> Unpin for ToSocketAddrsFuture<I> {}
|
||||
|
||||
/// Wrap `std::io::Error` with additional message
|
||||
///
|
||||
/// Keeps the original error kind and stores the original I/O error as `source`.
|
||||
|
@ -84,7 +87,7 @@ impl<I: Iterator<Item = SocketAddr>> Future for ToSocketAddrsFuture<I> {
|
|||
type Output = io::Result<I>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = unsafe { self.get_unchecked_mut() };
|
||||
let this = self.get_mut();
|
||||
let state = mem::replace(this, ToSocketAddrsFuture::Done);
|
||||
|
||||
match state {
|
||||
|
|
Loading…
Reference in a new issue