2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-30 09:15:33 +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:
Yoshua Wuyts 2020-11-22 12:39:01 +01:00 committed by GitHub
commit 73035006dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 22 deletions

View file

@ -71,7 +71,7 @@ log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
memchr = { version = "2.3.3", optional = true } memchr = { version = "2.3.3", optional = true }
num_cpus = { version = "1.12.0", optional = true } num_cpus = { version = "1.12.0", optional = true }
once_cell = { version = "1.3.1", 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 } pin-utils = { version = "0.1.0-alpha.4", optional = true }
slab = { version = "0.4.2", optional = true } slab = { version = "0.4.2", optional = true }

View file

@ -1,26 +1,39 @@
use pin_project_lite::pin_project;
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use crate::future::IntoFuture; use crate::future::IntoFuture;
use crate::task::{ready, Context, Poll}; use crate::task::{ready, Context, Poll};
#[doc(hidden)] pin_project! {
#[allow(missing_debug_implementations)] #[doc(hidden)]
pub struct FlattenFuture<Fut1, Fut2> { #[allow(missing_debug_implementations)]
state: State<Fut1, Fut2>, pub struct FlattenFuture<Fut1, Fut2> {
#[pin]
state: State<Fut1, Fut2>,
}
} }
#[derive(Debug)] pin_project! {
enum State<Fut1, Fut2> { #[project = StateProj]
First(Fut1), #[derive(Debug)]
Second(Fut2), enum State<Fut1, Fut2> {
Empty, First {
#[pin]
fut1: Fut1,
},
Second {
#[pin]
fut2: Fut2,
},
Empty,
}
} }
impl<Fut1, Fut2> FlattenFuture<Fut1, Fut2> { 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 { FlattenFuture {
state: State::First(future), state: State::First { fut1 },
} }
} }
} }
@ -33,19 +46,19 @@ where
type Output = <Fut1::Output as IntoFuture>::Output; type Output = <Fut1::Output as IntoFuture>::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::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 { loop {
match state { match state.as_mut().project() {
State::First(fut1) => { StateProj::First { fut1 } => {
let fut2 = ready!(unsafe { Pin::new_unchecked(fut1) }.poll(cx)).into_future(); let fut2 = ready!(fut1.poll(cx)).into_future();
*state = State::Second(fut2); state.set(State::Second { fut2 });
} }
State::Second(fut2) => { StateProj::Second { fut2 } => {
let v = ready!(unsafe { Pin::new_unchecked(fut2) }.poll(cx)); let v = ready!(fut2.poll(cx));
*state = State::Empty; state.set(State::Empty);
return Poll::Ready(v); return Poll::Ready(v);
} }
State::Empty => panic!("polled a completed future"), StateProj::Empty => panic!("polled a completed future"),
} }
} }
} }

View file

@ -68,6 +68,9 @@ pub enum ToSocketAddrsFuture<I> {
Done, 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 /// Wrap `std::io::Error` with additional message
/// ///
/// Keeps the original error kind and stores the original I/O error as `source`. /// 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>; type Output = io::Result<I>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 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); let state = mem::replace(this, ToSocketAddrsFuture::Done);
match state { match state {