diff --git a/.gitignore b/.gitignore index c6e75317cb..875fece1ce 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ target/ Cargo.lock _site .sass-cache +.idea +.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 3ef7820b62..bdeb2e5e9b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ matrix: # When updating this, the reminder to update the minimum required version in README.md. - name: cargo test (minimum required version) - rust: nightly-2019-01-11 + rust: nightly-2019-02-15 - name: cargo clippy rust: nightly diff --git a/README.md b/README.md index b78112b6c3..bf50b5045a 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Now, you can use futures-rs: use futures::future::Future; // Note: It's not `futures_preview` ``` -The current version of futures-rs requires Rust nightly 2019-01-11 or later. +The current version of futures-rs requires Rust nightly 2019-02-15 or later. ### Feature `std` diff --git a/futures-channel/benches/sync_mpsc.rs b/futures-channel/benches/sync_mpsc.rs index 28662568b1..9df44b32a9 100644 --- a/futures-channel/benches/sync_mpsc.rs +++ b/futures-channel/benches/sync_mpsc.rs @@ -9,16 +9,16 @@ use { ready, stream::{Stream, StreamExt}, sink::Sink, - task::{LocalWaker, Poll}, + task::{Waker, Poll}, }, - futures_test::task::noop_local_waker_ref, + futures_test::task::noop_waker_ref, std::pin::Pin, }; /// Single producer, single consumer #[bench] fn unbounded_1_tx(b: &mut Bencher) { - let lw = noop_local_waker_ref(); + let waker = noop_waker_ref(); b.iter(|| { let (tx, mut rx) = mpsc::unbounded(); @@ -27,12 +27,12 @@ fn unbounded_1_tx(b: &mut Bencher) { for i in 0..1000 { // Poll, not ready, park - assert_eq!(Poll::Pending, rx.poll_next_unpin(lw)); + assert_eq!(Poll::Pending, rx.poll_next_unpin(waker)); UnboundedSender::unbounded_send(&tx, i).unwrap(); // Now poll ready - assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(waker)); } }) } @@ -40,7 +40,7 @@ fn unbounded_1_tx(b: &mut Bencher) { /// 100 producers, single consumer #[bench] fn unbounded_100_tx(b: &mut Bencher) { - let lw = noop_local_waker_ref(); + let waker = noop_waker_ref(); b.iter(|| { let (tx, mut rx) = mpsc::unbounded(); @@ -49,11 +49,11 @@ fn unbounded_100_tx(b: &mut Bencher) { // 1000 send/recv operations total, result should be divided by 1000 for _ in 0..10 { for i in 0..tx.len() { - assert_eq!(Poll::Pending, rx.poll_next_unpin(lw)); + assert_eq!(Poll::Pending, rx.poll_next_unpin(waker)); UnboundedSender::unbounded_send(&tx[i], i).unwrap(); - assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(waker)); } } }) @@ -61,14 +61,14 @@ fn unbounded_100_tx(b: &mut Bencher) { #[bench] fn unbounded_uncontended(b: &mut Bencher) { - let lw = noop_local_waker_ref(); + let waker = noop_waker_ref(); b.iter(|| { let (tx, mut rx) = mpsc::unbounded(); for i in 0..1000 { UnboundedSender::unbounded_send(&tx, i).expect("send"); // No need to create a task, because poll is not going to park. - assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(waker)); } }) } @@ -84,16 +84,16 @@ struct TestSender { impl Stream for TestSender { type Item = u32; - fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) + fn poll_next(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { let this = &mut *self; let mut tx = Pin::new(&mut this.tx); - ready!(tx.as_mut().poll_ready(lw)).unwrap(); + ready!(tx.as_mut().poll_ready(waker)).unwrap(); tx.as_mut().start_send(this.last + 1).unwrap(); this.last += 1; - assert_eq!(Poll::Ready(Ok(())), tx.as_mut().poll_flush(lw)); + assert_eq!(Poll::Ready(Ok(())), tx.as_mut().poll_flush(waker)); Poll::Ready(Some(this.last)) } } @@ -101,16 +101,16 @@ impl Stream for TestSender { /// Single producers, single consumer #[bench] fn bounded_1_tx(b: &mut Bencher) { - let lw = noop_local_waker_ref(); + let waker = noop_waker_ref(); b.iter(|| { let (tx, mut rx) = mpsc::channel(0); let mut tx = TestSender { tx, last: 0 }; for i in 0..1000 { - assert_eq!(Poll::Ready(Some(i + 1)), tx.poll_next_unpin(lw)); - assert_eq!(Poll::Pending, tx.poll_next_unpin(lw)); - assert_eq!(Poll::Ready(Some(i + 1)), rx.poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i + 1)), tx.poll_next_unpin(waker)); + assert_eq!(Poll::Pending, tx.poll_next_unpin(waker)); + assert_eq!(Poll::Ready(Some(i + 1)), rx.poll_next_unpin(waker)); } }) } @@ -118,7 +118,7 @@ fn bounded_1_tx(b: &mut Bencher) { /// 100 producers, single consumer #[bench] fn bounded_100_tx(b: &mut Bencher) { - let lw = noop_local_waker_ref(); + let waker = noop_waker_ref(); b.iter(|| { // Each sender can send one item after specified capacity let (tx, mut rx) = mpsc::channel(0); @@ -133,11 +133,11 @@ fn bounded_100_tx(b: &mut Bencher) { for i in 0..10 { for j in 0..tx.len() { // Send an item - assert_eq!(Poll::Ready(Some(i + 1)), tx[j].poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i + 1)), tx[j].poll_next_unpin(waker)); // Then block - assert_eq!(Poll::Pending, tx[j].poll_next_unpin(lw)); + assert_eq!(Poll::Pending, tx[j].poll_next_unpin(waker)); // Recv the item - assert_eq!(Poll::Ready(Some(i + 1)), rx.poll_next_unpin(lw)); + assert_eq!(Poll::Ready(Some(i + 1)), rx.poll_next_unpin(waker)); } } }) diff --git a/futures-channel/src/mpsc/mod.rs b/futures-channel/src/mpsc/mod.rs index cd34f3569c..c1ac37e8f8 100644 --- a/futures-channel/src/mpsc/mod.rs +++ b/futures-channel/src/mpsc/mod.rs @@ -79,7 +79,7 @@ // by the queue structure. use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Waker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_core::task::__internal::AtomicWaker; use std::any::Any; use std::error::Error; @@ -555,7 +555,7 @@ impl SenderInner { /// - `Err(SendError)` if the receiver has been dropped. fn poll_ready( &mut self, - lw: &LocalWaker + waker: &Waker ) -> Poll> { let state = decode_state(self.inner.state.load(SeqCst)); if !state.is_open { @@ -564,7 +564,7 @@ impl SenderInner { })); } - self.poll_unparked(Some(lw)).map(Ok) + self.poll_unparked(Some(waker)).map(Ok) } /// Returns whether this channel is closed without needing a context. @@ -582,7 +582,7 @@ impl SenderInner { self.inner.recv_task.wake(); } - fn poll_unparked(&mut self, lw: Option<&LocalWaker>) -> Poll<()> { + fn poll_unparked(&mut self, waker: Option<&Waker>) -> Poll<()> { // First check the `maybe_parked` variable. This avoids acquiring the // lock in most cases if self.maybe_parked { @@ -600,7 +600,7 @@ impl SenderInner { // // Update the task in case the `Sender` has been moved to another // task - task.task = lw.map(|lw| lw.clone().into_waker()); + task.task = waker.map(|waker| waker.clone()); Poll::Pending } else { @@ -649,12 +649,12 @@ impl Sender { /// - `Err(SendError)` if the receiver has been dropped. pub fn poll_ready( &mut self, - lw: &LocalWaker + waker: &Waker, ) -> Poll> { let inner = self.0.as_mut().ok_or(SendError { kind: SendErrorKind::Disconnected, })?; - inner.poll_ready(lw) + inner.poll_ready(waker) } /// Returns whether this channel is closed without needing a context. @@ -679,7 +679,7 @@ impl UnboundedSender { /// Check if the channel is ready to receive a message. pub fn poll_ready( &self, - _: &LocalWaker, + _: &Waker, ) -> Poll> { let inner = self.0.as_ref().ok_or(SendError { kind: SendErrorKind::Disconnected, @@ -904,7 +904,7 @@ impl Stream for Receiver { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { // Try to read a message off of the message queue. match self.next_message() { @@ -916,7 +916,7 @@ impl Stream for Receiver { }, Poll::Pending => { // There are no messages to read, in this case, park. - self.inner.as_ref().unwrap().recv_task.register(lw); + self.inner.as_ref().unwrap().recv_task.register(waker); // Check queue again after parking to prevent race condition: // a message could be added to the queue after previous `next_message` // before `register` call. @@ -971,9 +971,9 @@ impl Stream for UnboundedReceiver { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - Pin::new(&mut self.0).poll_next(lw) + Pin::new(&mut self.0).poll_next(waker) } } diff --git a/futures-channel/src/oneshot.rs b/futures-channel/src/oneshot.rs index 73c0cbed6c..ff7b9dacfc 100644 --- a/futures-channel/src/oneshot.rs +++ b/futures-channel/src/oneshot.rs @@ -1,7 +1,7 @@ //! A channel for sending a single message between asynchronous tasks. use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll, Waker}; +use futures_core::task::{Waker, Poll}; use std::pin::Pin; use std::sync::Arc; use std::sync::atomic::AtomicBool; @@ -154,7 +154,7 @@ impl Inner { } } - fn poll_cancel(&self, lw: &LocalWaker) -> Poll<()> { + fn poll_cancel(&self, waker: &Waker) -> Poll<()> { // Fast path up first, just read the flag and see if our other half is // gone. This flag is set both in our destructor and the oneshot // destructor, but our destructor hasn't run yet so if it's set then the @@ -176,7 +176,7 @@ impl Inner { // `Receiver` may have been dropped. The first thing it does is set the // flag, and if it fails to acquire the lock it assumes that we'll see // the flag later on. So... we then try to see the flag later on! - let handle = lw.clone().into_waker(); + let handle = waker.clone(); match self.tx_task.try_lock() { Some(mut p) => *p = Some(handle), None => return Poll::Ready(()), @@ -249,7 +249,7 @@ impl Inner { } } - fn recv(&self, lw: &LocalWaker) -> Poll> { + fn recv(&self, waker: &Waker) -> Poll> { // Check to see if some data has arrived. If it hasn't then we need to // block our task. // @@ -260,7 +260,7 @@ impl Inner { let done = if self.complete.load(SeqCst) { true } else { - let task = lw.clone().into_waker(); + let task = waker.clone(); match self.rx_task.try_lock() { Some(mut slot) => { *slot = Some(task); false }, None => true, @@ -348,8 +348,8 @@ impl Sender { /// alive and may be able to receive a message if sent. The current task, /// however, is scheduled to receive a notification if the corresponding /// `Receiver` goes away. - pub fn poll_cancel(&mut self, lw: &LocalWaker) -> Poll<()> { - self.inner.poll_cancel(lw) + pub fn poll_cancel(&mut self, waker: &Waker) -> Poll<()> { + self.inner.poll_cancel(waker) } /// Tests to see whether this `Sender`'s corresponding `Receiver` @@ -416,9 +416,9 @@ impl Future for Receiver { fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.inner.recv(lw) + self.inner.recv(waker) } } diff --git a/futures-channel/tests/mpsc.rs b/futures-channel/tests/mpsc.rs index f40de419eb..ad99f0b308 100644 --- a/futures-channel/tests/mpsc.rs +++ b/futures-channel/tests/mpsc.rs @@ -6,7 +6,7 @@ use futures::future::{FutureExt, poll_fn}; use futures::stream::{Stream, StreamExt}; use futures::sink::{Sink, SinkExt}; use futures::task::Poll; -use futures_test::task::noop_local_waker_ref; +use futures_test::task::noop_waker_ref; use pin_utils::pin_mut; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -304,7 +304,7 @@ fn stress_receiver_multi_task_bounded_hard() { } else { // Just poll let n = n.clone(); - match rx.poll_next_unpin(noop_local_waker_ref()) { + match rx.poll_next_unpin(noop_waker_ref()) { Poll::Ready(Some(_)) => { n.fetch_add(1, Ordering::Relaxed); } diff --git a/futures-channel/tests/oneshot.rs b/futures-channel/tests/oneshot.rs index a025b66542..c149c58c8d 100644 --- a/futures-channel/tests/oneshot.rs +++ b/futures-channel/tests/oneshot.rs @@ -3,7 +3,7 @@ use futures::channel::oneshot::{self, Sender}; use futures::executor::block_on; use futures::future::{Future, FutureExt, poll_fn}; -use futures::task::{LocalWaker, Poll}; +use futures::task::{Waker, Poll}; use std::pin::Pin; use std::sync::mpsc; use std::thread; @@ -12,12 +12,12 @@ use std::thread; fn smoke_poll() { let (mut tx, rx) = oneshot::channel::(); let mut rx = Some(rx); - let f = poll_fn(|lw| { - assert!(tx.poll_cancel(lw).is_pending()); - assert!(tx.poll_cancel(lw).is_pending()); + let f = poll_fn(|waker| { + assert!(tx.poll_cancel(waker).is_pending()); + assert!(tx.poll_cancel(waker).is_pending()); drop(rx.take()); - assert!(tx.poll_cancel(lw).is_ready()); - assert!(tx.poll_cancel(lw).is_ready()); + assert!(tx.poll_cancel(waker).is_ready()); + assert!(tx.poll_cancel(waker).is_ready()); Poll::Ready(()) }); @@ -42,8 +42,8 @@ struct WaitForCancel { impl Future for WaitForCancel { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.tx.poll_cancel(lw) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.tx.poll_cancel(waker) } } @@ -73,12 +73,12 @@ fn cancel_lots() { fn close() { let (mut tx, mut rx) = oneshot::channel::(); rx.close(); - block_on(poll_fn(|lw| { - match rx.poll_unpin(lw) { + block_on(poll_fn(|waker| { + match rx.poll_unpin(waker) { Poll::Ready(Err(_)) => {}, _ => panic!(), }; - assert!(tx.poll_cancel(lw).is_ready()); + assert!(tx.poll_cancel(waker).is_ready()); Poll::Ready(()) })); } diff --git a/futures-core/src/future/future_obj.rs b/futures-core/src/future/future_obj.rs index 3a2d2aa2c4..94c76981b5 100644 --- a/futures-core/src/future/future_obj.rs +++ b/futures-core/src/future/future_obj.rs @@ -3,7 +3,7 @@ use core::{ future::Future, marker::PhantomData, pin::Pin, - task::{LocalWaker, Poll}, + task::{Waker, Poll}, }; /// A custom trait object for polling futures, roughly akin to @@ -14,7 +14,7 @@ use core::{ /// `Box` is not available in no_std contexts. pub struct LocalFutureObj<'a, T> { ptr: *mut (), - poll_fn: unsafe fn(*mut (), &LocalWaker) -> Poll, + poll_fn: unsafe fn(*mut (), &Waker) -> Poll, drop_fn: unsafe fn(*mut ()), _marker: PhantomData<&'a ()>, } @@ -61,9 +61,9 @@ impl<'a, T> Future for LocalFutureObj<'a, T> { type Output = T; #[inline] - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { unsafe { - ((*self).poll_fn)((*self).ptr, lw) + ((*self).poll_fn)((*self).ptr, waker) } } } @@ -111,11 +111,11 @@ impl<'a, T> Future for FutureObj<'a, T> { type Output = T; #[inline] - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { let pinned_field: Pin<&mut LocalFutureObj<'a, T>> = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }; - LocalFutureObj::poll(pinned_field, lw) + LocalFutureObj::poll(pinned_field, waker) } } @@ -140,7 +140,7 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a { /// `poll` with the result of `into_raw` until `drop` is called; such calls /// are not, however, allowed to race with each other or with calls to /// `drop`. - unsafe fn poll(ptr: *mut (), lw: &LocalWaker) -> Poll; + unsafe fn poll(ptr: *mut (), waker: &Waker) -> Poll; /// Drops the future represented by the given void pointer. /// @@ -160,9 +160,9 @@ where self as *mut F as *mut () } - unsafe fn poll(ptr: *mut (), lw: &LocalWaker) -> Poll { + unsafe fn poll(ptr: *mut (), waker: &Waker) -> Poll { let p: Pin<&mut F> = Pin::new_unchecked(&mut *(ptr as *mut F)); - F::poll(p, lw) + F::poll(p, waker) } unsafe fn drop(_ptr: *mut ()) {} @@ -177,9 +177,9 @@ where mut_ref as *mut F as *mut () } - unsafe fn poll(ptr: *mut (), lw: &LocalWaker) -> Poll { + unsafe fn poll(ptr: *mut (), waker: &Waker) -> Poll { let future: Pin<&mut F> = Pin::new_unchecked(&mut *(ptr as *mut F)); - F::poll(future, lw) + F::poll(future, waker) } unsafe fn drop(_ptr: *mut ()) {} @@ -197,10 +197,10 @@ mod if_std { Box::into_raw(self) as *mut () } - unsafe fn poll(ptr: *mut (), lw: &LocalWaker) -> Poll { + unsafe fn poll(ptr: *mut (), waker: &Waker) -> Poll { let ptr = ptr as *mut F; let pin: Pin<&mut F> = Pin::new_unchecked(&mut *ptr); - F::poll(pin, lw) + F::poll(pin, waker) } unsafe fn drop(ptr: *mut ()) { @@ -219,10 +219,10 @@ mod if_std { ptr } - unsafe fn poll(ptr: *mut (), lw: &LocalWaker) -> Poll { + unsafe fn poll(ptr: *mut (), waker: &Waker) -> Poll { let ptr = ptr as *mut F; let pin: Pin<&mut F> = Pin::new_unchecked(&mut *ptr); - F::poll(pin, lw) + F::poll(pin, waker) } unsafe fn drop(ptr: *mut ()) { diff --git a/futures-core/src/future/mod.rs b/futures-core/src/future/mod.rs index 8675c57551..efdca7524d 100644 --- a/futures-core/src/future/mod.rs +++ b/futures-core/src/future/mod.rs @@ -1,6 +1,6 @@ //! Futures. -use crate::task::{LocalWaker, Poll}; +use crate::task::{Waker, Poll}; use core::pin::Pin; pub use core::future::Future; @@ -71,7 +71,7 @@ pub trait TryFuture { /// needed. fn try_poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll>; } @@ -82,7 +82,7 @@ impl TryFuture for F type Error = E; #[inline] - fn try_poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.poll(lw) + fn try_poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.poll(waker) } } diff --git a/futures-core/src/stream/mod.rs b/futures-core/src/stream/mod.rs index 193b99a690..72e813f8d8 100644 --- a/futures-core/src/stream/mod.rs +++ b/futures-core/src/stream/mod.rs @@ -1,6 +1,6 @@ //! Asynchronous streams. -use crate::task::{LocalWaker, Poll}; +use crate::task::{Waker, Poll}; use core::ops; use core::pin::Pin; @@ -54,7 +54,7 @@ pub trait Stream { /// calls. fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll>; } @@ -63,9 +63,9 @@ impl<'a, S: ?Sized + Stream + Unpin> Stream for &'a mut S { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - S::poll_next(Pin::new(&mut **self), lw) + S::poll_next(Pin::new(&mut **self), waker) } } @@ -78,9 +78,9 @@ where fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - Pin::get_mut(self).as_mut().poll_next(lw) + Pin::get_mut(self).as_mut().poll_next(waker) } } @@ -91,11 +91,11 @@ impl Stream for Either { type Item = A::Item; - fn poll_next(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_next(self: Pin<&mut Self>, waker: &Waker) -> Poll> { unsafe { match Pin::get_unchecked_mut(self) { - Either::Left(a) => Pin::new_unchecked(a).poll_next(lw), - Either::Right(b) => Pin::new_unchecked(b).poll_next(lw), + Either::Left(a) => Pin::new_unchecked(a).poll_next(waker), + Either::Right(b) => Pin::new_unchecked(b).poll_next(waker), } } } @@ -128,7 +128,7 @@ pub trait TryStream { /// This method is a stopgap for a compiler limitation that prevents us from /// directly inheriting from the `Stream` trait; in the future it won't be /// needed. - fn try_poll_next(self: Pin<&mut Self>, lw: &LocalWaker) + fn try_poll_next(self: Pin<&mut Self>, waker: &Waker) -> Poll>>; } @@ -138,10 +138,10 @@ impl TryStream for S type Ok = T; type Error = E; - fn try_poll_next(self: Pin<&mut Self>, lw: &LocalWaker) + fn try_poll_next(self: Pin<&mut Self>, waker: &Waker) -> Poll>> { - self.poll_next(lw) + self.poll_next(waker) } } @@ -155,9 +155,9 @@ mod if_std { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - Pin::new(&mut **self).poll_next(lw) + Pin::new(&mut **self).poll_next(waker) } } @@ -166,9 +166,9 @@ mod if_std { fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }.poll_next(lw) + unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }.poll_next(waker) } } @@ -177,7 +177,7 @@ mod if_std { fn poll_next( mut self: Pin<&mut Self>, - _lw: &LocalWaker, + _lw: &Waker, ) -> Poll> { Poll::Ready(self.pop_front()) } diff --git a/futures-core/src/stream/stream_obj.rs b/futures-core/src/stream/stream_obj.rs index e85a602fae..dae033e398 100644 --- a/futures-core/src/stream/stream_obj.rs +++ b/futures-core/src/stream/stream_obj.rs @@ -1,5 +1,5 @@ use super::Stream; -use crate::task::{LocalWaker, Poll}; +use crate::task::{Waker, Poll}; use core::fmt; use core::marker::PhantomData; use core::pin::Pin; @@ -12,7 +12,7 @@ use core::pin::Pin; /// `Box` is not available in no_std contexts. pub struct LocalStreamObj<'a, T> { ptr: *mut (), - poll_next_fn: unsafe fn(*mut (), &LocalWaker) -> Poll>, + poll_next_fn: unsafe fn(*mut (), &Waker) -> Poll>, drop_fn: unsafe fn(*mut ()), _marker: PhantomData<&'a ()>, } @@ -60,9 +60,9 @@ impl<'a, T> Stream for LocalStreamObj<'a, T> { #[inline] fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - unsafe { (self.poll_next_fn)(self.ptr, lw) } + unsafe { (self.poll_next_fn)(self.ptr, waker) } } } @@ -108,10 +108,10 @@ impl<'a, T> Stream for StreamObj<'a, T> { #[inline] fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let pinned_field = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }; - pinned_field.poll_next(lw) + pinned_field.poll_next(waker) } } @@ -138,7 +138,7 @@ pub unsafe trait UnsafeStreamObj<'a, T>: 'a { /// `drop`. unsafe fn poll_next( ptr: *mut (), - lw: &LocalWaker, + waker: &Waker, ) -> Poll>; /// Drops the stream represented by the given void pointer. @@ -161,9 +161,9 @@ where unsafe fn poll_next( ptr: *mut (), - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - Pin::new_unchecked(&mut *(ptr as *mut F)).poll_next(lw) + Pin::new_unchecked(&mut *(ptr as *mut F)).poll_next(waker) } unsafe fn drop(_ptr: *mut ()) {} @@ -179,9 +179,9 @@ where unsafe fn poll_next( ptr: *mut (), - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - Pin::new_unchecked(&mut *(ptr as *mut F)).poll_next(lw) + Pin::new_unchecked(&mut *(ptr as *mut F)).poll_next(waker) } unsafe fn drop(_ptr: *mut ()) {} @@ -200,10 +200,10 @@ mod if_std { Box::into_raw(self) as *mut () } - unsafe fn poll_next(ptr: *mut (), lw: &LocalWaker) -> Poll> { + unsafe fn poll_next(ptr: *mut (), waker: &Waker) -> Poll> { let ptr = ptr as *mut F; let pin: Pin<&mut F> = Pin::new_unchecked(&mut *ptr); - pin.poll_next(lw) + pin.poll_next(waker) } unsafe fn drop(ptr: *mut ()) { @@ -221,10 +221,10 @@ mod if_std { ptr } - unsafe fn poll_next(ptr: *mut (), lw: &LocalWaker) -> Poll> { + unsafe fn poll_next(ptr: *mut (), waker: &Waker) -> Poll> { let ptr = ptr as *mut F; let pin: Pin<&mut F> = Pin::new_unchecked(&mut *ptr); - pin.poll_next(lw) + pin.poll_next(waker) } unsafe fn drop(ptr: *mut ()) { diff --git a/futures-core/src/task/__internal/atomic_waker.rs b/futures-core/src/task/__internal/atomic_waker.rs index 7800dbf845..039f3e198c 100644 --- a/futures-core/src/task/__internal/atomic_waker.rs +++ b/futures-core/src/task/__internal/atomic_waker.rs @@ -2,7 +2,7 @@ use core::fmt; use core::cell::UnsafeCell; use core::sync::atomic::AtomicUsize; use core::sync::atomic::Ordering::{Acquire, Release, AcqRel}; -use crate::task::{LocalWaker, Waker}; +use crate::task::Waker; /// A synchronization primitive for task wakeup. /// @@ -171,7 +171,7 @@ impl AtomicWaker { /// ``` /// #![feature(futures_api)] /// use futures::future::Future; - /// use futures::task::{LocalWaker, Poll, AtomicWaker}; + /// use futures::task::{Waker, Poll, AtomicWaker}; /// use std::sync::atomic::AtomicBool; /// use std::sync::atomic::Ordering::SeqCst; /// use std::pin::Pin; @@ -184,10 +184,10 @@ impl AtomicWaker { /// impl Future for Flag { /// type Output = (); /// - /// fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> { + /// fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> { /// // Register **before** checking `set` to avoid a race condition /// // that would result in lost notifications. - /// self.waker.register(lw); + /// self.waker.register(waker); /// /// if self.set.load(SeqCst) { /// Poll::Ready(()) @@ -197,12 +197,12 @@ impl AtomicWaker { /// } /// } /// ``` - pub fn register(&self, lw: &LocalWaker) { + pub fn register(&self, waker: &Waker) { match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) { WAITING => { unsafe { // Locked acquired, update the waker cell - *self.waker.get() = Some(lw.clone().into_waker()); + *self.waker.get() = Some(waker.clone()); // Release the lock. If the state transitioned to include // the `WAKING` bit, this means that a wake has been @@ -241,7 +241,7 @@ impl AtomicWaker { // Currently in the process of waking the task, i.e., // `wake` is currently being called on the old task handle. // So, we call wake on the new waker - lw.wake(); + waker.wake(); } state => { // In this case, a concurrent thread is holding the diff --git a/futures-core/src/task/mod.rs b/futures-core/src/task/mod.rs index 95dc3cb57f..239921a9f4 100644 --- a/futures-core/src/task/mod.rs +++ b/futures-core/src/task/mod.rs @@ -5,6 +5,4 @@ mod spawn; pub mod __internal; pub use self::spawn::{Spawn, LocalSpawn, SpawnError}; -pub use core::task::{Poll, Waker, LocalWaker, UnsafeWake}; -#[cfg(feature = "std")] -pub use std::task::{Wake, local_waker, local_waker_from_nonlocal}; +pub use core::task::{Poll, Waker, RawWaker, RawWakerVTable}; diff --git a/futures-executor/benches/thread_notify.rs b/futures-executor/benches/thread_notify.rs index d025516666..e36e463d09 100644 --- a/futures-executor/benches/thread_notify.rs +++ b/futures-executor/benches/thread_notify.rs @@ -5,7 +5,7 @@ use crate::test::Bencher; use futures::executor::block_on; use futures::future::Future; -use futures::task::{Poll, LocalWaker, Waker}; +use futures::task::{Poll, Waker}; use std::pin::Pin; #[bench] @@ -19,12 +19,12 @@ fn thread_yield_single_thread_one_wait(b: &mut Bencher) { impl Future for Yield { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { if self.rem == 0 { Poll::Ready(()) } else { self.rem -= 1; - lw.wake(); + waker.wake(); Poll::Pending } } @@ -47,12 +47,12 @@ fn thread_yield_single_thread_many_wait(b: &mut Bencher) { impl Future for Yield { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { if self.rem == 0 { Poll::Ready(()) } else { self.rem -= 1; - lw.wake(); + waker.wake(); Poll::Pending } } @@ -84,12 +84,12 @@ fn thread_yield_multi_thread(b: &mut Bencher) { impl Future for Yield { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { if self.rem == 0 { Poll::Ready(()) } else { self.rem -= 1; - self.tx.send(lw.clone().into_waker()).unwrap(); + self.tx.send(waker.clone()).unwrap(); Poll::Pending } } diff --git a/futures-executor/src/local_pool.rs b/futures-executor/src/local_pool.rs index d99df54dd3..083d3ebab7 100644 --- a/futures-executor/src/local_pool.rs +++ b/futures-executor/src/local_pool.rs @@ -2,9 +2,10 @@ use crate::{enter, ThreadPool}; use futures_core::future::{Future, FutureObj, LocalFutureObj}; use futures_core::stream::{Stream}; use futures_core::task::{ - self, Poll, LocalWaker, Wake, + Poll, Waker, Spawn, LocalSpawn, SpawnError, }; +use futures_util::task::{WakerRef, waker_ref, ArcWake}; use futures_util::stream::FuturesUnordered; use futures_util::stream::StreamExt; use lazy_static::lazy_static; @@ -52,7 +53,7 @@ thread_local! { }); } -impl Wake for ThreadNotify { +impl ArcWake for ThreadNotify { fn wake(arc_self: &Arc) { arc_self.thread.unpark(); } @@ -60,16 +61,15 @@ impl Wake for ThreadNotify { // Set up and run a basic single-threaded spawner loop, invoking `f` on each // turn. -fn run_executor Poll>(mut f: F) -> T { +fn run_executor Poll>(mut f: F) -> T { let _enter = enter() .expect("cannot execute `LocalPool` executor from within \ another executor"); CURRENT_THREAD_NOTIFY.with(|thread_notify| { - let local_waker = - task::local_waker_from_nonlocal(thread_notify.clone()); + let waker: WakerRef<'_> = waker_ref(thread_notify); loop { - if let Poll::Ready(t) = f(&local_waker) { + if let Poll::Ready(t) = f(&waker) { return t; } thread::park(); @@ -113,7 +113,7 @@ impl LocalPool { /// The function will block the calling thread until *all* tasks in the pool /// are complete, including any spawned while running existing tasks. pub fn run(&mut self) { - run_executor(|local_waker| self.poll_pool(local_waker)) + run_executor(|waker| self.poll_pool(waker)) } /// Runs all the tasks in the pool until the given future completes. @@ -143,23 +143,23 @@ impl LocalPool { pub fn run_until(&mut self, future: F) -> F::Output { pin_mut!(future); - run_executor(|local_waker| { + run_executor(|waker| { { // if our main task is done, so are we - let result = future.as_mut().poll(local_waker); + let result = future.as_mut().poll(waker); if let Poll::Ready(output) = result { return Poll::Ready(output); } } - self.poll_pool(local_waker); + let _ = self.poll_pool(waker); Poll::Pending }) } // Make maximal progress on the entire pool of spawned task, returning `Ready` // if the pool is empty and `Pending` if no further progress can be made. - fn poll_pool(&mut self, local_waker: &LocalWaker) -> Poll<()> { + fn poll_pool(&mut self, waker: &Waker) -> Poll<()> { // state for the FuturesUnordered, which will never be used loop { // empty the incoming queue of newly-spawned tasks @@ -170,7 +170,7 @@ impl LocalPool { } } - let ret = self.pool.poll_next_unpin(local_waker); + let ret = self.pool.poll_next_unpin(waker); // we queued up some new tasks; add them and poll again if !self.incoming.borrow().is_empty() { continue; @@ -208,7 +208,7 @@ lazy_static! { /// spawned tasks. pub fn block_on(f: F) -> F::Output { pin_mut!(f); - run_executor(|local_waker| f.as_mut().poll(local_waker)) + run_executor(|waker| f.as_mut().poll(waker)) } /// Turn a stream into a blocking iterator. diff --git a/futures-executor/src/thread_pool.rs b/futures-executor/src/thread_pool.rs index 1820e20b11..0a9879d2aa 100644 --- a/futures-executor/src/thread_pool.rs +++ b/futures-executor/src/thread_pool.rs @@ -1,9 +1,9 @@ use crate::enter; use crate::unpark_mutex::UnparkMutex; use futures_core::future::{Future, FutureObj}; -use futures_core::task::{Poll, Wake, Spawn, SpawnError}; +use futures_core::task::{Poll, Spawn, SpawnError}; use futures_util::future::FutureExt; -use futures_util::task::local_waker_ref_from_nonlocal; +use futures_util::task::{ArcWake, waker_ref}; use num_cpus; use std::io; use std::prelude::v1::*; @@ -299,7 +299,7 @@ impl Task { /// thread. pub fn run(self) { let Task { mut future, wake_handle, mut exec } = self; - let local_waker = local_waker_ref_from_nonlocal(&wake_handle); + let waker = waker_ref(&wake_handle); // Safety: The ownership of this `Task` object is evidence that // we are in the `POLLING`/`REPOLL` state for the mutex. @@ -307,7 +307,7 @@ impl Task { wake_handle.mutex.start_poll(); loop { - let res = future.poll_unpin(&local_waker); + let res = future.poll_unpin(&waker); match res { Poll::Pending => {} Poll::Ready(()) => return wake_handle.mutex.complete(), @@ -337,7 +337,7 @@ impl fmt::Debug for Task { } } -impl Wake for WakeHandle { +impl ArcWake for WakeHandle { fn wake(arc_self: &Arc) { match arc_self.mutex.notify() { Ok(task) => arc_self.exec.state.send(Message::Run(task)), diff --git a/futures-executor/tests/local_pool.rs b/futures-executor/tests/local_pool.rs index 4cfa2cdd42..93bcc13d92 100644 --- a/futures-executor/tests/local_pool.rs +++ b/futures-executor/tests/local_pool.rs @@ -3,7 +3,7 @@ use futures::channel::oneshot; use futures::executor::LocalPool; use futures::future::{Future, lazy}; -use futures::task::{LocalWaker, Poll, Spawn, LocalSpawn}; +use futures::task::{Waker, Poll, Spawn, LocalSpawn}; use std::cell::{Cell, RefCell}; use std::pin::Pin; use std::rc::Rc; @@ -13,7 +13,7 @@ struct Pending(Rc<()>); impl Future for Pending { type Output = (); - fn poll(self: Pin<&mut Self>, _lw: &LocalWaker) -> Poll<()> { + fn poll(self: Pin<&mut Self>, _waker: &Waker) -> Poll<()> { Poll::Pending } } @@ -128,7 +128,7 @@ fn tasks_are_scheduled_fairly() { impl Future for Spin { type Output = (); - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> { + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<()> { let mut state = self.state.borrow_mut(); if self.idx == 0 { @@ -147,7 +147,7 @@ fn tasks_are_scheduled_fairly() { return Poll::Ready(()); } - lw.wake(); + waker.wake(); Poll::Pending } } diff --git a/futures-io/src/lib.rs b/futures-io/src/lib.rs index 3b4df46553..72146dd2fc 100644 --- a/futures-io/src/lib.rs +++ b/futures-io/src/lib.rs @@ -14,7 +14,7 @@ #[cfg(feature = "std")] mod if_std { - use futures_core::task::{LocalWaker, Poll}; + use futures_core::task::{Waker, Poll}; use std::boxed::Box; use std::cmp; use std::io as StdIo; @@ -99,7 +99,7 @@ mod if_std { /// /// If no data is available for reading, the method returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object becomes + /// `waker.wake()`) to receive a notification when the object becomes /// readable or is closed. /// /// # Implementation @@ -108,7 +108,7 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_read(&mut self, lw: &LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, waker: &Waker, buf: &mut [u8]) -> Poll>; /// Attempt to read from the `AsyncRead` into `vec` using vectored @@ -121,7 +121,7 @@ mod if_std { /// /// If no data is available for reading, the method returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object becomes + /// `waker.wake()`) to receive a notification when the object becomes /// readable or is closed. /// By default, this method delegates to using `poll_read` on the first /// buffer in `vec`. Objects which support vectored IO should override @@ -133,11 +133,11 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_vectored_read(&mut self, lw: &LocalWaker, vec: &mut [&mut IoVec]) + fn poll_vectored_read(&mut self, waker: &Waker, vec: &mut [&mut IoVec]) -> Poll> { if let Some(ref mut first_iovec) = vec.get_mut(0) { - self.poll_read(lw, first_iovec) + self.poll_read(waker, first_iovec) } else { // `vec` is empty. Poll::Ready(Ok(0)) @@ -159,7 +159,7 @@ mod if_std { /// /// If the object is not ready for writing, the method returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object becomes + /// `waker.wake()`) to receive a notification when the object becomes /// readable or is closed. /// /// # Implementation @@ -168,7 +168,7 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_write(&mut self, lw: &LocalWaker, buf: &[u8]) + fn poll_write(&mut self, waker: &Waker, buf: &[u8]) -> Poll>; /// Attempt to write bytes from `vec` into the object using vectored @@ -181,7 +181,7 @@ mod if_std { /// /// If the object is not ready for writing, the method returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object becomes + /// `waker.wake()`) to receive a notification when the object becomes /// readable or is closed. /// /// By default, this method delegates to using `poll_write` on the first @@ -194,11 +194,11 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_vectored_write(&mut self, lw: &LocalWaker, vec: &[&IoVec]) + fn poll_vectored_write(&mut self, waker: &Waker, vec: &[&IoVec]) -> Poll> { if let Some(ref first_iovec) = vec.get(0) { - self.poll_write(lw, &*first_iovec) + self.poll_write(waker, &*first_iovec) } else { // `vec` is empty. Poll::Ready(Ok(0)) @@ -212,7 +212,7 @@ mod if_std { /// /// If flushing cannot immediately complete, this method returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object can make + /// `waker.wake()`) to receive a notification when the object can make /// progress towards flushing. /// /// # Implementation @@ -221,7 +221,7 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_flush(&mut self, lw: &LocalWaker) -> Poll>; + fn poll_flush(&mut self, waker: &Waker) -> Poll>; /// Attempt to close the object. /// @@ -229,7 +229,7 @@ mod if_std { /// /// If closing cannot immediately complete, this function returns /// `Ok(Async::Pending)` and arranges for the current task (via - /// `lw.waker()`) to receive a notification when the object can make + /// `waker.wake()`) to receive a notification when the object can make /// progress towards closing. /// /// # Implementation @@ -238,7 +238,7 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Async::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_close(&mut self, lw: &LocalWaker) -> Poll>; + fn poll_close(&mut self, waker: &Waker) -> Poll>; } macro_rules! deref_async_read { @@ -247,16 +247,16 @@ mod if_std { (**self).initializer() } - fn poll_read(&mut self, lw: &LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, waker: &Waker, buf: &mut [u8]) -> Poll> { - (**self).poll_read(lw, buf) + (**self).poll_read(waker, buf) } - fn poll_vectored_read(&mut self, lw: &LocalWaker, vec: &mut [&mut IoVec]) + fn poll_vectored_read(&mut self, waker: &Waker, vec: &mut [&mut IoVec]) -> Poll> { - (**self).poll_vectored_read(lw, vec) + (**self).poll_vectored_read(waker, vec) } } } @@ -277,7 +277,7 @@ mod if_std { Initializer::nop() } - fn poll_read(&mut self, _: &LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, _: &Waker, buf: &mut [u8]) -> Poll> { Poll::Ready(StdIo::Read::read(self, buf)) @@ -299,24 +299,24 @@ mod if_std { macro_rules! deref_async_write { () => { - fn poll_write(&mut self, lw: &LocalWaker, buf: &[u8]) + fn poll_write(&mut self, waker: &Waker, buf: &[u8]) -> Poll> { - (**self).poll_write(lw, buf) + (**self).poll_write(waker, buf) } - fn poll_vectored_write(&mut self, lw: &LocalWaker, vec: &[&IoVec]) + fn poll_vectored_write(&mut self, waker: &Waker, vec: &[&IoVec]) -> Poll> { - (**self).poll_vectored_write(lw, vec) + (**self).poll_vectored_write(waker, vec) } - fn poll_flush(&mut self, lw: &LocalWaker) -> Poll> { - (**self).poll_flush(lw) + fn poll_flush(&mut self, waker: &Waker) -> Poll> { + (**self).poll_flush(waker) } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll> { - (**self).poll_close(lw) + fn poll_close(&mut self, waker: &Waker) -> Poll> { + (**self).poll_close(waker) } } } @@ -331,18 +331,18 @@ mod if_std { macro_rules! delegate_async_write_to_stdio { () => { - fn poll_write(&mut self, _: &LocalWaker, buf: &[u8]) + fn poll_write(&mut self, _: &Waker, buf: &[u8]) -> Poll> { Poll::Ready(StdIo::Write::write(self, buf)) } - fn poll_flush(&mut self, _: &LocalWaker) -> Poll> { + fn poll_flush(&mut self, _: &Waker) -> Poll> { Poll::Ready(StdIo::Write::flush(self)) } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll> { - self.poll_flush(lw) + fn poll_close(&mut self, waker: &Waker) -> Poll> { + self.poll_flush(waker) } } } @@ -350,7 +350,7 @@ mod if_std { impl> AsyncWrite for StdIo::Cursor { fn poll_write( &mut self, - _: &LocalWaker, + _: &Waker, buf: &[u8], ) -> Poll> { let position = self.position(); @@ -365,12 +365,12 @@ mod if_std { Poll::Ready(result) } - fn poll_flush(&mut self, _: &LocalWaker) -> Poll> { + fn poll_flush(&mut self, _: &Waker) -> Poll> { Poll::Ready(StdIo::Write::flush(&mut self.get_mut().as_mut())) } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll> { - self.poll_flush(lw) + fn poll_close(&mut self, waker: &Waker) -> Poll> { + self.poll_flush(waker) } } diff --git a/futures-select-macro/src/lib.rs b/futures-select-macro/src/lib.rs index 1567b79e3a..a21f8be25d 100644 --- a/futures-select-macro/src/lib.rs +++ b/futures-select-macro/src/lib.rs @@ -180,23 +180,23 @@ pub fn select(input: TokenStream) -> TokenStream { }) .collect(); - // For each future, make an `&mut dyn FnMut(&LocalWaker) -> Option>` + // For each future, make an `&mut dyn FnMut(&Waker) -> Option>` // to use for polling that individual future. These will then be put in an array. let poll_functions = bound_future_names.iter().zip(variant_names.iter()) .map(|(bound_future_name, variant_name)| { quote! { - let mut #variant_name = |__lw: &_| { + let mut #variant_name = |__waker: &_| { if #futures_crate::future::FusedFuture::is_terminated(&#bound_future_name) { None } else { Some(#futures_crate::future::FutureExt::poll_unpin( &mut #bound_future_name, - __lw, + __waker, ).map(#enum_ident::#variant_name)) } }; let #variant_name: &mut dyn FnMut( - &#futures_crate::task::LocalWaker + &#futures_crate::task::Waker ) -> Option<#futures_crate::task::Poll<_>> = &mut #variant_name; } }); @@ -235,7 +235,7 @@ pub fn select(input: TokenStream) -> TokenStream { let await_and_select = if let Some(default_expr) = parsed.default { quote! { if let #futures_crate::task::Poll::Ready(x) = - __poll_fn(#futures_crate::task::noop_local_waker_ref()) + __poll_fn(#futures_crate::task::noop_waker_ref()) { match x { #branches } } else { @@ -254,7 +254,7 @@ pub fn select(input: TokenStream) -> TokenStream { #enum_item #( #future_let_bindings )* - let mut __poll_fn = |__lw: &#futures_crate::task::LocalWaker| { + let mut __poll_fn = |__waker: &#futures_crate::task::Waker| { let mut __any_polled = false; #( #poll_functions )* @@ -266,9 +266,9 @@ pub fn select(input: TokenStream) -> TokenStream { ); for poller in &mut __select_arr { let poller: &mut &mut dyn FnMut( - &#futures_crate::task::LocalWaker + &#futures_crate::task::Waker ) -> Option<#futures_crate::task::Poll<_>> = poller; - match poller(__lw) { + match poller(__waker) { Some(x @ #futures_crate::task::Poll::Ready(_)) => return x, Some(#futures_crate::task::Poll::Pending) => { diff --git a/futures-sink/src/channel_impls.rs b/futures-sink/src/channel_impls.rs index 16a655296e..292b4ac71b 100644 --- a/futures-sink/src/channel_impls.rs +++ b/futures-sink/src/channel_impls.rs @@ -1,5 +1,5 @@ use crate::{Sink, Poll}; -use futures_core::task::LocalWaker; +use futures_core::task::Waker; use futures_channel::mpsc::{Sender, SendError, UnboundedSender}; use std::pin::Pin; @@ -7,19 +7,19 @@ impl Sink for Sender { type SinkItem = T; type SinkError = SendError; - fn poll_ready(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - (*self).poll_ready(lw) + fn poll_ready(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + (*self).poll_ready(waker) } fn start_send(mut self: Pin<&mut Self>, msg: T) -> Result<(), Self::SinkError> { (*self).start_send(msg) } - fn poll_flush(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } - fn poll_close(mut self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_close(mut self: Pin<&mut Self>, _: &Waker) -> Poll> { self.disconnect(); Poll::Ready(Ok(())) } @@ -29,19 +29,19 @@ impl Sink for UnboundedSender { type SinkItem = T; type SinkError = SendError; - fn poll_ready(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - UnboundedSender::poll_ready(&*self, lw) + fn poll_ready(self: Pin<&mut Self>, waker: &Waker) -> Poll> { + UnboundedSender::poll_ready(&*self, waker) } fn start_send(mut self: Pin<&mut Self>, msg: T) -> Result<(), Self::SinkError> { UnboundedSender::start_send(&mut *self, msg) } - fn poll_flush(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } - fn poll_close(mut self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_close(mut self: Pin<&mut Self>, _: &Waker) -> Poll> { self.disconnect(); Poll::Ready(Ok(())) } @@ -51,8 +51,8 @@ impl<'a, T> Sink for &'a UnboundedSender { type SinkItem = T; type SinkError = SendError; - fn poll_ready(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - UnboundedSender::poll_ready(*self, lw) + fn poll_ready(self: Pin<&mut Self>, waker: &Waker) -> Poll> { + UnboundedSender::poll_ready(*self, waker) } fn start_send(self: Pin<&mut Self>, msg: T) -> Result<(), Self::SinkError> { @@ -60,11 +60,11 @@ impl<'a, T> Sink for &'a UnboundedSender { .map_err(|err| err.into_send_error()) } - fn poll_flush(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } - fn poll_close(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_close(self: Pin<&mut Self>, _: &Waker) -> Poll> { self.close_channel(); Poll::Ready(Ok(())) } diff --git a/futures-sink/src/lib.rs b/futures-sink/src/lib.rs index 3a06ced0f4..7f72d75a8a 100644 --- a/futures-sink/src/lib.rs +++ b/futures-sink/src/lib.rs @@ -9,7 +9,7 @@ #![feature(futures_api)] -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use core::pin::Pin; /// A `Sink` is a value into which other values can be sent, asynchronously. @@ -53,12 +53,12 @@ pub trait Sink { /// /// This method returns `Poll::Ready` once the underlying sink is ready to /// receive data. If this method returns `Async::Pending`, the current task - /// is registered to be notified (via `lw.waker()`) when `poll_ready` + /// is registered to be notified (via `waker.wake()`) when `poll_ready` /// should be called again. /// /// In most cases, if the sink encounters an error, the sink will /// permanently be unable to receive items. - fn poll_ready(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll>; + fn poll_ready(self: Pin<&mut Self>, waker: &Waker) -> Poll>; /// Begin the process of sending a value to the sink. /// Each call to this function must be proceeded by a successful call to @@ -89,12 +89,12 @@ pub trait Sink { /// via `start_send` have been flushed. /// /// Returns `Ok(Async::Pending)` if there is more work left to do, in which - /// case the current task is scheduled (via `lw.waker()`) to wake up when + /// case the current task is scheduled (via `waker.wake()`) to wake up when /// `poll_flush` should be called again. /// /// In most cases, if the sink encounters an error, the sink will /// permanently be unable to receive items. - fn poll_flush(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll>; + fn poll_flush(self: Pin<&mut Self>, waker: &Waker) -> Poll>; /// Flush any remaining output and close this sink, if necessary. /// @@ -102,32 +102,32 @@ pub trait Sink { /// has been successfully closed. /// /// Returns `Ok(Async::Pending)` if there is more work left to do, in which - /// case the current task is scheduled (via `lw.waker()`) to wake up when + /// case the current task is scheduled (via `waker.wake()`) to wake up when /// `poll_close` should be called again. /// /// If this function encounters an error, the sink should be considered to /// have failed permanently, and no more `Sink` methods should be called. - fn poll_close(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll>; + fn poll_close(self: Pin<&mut Self>, waker: &Waker) -> Poll>; } impl<'a, S: ?Sized + Sink + Unpin> Sink for &'a mut S { type SinkItem = S::SinkItem; type SinkError = S::SinkError; - fn poll_ready(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_ready(lw) + fn poll_ready(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_ready(waker) } fn start_send(mut self: Pin<&mut Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> { Pin::new(&mut **self).start_send(item) } - fn poll_flush(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_flush(lw) + fn poll_flush(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_flush(waker) } - fn poll_close(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_close(lw) + fn poll_close(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_close(waker) } } @@ -135,20 +135,20 @@ impl<'a, S: ?Sized + Sink> Sink for Pin<&'a mut S> { type SinkItem = S::SinkItem; type SinkError = S::SinkError; - fn poll_ready(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - S::poll_ready((*self).as_mut(), lw) + fn poll_ready(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + S::poll_ready((*self).as_mut(), waker) } fn start_send(mut self: Pin<&mut Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> { S::start_send((*self).as_mut(), item) } - fn poll_flush(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - S::poll_flush((*self).as_mut(), lw) + fn poll_flush(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + S::poll_flush((*self).as_mut(), waker) } - fn poll_close(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - S::poll_close((*self).as_mut(), lw) + fn poll_close(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + S::poll_close((*self).as_mut(), waker) } } @@ -168,7 +168,7 @@ mod if_std { type SinkItem = T; type SinkError = VecSinkError; - fn poll_ready(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_ready(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } @@ -178,11 +178,11 @@ mod if_std { Ok(()) } - fn poll_flush(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } - fn poll_close(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_close(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } } @@ -191,7 +191,7 @@ mod if_std { type SinkItem = T; type SinkError = VecSinkError; - fn poll_ready(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_ready(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } @@ -201,11 +201,11 @@ mod if_std { Ok(()) } - fn poll_flush(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } - fn poll_close(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_close(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Ok(())) } } @@ -214,20 +214,20 @@ mod if_std { type SinkItem = S::SinkItem; type SinkError = S::SinkError; - fn poll_ready(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_ready(lw) + fn poll_ready(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_ready(waker) } fn start_send(mut self: Pin<&mut Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> { Pin::new(&mut **self).start_send(item) } - fn poll_flush(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_flush(lw) + fn poll_flush(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_flush(waker) } - fn poll_close(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - Pin::new(&mut **self).poll_close(lw) + fn poll_close(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + Pin::new(&mut **self).poll_close(waker) } } } @@ -246,11 +246,11 @@ impl Sink for Either type SinkItem = ::SinkItem; type SinkError = ::SinkError; - fn poll_ready(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_ready(self: Pin<&mut Self>, waker: &Waker) -> Poll> { unsafe { match Pin::get_unchecked_mut(self) { - Either::Left(x) => Pin::new_unchecked(x).poll_ready(lw), - Either::Right(x) => Pin::new_unchecked(x).poll_ready(lw), + Either::Left(x) => Pin::new_unchecked(x).poll_ready(waker), + Either::Right(x) => Pin::new_unchecked(x).poll_ready(waker), } } } @@ -264,20 +264,20 @@ impl Sink for Either } } - fn poll_flush(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, waker: &Waker) -> Poll> { unsafe { match Pin::get_unchecked_mut(self) { - Either::Left(x) => Pin::new_unchecked(x).poll_flush(lw), - Either::Right(x) => Pin::new_unchecked(x).poll_flush(lw), + Either::Left(x) => Pin::new_unchecked(x).poll_flush(waker), + Either::Right(x) => Pin::new_unchecked(x).poll_flush(waker), } } } - fn poll_close(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_close(self: Pin<&mut Self>, waker: &Waker) -> Poll> { unsafe { match Pin::get_unchecked_mut(self) { - Either::Left(x) => Pin::new_unchecked(x).poll_close(lw), - Either::Right(x) => Pin::new_unchecked(x).poll_close(lw), + Either::Left(x) => Pin::new_unchecked(x).poll_close(waker), + Either::Right(x) => Pin::new_unchecked(x).poll_close(waker), } } } diff --git a/futures-test/src/assert.rs b/futures-test/src/assert.rs index 1877c40a5f..b57610707a 100644 --- a/futures-test/src/assert.rs +++ b/futures-test/src/assert.rs @@ -30,7 +30,7 @@ macro_rules! assert_stream_pending { let mut stream = &mut $stream; $crate::assert::assert_is_unpin_stream(stream); let stream = $crate::std_reexport::pin::Pin::new(stream); - let lw = &$crate::task::noop_local_waker_ref(); + let lw = &$crate::task::noop_waker_ref(); let poll = $crate::futures_core_reexport::stream::Stream::poll_next( stream, lw, ); @@ -67,7 +67,7 @@ macro_rules! assert_stream_next { let mut stream = &mut $stream; $crate::assert::assert_is_unpin_stream(stream); let stream = $crate::std_reexport::pin::Pin::new(stream); - let lw = &$crate::task::noop_local_waker_ref(); + let lw = &$crate::task::noop_waker_ref(); match $crate::futures_core_reexport::stream::Stream::poll_next(stream, lw) { $crate::futures_core_reexport::task::Poll::Ready(Some(x)) => { assert_eq!(x, $item); @@ -110,7 +110,7 @@ macro_rules! assert_stream_done { let mut stream = &mut $stream; $crate::assert::assert_is_unpin_stream(stream); let stream = $crate::std_reexport::pin::Pin::new(stream); - let lw = &$crate::task::noop_local_waker_ref(); + let lw = &$crate::task::noop_waker_ref(); match $crate::futures_core_reexport::stream::Stream::poll_next(stream, lw) { $crate::futures_core_reexport::task::Poll::Ready(Some(_)) => { panic!("assertion failed: expected stream to be done but had more elements"); diff --git a/futures-test/src/future/assert_unmoved.rs b/futures-test/src/future/assert_unmoved.rs index da7b23489e..a4339efae5 100644 --- a/futures-test/src/future/assert_unmoved.rs +++ b/futures-test/src/future/assert_unmoved.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::marker::PhantomPinned; use std::pin::Pin; @@ -35,7 +35,7 @@ impl Future for AssertUnmoved { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { let cur_this = &*self as *const Self; if self.this_ptr.is_null() { @@ -44,7 +44,7 @@ impl Future for AssertUnmoved { } else { assert_eq!(self.this_ptr, cur_this, "Future moved between poll calls"); } - self.as_mut().future().poll(lw) + self.as_mut().future().poll(waker) } } @@ -64,7 +64,7 @@ mod tests { use futures_core::future::Future; use futures_core::task::Poll; use futures_util::future::empty; - use futures_util::task::noop_local_waker; + use futures_util::task::noop_waker; use std::pin::Pin; use super::AssertUnmoved; @@ -80,7 +80,7 @@ mod tests { #[should_panic(expected = "Future moved between poll calls")] fn dont_double_panic() { // This test should only panic, not abort the process. - let waker = noop_local_waker(); + let waker = noop_waker(); // First we allocate the future on the stack and poll it. let mut future = AssertUnmoved::new(empty::<()>()); diff --git a/futures-test/src/future/mod.rs b/futures-test/src/future/mod.rs index bb2f1e157b..713d745d39 100644 --- a/futures-test/src/future/mod.rs +++ b/futures-test/src/future/mod.rs @@ -43,7 +43,7 @@ pub trait FutureTestExt: Future { /// let future = (async { 5 }).pending_once(); /// pin_mut!(future); /// - /// let lw = &task::noop_local_waker_ref(); + /// let lw = &task::noop_waker_ref(); /// /// assert_eq!(future.poll_unpin(lw), Poll::Pending); /// assert_eq!(future.poll_unpin(lw), Poll::Ready(5)); diff --git a/futures-test/src/future/pending_once.rs b/futures-test/src/future/pending_once.rs index 7ba0221dce..238513fa43 100644 --- a/futures-test/src/future/pending_once.rs +++ b/futures-test/src/future/pending_once.rs @@ -1,5 +1,5 @@ use futures_core::future::{Future, FusedFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use std::pin::Pin; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -33,13 +33,13 @@ impl Future for PendingOnce { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { if self.polled_before { - self.as_mut().future().poll(lw) + self.as_mut().future().poll(waker) } else { *self.as_mut().polled_before() = true; - lw.wake(); + waker.wake(); Poll::Pending } } diff --git a/futures-test/src/task/mod.rs b/futures-test/src/task/mod.rs index 81b3737ed7..01c4e4f2f9 100644 --- a/futures-test/src/task/mod.rs +++ b/futures-test/src/task/mod.rs @@ -1,16 +1,16 @@ //! Task related testing utilities. //! //! This module provides utilities for creating test -//! [`LocalWaker`](futures_core::task::LocalWaker)s and +//! [`Waker`](futures_core::task::Waker)s and //! [`Spawn`](futures_core::task::Spawn) implementations. //! //! Test wakers: -//! - [`noop_local_waker`] creates a waker that ignores calls to -//! [`wake`](futures_core::task::LocalWaker). -//! - [`panic_local_waker`] creates a waker that panics when -//! [`wake`](futures_core::task::LocalWaker) is called. +//! - [`noop_waker`] creates a waker that ignores calls to +//! [`wake`](futures_core::task::Waker). +//! - [`panic_waker`] creates a waker that panics when +//! [`wake`](futures_core::task::Waker) is called. //! - [`new_count_waker`] creates a waker that increments a counter whenever -//! [`wake`](futures_core::task::LocalWaker) is called. +//! [`wake`](futures_core::task::Waker) is called. //! //! Test spawners: //! - [`NoopSpawner`] ignores calls to @@ -20,19 +20,19 @@ //! - [`RecordSpawner`] records the spawned futures. //! //! For convenience there additionally exist various functions that directly -//! return waker/spawner references: [`noop_local_waker_ref`], -//! [`panic_local_waker_ref`], [`noop_spawner_mut`] and [`panic_spawner_mut`]. +//! return waker/spawner references: [`noop_waker_ref`], +//! [`panic_waker_ref`], [`noop_spawner_mut`] and [`panic_spawner_mut`]. mod noop_spawner; pub use self::noop_spawner::{noop_spawner_mut, NoopSpawner}; -pub use futures_util::task::{noop_local_waker, noop_local_waker_ref}; +pub use futures_util::task::{noop_waker, noop_waker_ref}; mod panic_spawner; pub use self::panic_spawner::{panic_spawner_mut, PanicSpawner}; mod panic_waker; -pub use self::panic_waker::{panic_local_waker, panic_local_waker_ref, PanicWake}; +pub use self::panic_waker::{panic_waker, panic_waker_ref}; mod record_spawner; pub use self::record_spawner::RecordSpawner; diff --git a/futures-test/src/task/panic_waker.rs b/futures-test/src/task/panic_waker.rs index 6dcad3cbb1..13a113fc63 100644 --- a/futures-test/src/task/panic_waker.rs +++ b/futures-test/src/task/panic_waker.rs @@ -1,89 +1,63 @@ -use futures_core::task::{LocalWaker, UnsafeWake, Wake, Waker}; -use std::cell::UnsafeCell; -use std::ptr::NonNull; -use std::sync::Arc; +use futures_core::task::{Waker, RawWaker, RawWakerVTable}; +use core::cell::UnsafeCell; +use core::ptr::null; -/// An implementation of [`Wake`](futures_core::task::Wake) that panics when -/// woken. -/// -/// # Examples -/// -/// ```should_panic -/// #![feature(futures_api)] -/// use futures_test::task::panic_local_waker_ref; -/// -/// let lw = panic_local_waker_ref(); -/// lw.wake(); // Will panic -/// ``` -#[derive(Debug)] -pub struct PanicWake { - _reserved: (), -} - -impl PanicWake { - /// Create a new instance - pub fn new() -> Self { - Self { _reserved: () } - } -} - -impl Default for PanicWake { - fn default() -> Self { - Self::new() - } +unsafe fn clone_panic_waker(_data: *const()) -> RawWaker { + raw_panic_waker() } -impl Wake for PanicWake { - fn wake(_arc_self: &Arc) { - panic!("should not be woken") - } +unsafe fn noop(_data: *const()) { } -unsafe impl UnsafeWake for PanicWake { - unsafe fn clone_raw(&self) -> Waker { - panic_waker() - } - - unsafe fn drop_raw(&self) {} - - unsafe fn wake(&self) { - panic!("should not be woken") - } +unsafe fn wake_panic(_data: *const()) { + panic!("should not be woken"); } -fn panic_unsafe_wake() -> NonNull { - static mut INSTANCE: PanicWake = PanicWake { _reserved: () }; - unsafe { NonNull::new_unchecked(&mut INSTANCE as *mut dyn UnsafeWake) } -} +const PANIC_WAKER_VTABLE: RawWakerVTable = RawWakerVTable { + clone: clone_panic_waker, + drop: noop, + wake: wake_panic, +}; -fn panic_waker() -> Waker { - unsafe { Waker::new(panic_unsafe_wake()) } +fn raw_panic_waker() -> RawWaker { + RawWaker::new(null(), &PANIC_WAKER_VTABLE) } -/// Create a new [`LocalWaker`](futures_core::task::LocalWaker) referencing -/// a singleton instance of [`PanicWake`]. -pub fn panic_local_waker() -> LocalWaker { - unsafe { LocalWaker::new(panic_unsafe_wake()) } +/// Create a new [`Waker`](futures_core::task::Waker) which will +/// panic when `wake()` is called on it. The [`Waker`] can be converted +/// into a [`Waker`] which will behave the same way. +/// +/// # Examples +/// +/// ```should_panic +/// #![feature(futures_api)] +/// use futures_test::task::panic_waker; +/// +/// let lw = panic_waker(); +/// lw.wake(); // Will panic +/// ``` +pub fn panic_waker() -> Waker { + unsafe { Waker::new_unchecked(raw_panic_waker()) } } -/// Get a thread local reference to a -/// [`LocalWaker`](futures_core::task::LocalWaker) referencing a singleton -/// instance of [`PanicWake`]. +/// Get a global reference to a +/// [`Waker`](futures_core::task::Waker) referencing a singleton +/// instance of a [`Waker`] which panics when woken. /// /// # Examples /// /// ```should_panic /// #![feature(async_await, futures_api)] /// use futures::task; -/// use futures_test::task::panic_local_waker_ref; +/// use futures_test::task::panic_waker_ref; /// -/// let lw = panic_local_waker_ref(); +/// let lw = panic_waker_ref(); /// lw.wake(); // Will panic /// ``` -pub fn panic_local_waker_ref() -> &'static LocalWaker { +pub fn panic_waker_ref() -> &'static Waker { thread_local! { - static LOCAL_WAKER_INSTANCE: UnsafeCell = - UnsafeCell::new(panic_local_waker()); + static PANIC_WAKER_INSTANCE: UnsafeCell = + UnsafeCell::new(panic_waker()); } - LOCAL_WAKER_INSTANCE.with(|l| unsafe { &*l.get() }) + PANIC_WAKER_INSTANCE.with(|l| unsafe { &*l.get() }) } diff --git a/futures-test/src/task/wake_counter.rs b/futures-test/src/task/wake_counter.rs index b373b76653..3c43a0f293 100644 --- a/futures-test/src/task/wake_counter.rs +++ b/futures-test/src/task/wake_counter.rs @@ -1,6 +1,7 @@ -use futures_core::task::{local_waker_from_nonlocal, LocalWaker, Wake}; +use futures_core::task::{Waker}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; +use futures_util::task::ArcWake; /// Number of times the waker was awoken. /// @@ -28,15 +29,15 @@ struct WakerInner { count: AtomicUsize, } -impl Wake for WakerInner { +impl ArcWake for WakerInner { fn wake(arc_self: &Arc) { let _ = arc_self.count.fetch_add(1, Ordering::SeqCst); } } -/// Create a new [`LocalWaker`] that counts the number of times it's awoken. +/// Create a new [`Waker`] that counts the number of times it's awoken. /// -/// [`LocalWaker`]: futures_core::task::LocalWaker +/// [`Waker`]: futures_core::task::Waker /// /// # Examples /// @@ -53,7 +54,7 @@ impl Wake for WakerInner { /// /// assert_eq!(count, 2); /// ``` -pub fn new_count_waker() -> (LocalWaker, AwokenCount) { +pub fn new_count_waker() -> (Waker, AwokenCount) { let inner = Arc::new(WakerInner { count: AtomicUsize::new(0) }); - (local_waker_from_nonlocal(inner.clone()), AwokenCount { inner }) + (ArcWake::into_waker(inner.clone()), AwokenCount { inner }) } diff --git a/futures-util/benches_disabled/bilock.rs b/futures-util/benches_disabled/bilock.rs index b8fd377c12..a8418af204 100644 --- a/futures-util/benches_disabled/bilock.rs +++ b/futures-util/benches_disabled/bilock.rs @@ -2,11 +2,12 @@ #[cfg(feature = "bench")] mod bench { -use futures::task::{LocalWaker, Wake, Waker}; +use futures::task::Waker; use futures::executor::LocalPool; use futures_util::lock::BiLock; use futures_util::lock::BiLockAcquire; use futures_util::lock::BiLockAcquired; +use futures_util::task::ArcWake; use std::sync::Arc; use test::Bencher; @@ -14,11 +15,11 @@ use test::Bencher; fn notify_noop() -> Waker { struct Noop; - impl Wake for Noop { + impl ArcWake for Noop { fn wake(_: &Arc) {} } - Waker::from(Arc::new(Noop)) + ArcWake::into_waker(Arc::new(Noop)) } @@ -45,8 +46,8 @@ impl Stream for LockStream { type Item = BiLockAcquired; type Error = (); - fn poll_next(&mut self, lw: &LocalWaker) -> Poll, Self::Error> { - self.lock.poll(lw).map(|a| match a { + fn poll_next(&mut self, waker: &Waker) -> Poll, Self::Error> { + self.lock.poll(waker).map(|a| match a { Async::Ready(a) => Async::Ready(Some(a)), Async::Pending => Async::Pending, }) @@ -60,7 +61,7 @@ fn contended(b: &mut Bencher) { let mut exec = pool.executor(); let waker = notify_noop(); let mut map = task::LocalMap::new(); - let mut lw = task::Context::new(&mut map, &waker, &mut exec); + let mut waker = task::Context::new(&mut map, &waker, &mut exec); b.iter(|| { let (x, y) = BiLock::new(1); @@ -69,20 +70,20 @@ fn contended(b: &mut Bencher) { let mut y = LockStream::new(y); for _ in 0..1000 { - let x_guard = match x.poll_next(&mut lw) { + let x_guard = match x.poll_next(&mut waker) { Ok(Async::Ready(Some(guard))) => guard, _ => panic!(), }; // Try poll second lock while first lock still holds the lock - match y.poll_next(&mut lw) { + match y.poll_next(&mut waker) { Ok(Async::Pending) => (), _ => panic!(), }; x.release_lock(x_guard); - let y_guard = match y.poll_next(&mut lw) { + let y_guard = match y.poll_next(&mut waker) { Ok(Async::Ready(Some(guard))) => guard, _ => panic!(), }; @@ -99,7 +100,7 @@ fn lock_unlock(b: &mut Bencher) { let mut exec = pool.executor(); let waker = notify_noop(); let mut map = task::LocalMap::new(); - let mut lw = task::Context::new(&mut map, &waker, &mut exec); + let mut waker = task::Context::new(&mut map, &waker, &mut exec); b.iter(|| { let (x, y) = BiLock::new(1); @@ -108,14 +109,14 @@ fn lock_unlock(b: &mut Bencher) { let mut y = LockStream::new(y); for _ in 0..1000 { - let x_guard = match x.poll_next(&mut lw) { + let x_guard = match x.poll_next(&mut waker) { Ok(Async::Ready(Some(guard))) => guard, _ => panic!(), }; x.release_lock(x_guard); - let y_guard = match y.poll_next(&mut lw) { + let y_guard = match y.poll_next(&mut waker) { Ok(Async::Ready(Some(guard))) => guard, _ => panic!(), }; diff --git a/futures-util/src/async_await/pending.rs b/futures-util/src/async_await/pending.rs index e84489ae9f..10cf2572ee 100644 --- a/futures-util/src/async_await/pending.rs +++ b/futures-util/src/async_await/pending.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A macro which yields to the event loop once. /// @@ -30,7 +30,7 @@ pub struct PendingOnce { impl Future for PendingOnce { type Output = (); - fn poll(mut self: Pin<&mut Self>, _: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, _: &Waker) -> Poll { if self.is_ready { Poll::Ready(()) } else { diff --git a/futures-util/src/async_await/poll.rs b/futures-util/src/async_await/poll.rs index e22225b82c..3479f988a6 100644 --- a/futures-util/src/async_await/poll.rs +++ b/futures-util/src/async_await/poll.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A macro which returns the result of polling a future once within the /// current `async` context. @@ -26,7 +26,7 @@ pub struct PollOnce { impl Future for PollOnce { type Output = Poll; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - Poll::Ready(Pin::new(&mut self.future).poll(lw)) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + Poll::Ready(Pin::new(&mut self.future).poll(waker)) } } diff --git a/futures-util/src/compat/compat01as03.rs b/futures-util/src/compat/compat01as03.rs index 71ab806b60..5928777644 100644 --- a/futures-util/src/compat/compat01as03.rs +++ b/futures-util/src/compat/compat01as03.rs @@ -7,7 +7,7 @@ use futures_01::executor::{ use futures_01::{Async as Async01, Future as Future01, Stream as Stream01}; use futures_core::{task as task03, Future as Future03, Stream as Stream03}; use std::pin::Pin; -use std::task::LocalWaker; +use std::task::Waker; /// Converts a futures 0.1 Future, Stream, AsyncRead, or AsyncWrite /// object to a futures 0.3-compatible version, @@ -28,8 +28,8 @@ impl Compat01As03 { } } - fn in_notify(&mut self, lw: &LocalWaker, f: impl FnOnce(&mut T) -> R) -> R { - let notify = &WakerToHandle(lw.as_waker()); + fn in_notify(&mut self, waker: &Waker, f: impl FnOnce(&mut T) -> R) -> R { + let notify = &WakerToHandle(waker); self.inner.poll_fn_notify(notify, 0, f) } } @@ -73,9 +73,9 @@ impl Future03 for Compat01As03 { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> task03::Poll { - poll_01_to_03(self.in_notify(lw, |f| f.poll())) + poll_01_to_03(self.in_notify(waker, |f| f.poll())) } } @@ -84,9 +84,9 @@ impl Stream03 for Compat01As03 { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> task03::Poll> { - match self.in_notify(lw, |f| f.poll()) { + match self.in_notify(waker, |f| f.poll()) { Ok(Async01::Ready(Some(t))) => task03::Poll::Ready(Some(Ok(t))), Ok(Async01::Ready(None)) => task03::Poll::Ready(None), Ok(Async01::NotReady) => task03::Poll::Pending, @@ -146,30 +146,30 @@ mod io { } } - fn poll_read(&mut self, lw: &task03::LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, waker: &task03::Waker, buf: &mut [u8]) -> task03::Poll> { - poll_01_to_03(self.in_notify(lw, |x| x.poll_read(buf))) + poll_01_to_03(self.in_notify(waker, |x| x.poll_read(buf))) } } impl AsyncWrite03 for Compat01As03 { - fn poll_write(&mut self, lw: &task03::LocalWaker, buf: &[u8]) + fn poll_write(&mut self, waker: &task03::Waker, buf: &[u8]) -> task03::Poll> { - poll_01_to_03(self.in_notify(lw, |x| x.poll_write(buf))) + poll_01_to_03(self.in_notify(waker, |x| x.poll_write(buf))) } - fn poll_flush(&mut self, lw: &task03::LocalWaker) + fn poll_flush(&mut self, waker: &task03::Waker) -> task03::Poll> { - poll_01_to_03(self.in_notify(lw, |x| x.poll_flush())) + poll_01_to_03(self.in_notify(waker, |x| x.poll_flush())) } - fn poll_close(&mut self, lw: &task03::LocalWaker) + fn poll_close(&mut self, waker: &task03::Waker) -> task03::Poll> { - poll_01_to_03(self.in_notify(lw, |x| x.shutdown())) + poll_01_to_03(self.in_notify(waker, |x| x.shutdown())) } } } diff --git a/futures-util/src/compat/compat03as01.rs b/futures-util/src/compat/compat03as01.rs index 1519b2940a..a04bebaa9c 100644 --- a/futures-util/src/compat/compat03as01.rs +++ b/futures-util/src/compat/compat03as01.rs @@ -7,7 +7,8 @@ use futures_core::{ task as task03, TryFuture as TryFuture03, TryStream as TryStream03, }; use futures_sink::Sink as Sink03; -use std::{marker::PhantomData, ops::Deref, pin::Pin, ptr::NonNull, sync::Arc}; +use crate::task::ArcWake as ArcWake03; +use std::{pin::Pin, sync::Arc}; /// Converts a futures 0.3 [`TryFuture`](futures_core::future::TryFuture), /// [`TryStream`](futures_core::stream::TryStream) or @@ -55,7 +56,7 @@ where type Error = Fut::Error; fn poll(&mut self) -> Poll01 { - with_context(self, |inner, lw| poll_03_to_01(inner.try_poll(lw))) + with_context(self, |inner, waker| poll_03_to_01(inner.try_poll(waker))) } } @@ -67,7 +68,7 @@ where type Error = St::Error; fn poll(&mut self) -> Poll01, Self::Error> { - with_context(self, |inner, lw| match inner.try_poll_next(lw) { + with_context(self, |inner, waker| match inner.try_poll_next(waker) { task03::Poll::Ready(None) => Ok(Async01::Ready(None)), task03::Poll::Ready(Some(Ok(t))) => Ok(Async01::Ready(Some(t))), task03::Poll::Pending => Ok(Async01::NotReady), @@ -87,8 +88,8 @@ where &mut self, item: Self::SinkItem, ) -> StartSend01 { - with_context(self, |mut inner, lw| { - match inner.as_mut().poll_ready(lw) { + with_context(self, |mut inner, waker| { + match inner.as_mut().poll_ready(waker) { task03::Poll::Ready(Ok(())) => { inner.start_send(item).map(|()| AsyncSink01::Ready) } @@ -99,25 +100,11 @@ where } fn poll_complete(&mut self) -> Poll01<(), Self::SinkError> { - with_context(self, |inner, lw| poll_03_to_01(inner.poll_flush(lw))) + with_context(self, |inner, waker| poll_03_to_01(inner.poll_flush(waker))) } fn close(&mut self) -> Poll01<(), Self::SinkError> { - with_context(self, |inner, lw| poll_03_to_01(inner.poll_close(lw))) - } -} - -// `task::LocalWaker` with a lifetime tied to it. -struct LocalWakerLt<'a> { - inner: task03::LocalWaker, - _marker: PhantomData<&'a ()>, -} - -impl<'a> Deref for LocalWakerLt<'a> { - type Target = task03::LocalWaker; - - fn deref(&self) -> &Self::Target { - &self.inner + with_context(self, |inner, waker| poll_03_to_01(inner.poll_close(waker))) } } @@ -128,32 +115,16 @@ impl Current { Current(task01::current()) } - fn as_waker(&self) -> LocalWakerLt<'_> { - unsafe { - LocalWakerLt { - inner: task03::LocalWaker::new(NonNull::new_unchecked(self as *const Current as *mut Current)), - _marker: PhantomData, - } - } - } -} - -unsafe impl task03::UnsafeWake for Current { - #[inline] - unsafe fn clone_raw(&self) -> task03::Waker { - task03::Waker::from(Arc::new(Current(self.0.clone()))) - } - - #[inline] - unsafe fn drop_raw(&self) {} // Does nothing - - #[inline] - unsafe fn wake(&self) { - self.0.notify(); + fn as_waker(&self) -> task03::Waker { + // For simplicity reasons wrap the Waker into an Arc. + // We can optimize this again later on and reintroduce WakerLt<'a> which + // derefs to Waker, and where cloning it through RawWakerVTable returns + // an Arc version + ArcWake03::into_waker(Arc::new(Current(self.0.clone()))) } } -impl task03::Wake for Current { +impl ArcWake03 for Current { fn wake(arc_self: &Arc) { arc_self.0.notify(); } @@ -162,11 +133,11 @@ impl task03::Wake for Current { fn with_context(compat: &mut Compat, f: F) -> R where T: Unpin, - F: FnOnce(Pin<&mut T>, &task03::LocalWaker) -> R, + F: FnOnce(Pin<&mut T>, &task03::Waker) -> R, { let current = Current::new(); - let lw = current.as_waker(); - f(Pin::new(&mut compat.inner), &lw) + let waker = current.as_waker(); + f(Pin::new(&mut compat.inner), &waker) } #[cfg(feature = "io-compat")] @@ -188,8 +159,8 @@ mod io { impl std::io::Read for Compat { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { let current = Current::new(); - let lw = current.as_waker(); - poll_03_to_io(self.inner.poll_read(&lw, buf)) + let waker = current.as_waker(); + poll_03_to_io(self.inner.poll_read(&waker, buf)) } } @@ -207,22 +178,22 @@ mod io { impl std::io::Write for Compat { fn write(&mut self, buf: &[u8]) -> std::io::Result { let current = Current::new(); - let lw = current.as_waker(); - poll_03_to_io(self.inner.poll_write(&lw, buf)) + let waker = current.as_waker(); + poll_03_to_io(self.inner.poll_write(&waker, buf)) } fn flush(&mut self) -> std::io::Result<()> { let current = Current::new(); - let lw = current.as_waker(); - poll_03_to_io(self.inner.poll_flush(&lw)) + let waker = current.as_waker(); + poll_03_to_io(self.inner.poll_flush(&waker)) } } impl AsyncWrite01 for Compat { fn shutdown(&mut self) -> std::io::Result> { let current = Current::new(); - let lw = current.as_waker(); - poll_03_to_01(self.inner.poll_close(&lw)) + let waker = current.as_waker(); + poll_03_to_01(self.inner.poll_close(&waker)) } } } diff --git a/futures-util/src/future/abortable.rs b/futures-util/src/future/abortable.rs index 91696ed666..cfb51d55be 100644 --- a/futures-util/src/future/abortable.rs +++ b/futures-util/src/future/abortable.rs @@ -1,6 +1,6 @@ use crate::task::AtomicWaker; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; use std::pin::Pin; use std::sync::Arc; @@ -121,19 +121,19 @@ pub struct Aborted; impl Future for Abortable where Fut: Future { type Output = Result; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { // Check if the future has been aborted if self.inner.cancel.load(Ordering::Relaxed) { return Poll::Ready(Err(Aborted)) } // attempt to complete the future - if let Poll::Ready(x) = self.as_mut().future().poll(lw) { + if let Poll::Ready(x) = self.as_mut().future().poll(waker) { return Poll::Ready(Ok(x)) } // Register to receive a wakeup if the future is aborted in the... future - self.inner.waker.register(lw); + self.inner.waker.register(waker); // Check to see if the future was aborted between the first check and // registration. diff --git a/futures-util/src/future/catch_unwind.rs b/futures-util/src/future/catch_unwind.rs index d6eb124b72..3b8e8fd97e 100644 --- a/futures-util/src/future/catch_unwind.rs +++ b/futures-util/src/future/catch_unwind.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; use std::any::Any; use std::pin::Pin; @@ -28,8 +28,8 @@ impl Future for CatchUnwind { type Output = Result>; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - match catch_unwind(AssertUnwindSafe(|| self.future().poll(lw))) { + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + match catch_unwind(AssertUnwindSafe(|| self.future().poll(waker))) { Ok(res) => res.map(Ok), Err(e) => Poll::Ready(Err(e)) } diff --git a/futures-util/src/future/chain.rs b/futures-util/src/future/chain.rs index b40923ebb2..efc79242b1 100644 --- a/futures-util/src/future/chain.rs +++ b/futures-util/src/future/chain.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; #[must_use = "futures do nothing unless polled"] #[derive(Debug)] @@ -26,7 +26,7 @@ impl Chain pub(crate) fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, f: F, ) -> Poll where F: FnOnce(Fut1::Output, Data) -> Fut2, @@ -39,13 +39,13 @@ impl Chain loop { let (output, data) = match this { Chain::First(fut1, data) => { - match unsafe { Pin::new_unchecked(fut1) }.poll(lw) { + match unsafe { Pin::new_unchecked(fut1) }.poll(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(output) => (output, data.take().unwrap()), } } Chain::Second(fut2) => { - return unsafe { Pin::new_unchecked(fut2) }.poll(lw); + return unsafe { Pin::new_unchecked(fut2) }.poll(waker); } Chain::Empty => unreachable!() }; diff --git a/futures-util/src/future/disabled/select.rs b/futures-util/src/future/disabled/select.rs index be267d6677..c5e30259df 100644 --- a/futures-util/src/future/disabled/select.rs +++ b/futures-util/src/future/disabled/select.rs @@ -23,12 +23,12 @@ impl Future for Select where A: Future, B: Future { type Item = Either<(A::Item, B), (B::Item, A)>; type Error = Either<(A::Error, B), (B::Error, A)>; - fn poll(&mut self, lw: &LocalWaker) -> Poll { + fn poll(&mut self, waker: &Waker) -> Poll { let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice"); - match a.poll(lw) { + match a.poll(waker) { Err(e) => Err(Either::Left((e, b))), Ok(Async::Ready(x)) => Ok(Async::Ready(Either::Left((x, b)))), - Ok(Async::Pending) => match b.poll(lw) { + Ok(Async::Pending) => match b.poll(waker) { Err(e) => Err(Either::Right((e, a))), Ok(Async::Ready(x)) => Ok(Async::Ready(Either::Right((x, a)))), Ok(Async::Pending) => { diff --git a/futures-util/src/future/disabled/select_all.rs b/futures-util/src/future/disabled/select_all.rs index 713ff28e08..1f1086a7e2 100644 --- a/futures-util/src/future/disabled/select_all.rs +++ b/futures-util/src/future/disabled/select_all.rs @@ -49,9 +49,9 @@ impl Future for SelectAll type Item = (A::Item, usize, Vec); type Error = (A::Error, usize, Vec); - fn poll(&mut self, lw: &LocalWaker) -> Poll { + fn poll(&mut self, waker: &Waker) -> Poll { let item = self.inner.iter_mut().enumerate().filter_map(|(i, f)| { - match f.poll(lw) { + match f.poll(waker) { Ok(Async::Pending) => None, Ok(Async::Ready(e)) => Some((i, Ok(e))), Err(e) => Some((i, Err(e))), diff --git a/futures-util/src/future/disabled/select_ok.rs b/futures-util/src/future/disabled/select_ok.rs index faaa92d8e7..6c5df0ae62 100644 --- a/futures-util/src/future/disabled/select_ok.rs +++ b/futures-util/src/future/disabled/select_ok.rs @@ -45,11 +45,11 @@ impl Future for SelectOk where A: Future { type Item = (A::Item, Vec); type Error = A::Error; - fn poll(&mut self, lw: &LocalWaker) -> Poll { + fn poll(&mut self, waker: &Waker) -> Poll { // loop until we've either exhausted all errors, a success was hit, or nothing is ready loop { let item = self.inner.iter_mut().enumerate().filter_map(|(i, f)| { - match f.poll(lw) { + match f.poll(waker) { Ok(Async::Pending) => None, Ok(Async::Ready(e)) => Some((i, Ok(e))), Err(e) => Some((i, Err(e))), diff --git a/futures-util/src/future/empty.rs b/futures-util/src/future/empty.rs index 8a561975b6..b9cb471fef 100644 --- a/futures-util/src/future/empty.rs +++ b/futures-util/src/future/empty.rs @@ -1,7 +1,7 @@ use core::marker; use core::pin::Pin; use futures_core::future::{Future, FusedFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future which is never resolved. /// @@ -40,7 +40,7 @@ pub fn empty() -> Empty { impl Future for Empty { type Output = T; - fn poll(self: Pin<&mut Self>, _: &LocalWaker) -> Poll { + fn poll(self: Pin<&mut Self>, _: &Waker) -> Poll { Poll::Pending } } diff --git a/futures-util/src/future/flatten.rs b/futures-util/src/future/flatten.rs index 0529ec0229..818298fbae 100644 --- a/futures-util/src/future/flatten.rs +++ b/futures-util/src/future/flatten.rs @@ -2,7 +2,7 @@ use super::chain::Chain; use core::fmt; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the `flatten` combinator. @@ -55,7 +55,7 @@ impl Future for Flatten { type Output = ::Output; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.state().poll(lw, |a, ()| a) + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.state().poll(waker, |a, ()| a) } } diff --git a/futures-util/src/future/flatten_stream.rs b/futures-util/src/future/flatten_stream.rs index 8ab3d77ae3..d90050b380 100644 --- a/futures-util/src/future/flatten_stream.rs +++ b/futures-util/src/future/flatten_stream.rs @@ -2,7 +2,7 @@ use core::fmt; use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// Future for the `flatten_stream` combinator, flattening a /// future-of-a-stream to get just the result of the final stream as a stream. @@ -58,14 +58,14 @@ impl Stream for FlattenStream { type Item = ::Item; - fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_next(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { loop { // safety: data is never moved via the resulting &mut reference let stream = match &mut unsafe { Pin::get_unchecked_mut(self.as_mut()) }.state { State::Future(f) => { // safety: the future we're re-pinning here will never be moved; // it will just be polled, then dropped in place - match unsafe { Pin::new_unchecked(f) }.poll(lw) { + match unsafe { Pin::new_unchecked(f) }.poll(waker) { Poll::Pending => { // State is not changed, early return. return Poll::Pending @@ -81,7 +81,7 @@ impl Stream for FlattenStream State::Stream(s) => { // safety: the stream we're repinning here will never be moved; // it will just be polled, then dropped in place - return unsafe { Pin::new_unchecked(s) }.poll_next(lw); + return unsafe { Pin::new_unchecked(s) }.poll_next(waker); } }; diff --git a/futures-util/src/future/fuse.rs b/futures-util/src/future/fuse.rs index c7e8434cf7..172081adf1 100644 --- a/futures-util/src/future/fuse.rs +++ b/futures-util/src/future/fuse.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{Future, FusedFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// A future which "fuses" a future once it has been resolved. @@ -37,12 +37,12 @@ impl FusedFuture for Fuse { impl Future for Fuse { type Output = Fut::Output; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { // safety: we use this &mut only for matching, not for movement let v = match self.as_mut().future().as_pin_mut() { Some(fut) => { // safety: this re-pinned future will never move before being dropped - match fut.poll(lw) { + match fut.poll(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(v) => v } diff --git a/futures-util/src/future/inspect.rs b/futures-util/src/future/inspect.rs index 8661c93a86..85bd47cb5f 100644 --- a/futures-util/src/future/inspect.rs +++ b/futures-util/src/future/inspect.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Do something with the item of a future, passing it on. @@ -37,8 +37,8 @@ impl Future for Inspect { type Output = Fut::Output; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - let e = match self.as_mut().future().poll(lw) { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + let e = match self.as_mut().future().poll(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(e) => e, }; diff --git a/futures-util/src/future/into_stream.rs b/futures-util/src/future/into_stream.rs index 97a4ba9f08..28e77e07a9 100644 --- a/futures-util/src/future/into_stream.rs +++ b/futures-util/src/future/into_stream.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// A type which converts a `Future` into a `Stream` @@ -25,10 +25,10 @@ impl IntoStream { impl Stream for IntoStream { type Item = Fut::Output; - fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_next(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { let v = match self.as_mut().future().as_pin_mut() { Some(fut) => { - match fut.poll(lw) { + match fut.poll(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(v) => v } diff --git a/futures-util/src/future/join.rs b/futures-util/src/future/join.rs index dad4c85c9a..b0b1cba8c7 100644 --- a/futures-util/src/future/join.rs +++ b/futures-util/src/future/join.rs @@ -4,7 +4,7 @@ use crate::future::{MaybeDone, maybe_done}; use core::fmt; use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; macro_rules! generate { @@ -48,11 +48,11 @@ macro_rules! generate { #[allow(clippy::useless_let_if_seq)] fn poll( - mut self: Pin<&mut Self>, lw: &LocalWaker + mut self: Pin<&mut Self>, waker: &Waker ) -> Poll { let mut all_done = true; $( - if self.as_mut().$Fut().poll(lw).is_pending() { + if self.as_mut().$Fut().poll(waker).is_pending() { all_done = false; } )* diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 5512eed6e2..86c9e91d1c 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -129,13 +129,13 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &::std::task::LocalWaker, + waker: &::std::task::Waker, ) -> Poll { let mut all_done = true; for mut elem in iter_pin_mut(self.elems.as_mut()) { if let Some(pending) = elem.as_mut().pending_pin_mut() { - if let Poll::Ready(output) = pending.poll(lw) { + if let Poll::Ready(output) = pending.poll(waker) { elem.set(ElemState::Done(Some(output))); } else { all_done = false; diff --git a/futures-util/src/future/lazy.rs b/futures-util/src/future/lazy.rs index f7bc3430aa..1b5ff9570b 100644 --- a/futures-util/src/future/lazy.rs +++ b/futures-util/src/future/lazy.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future which, when polled, invokes a closure and yields its result. /// @@ -35,7 +35,7 @@ impl Unpin for Lazy {} /// # }); /// ``` pub fn lazy(f: F) -> Lazy - where F: FnOnce(&LocalWaker) -> R, + where F: FnOnce(&Waker) -> R, { Lazy { f: Some(f) } } @@ -45,11 +45,11 @@ impl FusedFuture for Lazy { } impl Future for Lazy - where F: FnOnce(&LocalWaker) -> R, + where F: FnOnce(&Waker) -> R, { type Output = R; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - Poll::Ready((self.f.take().unwrap())(lw)) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + Poll::Ready((self.f.take().unwrap())(waker)) } } diff --git a/futures-util/src/future/map.rs b/futures-util/src/future/map.rs index ab1bb22dfd..aa487ecece 100644 --- a/futures-util/src/future/map.rs +++ b/futures-util/src/future/map.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Future for the `map` combinator, changing the type of a future. @@ -35,8 +35,8 @@ impl Future for Map { type Output = T; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - match self.as_mut().future().poll(lw) { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + match self.as_mut().future().poll(waker) { Poll::Pending => Poll::Pending, Poll::Ready(output) => { let f = self.f().take() diff --git a/futures-util/src/future/maybe_done.rs b/futures-util/src/future/maybe_done.rs index 916a302e6c..dfcd38581e 100644 --- a/futures-util/src/future/maybe_done.rs +++ b/futures-util/src/future/maybe_done.rs @@ -3,7 +3,7 @@ use core::mem; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future that may have completed. /// @@ -92,11 +92,11 @@ impl FusedFuture for MaybeDone { impl Future for MaybeDone { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let res = unsafe { match Pin::get_unchecked_mut(self.as_mut()) { MaybeDone::Future(a) => { - if let Poll::Ready(res) = Pin::new_unchecked(a).poll(lw) { + if let Poll::Ready(res) = Pin::new_unchecked(a).poll(waker) { res } else { return Poll::Pending diff --git a/futures-util/src/future/mod.rs b/futures-util/src/future/mod.rs index d21e44eed4..419cca8426 100644 --- a/futures-util/src/future/mod.rs +++ b/futures-util/src/future/mod.rs @@ -6,7 +6,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; // re-export for `select!` #[doc(hidden)] @@ -668,10 +668,10 @@ pub trait FutureExt: Future { } /// A convenience for calling `Future::poll` on `Unpin` future types. - fn poll_unpin(&mut self, lw: &LocalWaker) -> Poll + fn poll_unpin(&mut self, waker: &Waker) -> Poll where Self: Unpin + Sized { - Pin::new(self).poll(lw) + Pin::new(self).poll(waker) } } diff --git a/futures-util/src/future/option.rs b/futures-util/src/future/option.rs index 8f21d5aad1..4c84ce35a3 100644 --- a/futures-util/src/future/option.rs +++ b/futures-util/src/future/option.rs @@ -2,7 +2,7 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// A future representing a value which may or may not be present. @@ -38,10 +38,10 @@ impl Future for OptionFuture { fn poll( self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll { match self.option().as_pin_mut() { - Some(x) => x.poll(lw).map(Some), + Some(x) => x.poll(waker).map(Some), None => Poll::Ready(None), } } diff --git a/futures-util/src/future/poll_fn.rs b/futures-util/src/future/poll_fn.rs index 1984e34a1c..84d377f140 100644 --- a/futures-util/src/future/poll_fn.rs +++ b/futures-util/src/future/poll_fn.rs @@ -2,7 +2,7 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future which wraps a function returning [`Poll`]. /// @@ -25,9 +25,9 @@ impl Unpin for PollFn {} /// #![feature(async_await, await_macro, futures_api)] /// # futures::executor::block_on(async { /// use futures::future::poll_fn; -/// use futures::task::{LocalWaker, Poll}; +/// use futures::task::{Waker, Poll}; /// -/// fn read_line(lw: &LocalWaker) -> Poll { +/// fn read_line(waker: &Waker) -> Poll { /// Poll::Ready("Hello, World!".into()) /// } /// @@ -37,17 +37,17 @@ impl Unpin for PollFn {} /// ``` pub fn poll_fn(f: F) -> PollFn where - F: FnMut(&LocalWaker) -> Poll + F: FnMut(&Waker) -> Poll { PollFn { f } } impl Future for PollFn - where F: FnMut(&LocalWaker) -> Poll, + where F: FnMut(&Waker) -> Poll, { type Output = T; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - (&mut self.f)(lw) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + (&mut self.f)(waker) } } diff --git a/futures-util/src/future/ready.rs b/futures-util/src/future/ready.rs index 9c960002f3..94b6c1b7a5 100644 --- a/futures-util/src/future/ready.rs +++ b/futures-util/src/future/ready.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future that is immediately ready with a value /// @@ -21,7 +21,7 @@ impl Future for Ready { type Output = T; #[inline] - fn poll(mut self: Pin<&mut Self>, _lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, _waker: &Waker) -> Poll { Poll::Ready(self.0.take().unwrap()) } } diff --git a/futures-util/src/future/remote_handle.rs b/futures-util/src/future/remote_handle.rs index e4a692d516..a16f0dee2c 100644 --- a/futures-util/src/future/remote_handle.rs +++ b/futures-util/src/future/remote_handle.rs @@ -3,7 +3,7 @@ use { futures_channel::oneshot::{self, Sender, Receiver}, futures_core::{ future::Future, - task::{LocalWaker, Poll}, + task::{Waker, Poll}, }, pin_utils::{unsafe_pinned, unsafe_unpinned}, std::{ @@ -41,8 +41,8 @@ impl RemoteHandle { impl Future for RemoteHandle { type Output = T; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - match self.rx.poll_unpin(lw) { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + match self.rx.poll_unpin(waker) { Poll::Ready(Ok(Ok(output))) => Poll::Ready(output), Poll::Ready(Ok(Err(e))) => panic::resume_unwind(e), Poll::Ready(Err(e)) => panic::resume_unwind(Box::new(e)), @@ -80,15 +80,15 @@ impl Remote { impl Future for Remote { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> { - if let Poll::Ready(_) = self.as_mut().tx().as_mut().unwrap().poll_cancel(lw) { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> { + if let Poll::Ready(_) = self.as_mut().tx().as_mut().unwrap().poll_cancel(waker) { if !self.keep_running.load(Ordering::SeqCst) { // Cancelled, bail out return Poll::Ready(()) } } - let output = match self.as_mut().future().poll(lw) { + let output = match self.as_mut().future().poll(waker) { Poll::Ready(output) => output, Poll::Pending => return Poll::Pending, }; diff --git a/futures-util/src/future/shared.rs b/futures-util/src/future/shared.rs index ceb6271a9a..45d61d73ca 100644 --- a/futures-util/src/future/shared.rs +++ b/futures-util/src/future/shared.rs @@ -1,6 +1,6 @@ -use crate::task::local_waker_ref_from_nonlocal; +use crate::task::{ArcWake, waker_ref}; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll, Wake, Waker}; +use futures_core::task::{Waker, Poll}; use slab::Slab; use std::cell::UnsafeCell; use std::fmt; @@ -110,7 +110,7 @@ where } /// Registers the current task to receive a wakeup when `Inner` is awoken. - fn set_waker(&mut self, lw: &LocalWaker) { + fn set_waker(&mut self, waker: &Waker) { // Acquire the lock first before checking COMPLETE to ensure there // isn't a race. let mut wakers_guard = if let Some(inner) = self.inner.as_ref() { @@ -126,18 +126,20 @@ where }; if self.waker_key == NULL_WAKER_KEY { - self.waker_key = wakers.insert(Some(lw.clone().into_waker())); + self.waker_key = wakers.insert(Some(waker.clone())); } else { let waker_slot = &mut wakers[self.waker_key]; - let needs_replacement = if let Some(old_waker) = waker_slot { + let needs_replacement = if let Some(_old_waker) = waker_slot { // If there's still an unwoken waker in the slot, only replace // if the current one wouldn't wake the same task. - !lw.will_wake_nonlocal(old_waker) + // TODO: This API is currently not available, so replace always + // !waker.will_wake_nonlocal(old_waker) + true } else { true }; if needs_replacement { - *waker_slot = Some(lw.clone().into_waker()); + *waker_slot = Some(waker.clone()); } } debug_assert!(self.waker_key != NULL_WAKER_KEY); @@ -185,10 +187,10 @@ where { type Output = Fut::Output; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let this = &mut *self; - this.set_waker(lw); + this.set_waker(waker); let inner = if let Some(inner) = this.inner.as_ref() { inner @@ -214,8 +216,8 @@ where _ => unreachable!(), } - let waker = local_waker_ref_from_nonlocal(&inner.notifier); - let lw = &waker; + let waker = waker_ref(&inner.notifier); + let waker = &waker; struct Reset<'a>(&'a AtomicUsize); @@ -239,7 +241,7 @@ where } }; - let poll = future.poll(&lw); + let poll = future.poll(&waker); match poll { Poll::Pending => { @@ -314,7 +316,7 @@ where } } -impl Wake for Notifier { +impl ArcWake for Notifier { fn wake(arc_self: &Arc) { arc_self.state.compare_and_swap(POLLING, REPOLL, SeqCst); diff --git a/futures-util/src/future/then.rs b/futures-util/src/future/then.rs index 2de497e2a3..c5815f9ea3 100644 --- a/futures-util/src/future/then.rs +++ b/futures-util/src/future/then.rs @@ -1,7 +1,7 @@ use super::Chain; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the `then` combinator, chaining computations on the end of @@ -39,7 +39,7 @@ impl Future for Then { type Output = Fut2::Output; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.as_mut().chain().poll(lw, |output, f| f(output)) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.as_mut().chain().poll(waker, |output, f| f(output)) } } diff --git a/futures-util/src/future/unit_error.rs b/futures-util/src/future/unit_error.rs index 476feb138d..12221d9251 100644 --- a/futures-util/src/future/unit_error.rs +++ b/futures-util/src/future/unit_error.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the `unit_error` combinator, turning a `Future` into a `TryFuture`. @@ -32,7 +32,7 @@ impl Future for UnitError { type Output = Result; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - self.future().poll(lw).map(Ok) + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll> { + self.future().poll(waker).map(Ok) } } diff --git a/futures-util/src/io/allow_std.rs b/futures-util/src/io/allow_std.rs index 8cc2f40dfc..4357ccb62d 100644 --- a/futures-util/src/io/allow_std.rs +++ b/futures-util/src/io/allow_std.rs @@ -1,4 +1,4 @@ -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::{AsyncRead, AsyncWrite}; use std::{fmt, io}; use std::string::String; @@ -73,19 +73,19 @@ impl io::Write for AllowStdIo where T: io::Write { } impl AsyncWrite for AllowStdIo where T: io::Write { - fn poll_write(&mut self, _: &LocalWaker, buf: &[u8]) + fn poll_write(&mut self, _: &Waker, buf: &[u8]) -> Poll> { Poll::Ready(Ok(try_with_interrupt!(io::Write::write(&mut self.0, buf)))) } - fn poll_flush(&mut self, _: &LocalWaker) -> Poll> { + fn poll_flush(&mut self, _: &Waker) -> Poll> { try_with_interrupt!(io::Write::flush(self)); Poll::Ready(Ok(())) } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll> { - self.poll_flush(lw) + fn poll_close(&mut self, waker: &Waker) -> Poll> { + self.poll_flush(waker) } } @@ -107,7 +107,7 @@ impl io::Read for AllowStdIo where T: io::Read { } impl AsyncRead for AllowStdIo where T: io::Read { - fn poll_read(&mut self, _: &LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, _: &Waker, buf: &mut [u8]) -> Poll> { Poll::Ready(Ok(try_with_interrupt!(io::Read::read(&mut self.0, buf)))) diff --git a/futures-util/src/io/close.rs b/futures-util/src/io/close.rs index a43f7ceab6..dda8e6e638 100644 --- a/futures-util/src/io/close.rs +++ b/futures-util/src/io/close.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::AsyncWrite; use std::io; use std::pin::Pin; @@ -26,7 +26,7 @@ impl<'a, W: AsyncWrite + ?Sized> Close<'a, W> { impl Future for Close<'_, W> { type Output = io::Result<()>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.writer.poll_close(lw) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.writer.poll_close(waker) } } diff --git a/futures-util/src/io/copy_into.rs b/futures-util/src/io/copy_into.rs index e544acb505..491c362ef1 100644 --- a/futures-util/src/io/copy_into.rs +++ b/futures-util/src/io/copy_into.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::{AsyncRead, AsyncWrite}; use std::boxed::Box; use std::io; @@ -45,13 +45,13 @@ impl Future for CopyInto<'_, R, W> { type Output = io::Result; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let this = &mut *self; loop { // If our buffer is empty, then we need to read some data to // continue. if this.pos == this.cap && !this.read_done { - let n = try_ready!(this.reader.poll_read(lw, &mut this.buf)); + let n = try_ready!(this.reader.poll_read(waker, &mut this.buf)); if n == 0 { this.read_done = true; } else { @@ -62,7 +62,7 @@ impl Future for CopyInto<'_, R, W> // If our buffer has some data, let's write it out! while this.pos < this.cap { - let i = try_ready!(this.writer.poll_write(lw, &this.buf[this.pos..this.cap])); + let i = try_ready!(this.writer.poll_write(waker, &this.buf[this.pos..this.cap])); if i == 0 { return Poll::Ready(Err(io::ErrorKind::WriteZero.into())) } else { @@ -75,7 +75,7 @@ impl Future for CopyInto<'_, R, W> // data and finish the transfer. // done with the entire transfer. if this.pos == this.cap && this.read_done { - try_ready!(this.writer.poll_flush(lw)); + try_ready!(this.writer.poll_flush(waker)); return Poll::Ready(Ok(this.amt)); } } diff --git a/futures-util/src/io/disabled/lines.rs b/futures-util/src/io/disabled/lines.rs index 933ba277e1..250479d448 100644 --- a/futures-util/src/io/disabled/lines.rs +++ b/futures-util/src/io/disabled/lines.rs @@ -45,7 +45,7 @@ impl Stream for Lines type Item = String; type Error = io::Error; - fn poll(&mut self, lw: &LocalWaker) -> Poll, io::Error> { + fn poll(&mut self, waker: &Waker) -> Poll, io::Error> { let n = ready!(self.io.read_line(&mut self.line)); if n == 0 && self.line.len() == 0 { return Ok(None.into()) diff --git a/futures-util/src/io/flush.rs b/futures-util/src/io/flush.rs index 184df70adf..51c26171f4 100644 --- a/futures-util/src/io/flush.rs +++ b/futures-util/src/io/flush.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use std::io; use std::pin::Pin; @@ -31,7 +31,7 @@ impl Future for Flush<'_, W> { type Output = io::Result<()>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.writer.poll_flush(lw) + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.writer.poll_flush(waker) } } diff --git a/futures-util/src/io/read.rs b/futures-util/src/io/read.rs index 4e03464d52..96dbb57765 100644 --- a/futures-util/src/io/read.rs +++ b/futures-util/src/io/read.rs @@ -1,6 +1,6 @@ use crate::io::AsyncRead; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use std::io; use std::pin::Pin; @@ -24,8 +24,8 @@ impl<'a, R: AsyncRead + ?Sized> Read<'a, R> { impl Future for Read<'_, R> { type Output = io::Result; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let this = &mut *self; - this.reader.poll_read(lw, this.buf) + this.reader.poll_read(waker, this.buf) } } diff --git a/futures-util/src/io/read_exact.rs b/futures-util/src/io/read_exact.rs index a1aadf0e35..09c6ed5992 100644 --- a/futures-util/src/io/read_exact.rs +++ b/futures-util/src/io/read_exact.rs @@ -1,6 +1,6 @@ use crate::io::AsyncRead; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use std::io; use std::mem; use std::pin::Pin; @@ -28,10 +28,10 @@ impl<'a, R: AsyncRead + ?Sized> ReadExact<'a, R> { impl Future for ReadExact<'_, R> { type Output = io::Result<()>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let this = &mut *self; while !this.buf.is_empty() { - let n = try_ready!(this.reader.poll_read(lw, this.buf)); + let n = try_ready!(this.reader.poll_read(waker, this.buf)); { let (_, rest) = mem::replace(&mut this.buf, &mut []).split_at_mut(n); this.buf = rest; diff --git a/futures-util/src/io/read_to_end.rs b/futures-util/src/io/read_to_end.rs index 1f8f903330..a9432bd312 100644 --- a/futures-util/src/io/read_to_end.rs +++ b/futures-util/src/io/read_to_end.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::AsyncRead; use std::io; use std::pin::Pin; @@ -45,7 +45,7 @@ impl Drop for Guard<'_> { // readers, we need to make sure to truncate that if any of this panics. fn read_to_end_internal( rd: &mut R, - lw: &LocalWaker, + waker: &Waker, buf: &mut Vec, ) -> Poll> { let mut g = Guard { len: buf.len(), buf }; @@ -60,7 +60,7 @@ fn read_to_end_internal( } } - match rd.poll_read(lw, &mut g.buf[g.len..]) { + match rd.poll_read(waker, &mut g.buf[g.len..]) { Poll::Ready(Ok(0)) => { ret = Poll::Ready(Ok(())); break; @@ -82,8 +82,8 @@ impl Future for ReadToEnd<'_, A> { type Output = io::Result<()>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let this = &mut *self; - read_to_end_internal(this.reader, lw, this.buf) + read_to_end_internal(this.reader, waker, this.buf) } } diff --git a/futures-util/src/io/split.rs b/futures-util/src/io/split.rs index c808dd331b..8103c613d6 100644 --- a/futures-util/src/io/split.rs +++ b/futures-util/src/io/split.rs @@ -1,5 +1,5 @@ use crate::lock::BiLock; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::{AsyncRead, AsyncWrite, IoVec}; use std::io; use std::pin::Pin; @@ -18,15 +18,15 @@ pub struct WriteHalf { fn lock_and_then( lock: &BiLock, - lw: &LocalWaker, + waker: &Waker, f: F ) -> Poll> - where F: FnOnce(&mut T, &LocalWaker) -> Poll> + where F: FnOnce(&mut T, &Waker) -> Poll> { - match lock.poll_lock(lw) { + match lock.poll_lock(waker) { // Safety: the value behind the bilock used by `ReadHalf` and `WriteHalf` is never exposed // as a `Pin<&mut T>` anywhere other than here as a way to get to `&mut T`. - Poll::Ready(mut l) => f(unsafe { Pin::get_unchecked_mut(l.as_pin_mut()) }, lw), + Poll::Ready(mut l) => f(unsafe { Pin::get_unchecked_mut(l.as_pin_mut()) }, waker), Poll::Pending => Poll::Pending, } } @@ -37,37 +37,37 @@ pub fn split(t: T) -> (ReadHalf, WriteHalf) { } impl AsyncRead for ReadHalf { - fn poll_read(&mut self, lw: &LocalWaker, buf: &mut [u8]) + fn poll_read(&mut self, waker: &Waker, buf: &mut [u8]) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_read(lw, buf)) + lock_and_then(&self.handle, waker, |l, waker| l.poll_read(waker, buf)) } - fn poll_vectored_read(&mut self, lw: &LocalWaker, vec: &mut [&mut IoVec]) + fn poll_vectored_read(&mut self, waker: &Waker, vec: &mut [&mut IoVec]) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_vectored_read(lw, vec)) + lock_and_then(&self.handle, waker, |l, waker| l.poll_vectored_read(waker, vec)) } } impl AsyncWrite for WriteHalf { - fn poll_write(&mut self, lw: &LocalWaker, buf: &[u8]) + fn poll_write(&mut self, waker: &Waker, buf: &[u8]) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_write(lw, buf)) + lock_and_then(&self.handle, waker, |l, waker| l.poll_write(waker, buf)) } - fn poll_vectored_write(&mut self, lw: &LocalWaker, vec: &[&IoVec]) + fn poll_vectored_write(&mut self, waker: &Waker, vec: &[&IoVec]) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_vectored_write(lw, vec)) + lock_and_then(&self.handle, waker, |l, waker| l.poll_vectored_write(waker, vec)) } - fn poll_flush(&mut self, lw: &LocalWaker) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_flush(lw)) + fn poll_flush(&mut self, waker: &Waker) -> Poll> { + lock_and_then(&self.handle, waker, |l, waker| l.poll_flush(waker)) } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll> { - lock_and_then(&self.handle, lw, |l, lw| l.poll_close(lw)) + fn poll_close(&mut self, waker: &Waker) -> Poll> { + lock_and_then(&self.handle, waker, |l, waker| l.poll_close(waker)) } } diff --git a/futures-util/src/io/write_all.rs b/futures-util/src/io/write_all.rs index 9b05f4e97d..a85a2e851d 100644 --- a/futures-util/src/io/write_all.rs +++ b/futures-util/src/io/write_all.rs @@ -1,5 +1,5 @@ use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::AsyncWrite; use std::io; use std::mem; @@ -28,10 +28,10 @@ impl<'a, W: AsyncWrite + ?Sized> WriteAll<'a, W> { impl Future for WriteAll<'_, W> { type Output = io::Result<()>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { let this = &mut *self; while !this.buf.is_empty() { - let n = try_ready!(this.writer.poll_write(lw, this.buf)); + let n = try_ready!(this.writer.poll_write(waker, this.buf)); { let (_, rest) = mem::replace(&mut this.buf, &[]).split_at(n); this.buf = rest; diff --git a/futures-util/src/lib.rs b/futures-util/src/lib.rs index d9b3c3ba61..342d6bac1e 100644 --- a/futures-util/src/lib.rs +++ b/futures-util/src/lib.rs @@ -38,9 +38,9 @@ macro_rules! delegate_sink { ($field:ident) => { fn poll_ready( self: Pin<&mut Self>, - lw: &$crate::core_reexport::task::LocalWaker, + waker: &$crate::core_reexport::task::Waker, ) -> $crate::core_reexport::task::Poll> { - self.$field().poll_ready(lw) + self.$field().poll_ready(waker) } fn start_send( @@ -52,16 +52,16 @@ macro_rules! delegate_sink { fn poll_flush( self: Pin<&mut Self>, - lw: &$crate::core_reexport::task::LocalWaker + waker: &$crate::core_reexport::task::Waker ) -> $crate::core_reexport::task::Poll> { - self.$field().poll_flush(lw) + self.$field().poll_flush(waker) } fn poll_close( self: Pin<&mut Self>, - lw: &$crate::core_reexport::task::LocalWaker + waker: &$crate::core_reexport::task::Waker ) -> $crate::core_reexport::task::Poll> { - self.$field().poll_close(lw) + self.$field().poll_close(waker) } } } diff --git a/futures-util/src/lock/bilock.rs b/futures-util/src/lock/bilock.rs index f5d8c61c9f..a8c9676695 100644 --- a/futures-util/src/lock/bilock.rs +++ b/futures-util/src/lock/bilock.rs @@ -2,7 +2,7 @@ #![allow(unused)] use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll, Waker}; +use futures_core::task::{Waker, Poll}; use std::any::Any; use std::boxed::Box; use std::cell::UnsafeCell; @@ -86,7 +86,7 @@ impl BiLock { /// /// This function will panic if called outside the context of a future's /// task. - pub fn poll_lock(&self, lw: &LocalWaker) -> Poll> { + pub fn poll_lock(&self, waker: &Waker) -> Poll> { loop { match self.arc.state.swap(1, SeqCst) { // Woohoo, we grabbed the lock! @@ -103,7 +103,7 @@ impl BiLock { } // type ascription for safety's sake! - let me: Box = Box::new(lw.clone().into_waker()); + let me: Box = Box::new(waker.clone()); let me = Box::into_raw(me) as usize; match self.arc.state.compare_exchange(1, me, SeqCst, SeqCst) { @@ -268,7 +268,7 @@ impl<'a, T> Unpin for BiLockAcquire<'a, T> {} impl<'a, T> Future for BiLockAcquire<'a, T> { type Output = BiLockGuard<'a, T>; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.bilock.poll_lock(lw) + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.bilock.poll_lock(waker) } } diff --git a/futures-util/src/lock/mutex.rs b/futures-util/src/lock/mutex.rs index e6bde4a891..0767f0fbf3 100644 --- a/futures-util/src/lock/mutex.rs +++ b/futures-util/src/lock/mutex.rs @@ -1,5 +1,5 @@ use futures_core::future::{FusedFuture, Future}; -use futures_core::task::{LocalWaker, Poll, Waker}; +use futures_core::task::{Waker, Poll}; use slab::Slab; use std::{fmt, mem, usize}; use std::cell::UnsafeCell; @@ -37,10 +37,10 @@ enum Waiter { } impl Waiter { - fn register(&mut self, lw: &LocalWaker) { + fn register(&mut self, waker: &Waker) { match self { - Waiter::Waiting(waker) if lw.will_wake_nonlocal(waker) => {}, - _ => *self = Waiter::Waiting(lw.clone().into_waker()), + Waiter::Waiting(waker) if waker.will_wake(waker) => {}, + _ => *self = Waiter::Waiting(waker.clone()), } } @@ -147,7 +147,7 @@ impl FusedFuture for MutexLockFuture<'_, T> { impl<'a, T> Future for MutexLockFuture<'a, T> { type Output = MutexGuard<'a, T>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { let mutex = self.mutex.expect("polled MutexLockFuture after completion"); if let Some(lock) = mutex.try_lock() { @@ -159,12 +159,12 @@ impl<'a, T> Future for MutexLockFuture<'a, T> { { let mut waiters = mutex.waiters.lock().unwrap(); if self.wait_key == WAIT_KEY_NONE { - self.wait_key = waiters.insert(Waiter::Waiting(lw.clone().into_waker())); + self.wait_key = waiters.insert(Waiter::Waiting(waker.clone())); if waiters.len() == 1 { mutex.state.fetch_or(HAS_WAITERS, Ordering::Relaxed); // released by mutex unlock } } else { - waiters[self.wait_key].register(lw); + waiters[self.wait_key].register(waker); } } diff --git a/futures-util/src/macros/poll.rs b/futures-util/src/macros/poll.rs index df5a588661..92a3e79b0b 100644 --- a/futures-util/src/macros/poll.rs +++ b/futures-util/src/macros/poll.rs @@ -14,25 +14,6 @@ macro_rules! try_ready { } } - -/// Extracts `Poll` from `Poll>`. -/// -/// This macro bakes in propagation of `Err` signals by returning early. -/// This macro bakes in propagation of `Pending` and `Err` signals by returning early. -#[macro_export] -macro_rules! try_poll { - ($x:expr) => { - match $x { - $crate::core_reexport::task::Poll::Ready(Ok(x)) => - $crate::core_reexport::task::Poll::Ready(x), - $crate::core_reexport::task::Poll::Ready(Err(e)) => - return $crate::core_reexport::task::Poll::Ready(Err(e.into())), - $crate::core_reexport::task::Poll::Pending => - $crate::core_reexport::task::Poll::Pending, - } - } -} - /// Extracts the successful type of a `Poll`. /// /// This macro bakes in propagation of `Pending` signals by returning early. diff --git a/futures-util/src/sink/buffer.rs b/futures-util/src/sink/buffer.rs index a715903cf5..e61c1f1a69 100644 --- a/futures-util/src/sink/buffer.rs +++ b/futures-util/src/sink/buffer.rs @@ -1,5 +1,5 @@ use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::collections::VecDeque; @@ -40,15 +40,15 @@ impl Buffer { fn try_empty_buffer( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { - try_ready!(self.as_mut().sink().poll_ready(lw)); + try_ready!(self.as_mut().sink().poll_ready(waker)); while let Some(item) = self.as_mut().buf().pop_front() { if let Err(e) = self.as_mut().sink().start_send(item) { return Poll::Ready(Err(e)); } if !self.buf.is_empty() { - try_ready!(self.as_mut().sink().poll_ready(lw)); + try_ready!(self.as_mut().sink().poll_ready(waker)); } } Poll::Ready(Ok(())) @@ -59,8 +59,8 @@ impl Buffer { impl Stream for Buffer where S: Sink + Stream { type Item = S::Item; - fn poll_next(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - self.sink().poll_next(lw) + fn poll_next(self: Pin<&mut Self>, waker: &Waker) -> Poll> { + self.sink().poll_next(waker) } } @@ -70,13 +70,13 @@ impl Sink for Buffer { fn poll_ready( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if self.capacity == 0 { - return self.as_mut().sink().poll_ready(lw); + return self.as_mut().sink().poll_ready(waker); } - if let Poll::Ready(Err(e)) = self.as_mut().try_empty_buffer(lw) { + if let Poll::Ready(Err(e)) = self.as_mut().try_empty_buffer(waker) { return Poll::Ready(Err(e)); } @@ -101,19 +101,19 @@ impl Sink for Buffer { fn poll_flush( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - try_ready!(self.as_mut().try_empty_buffer(lw)); + try_ready!(self.as_mut().try_empty_buffer(waker)); debug_assert!(self.as_mut().buf().is_empty()); - self.as_mut().sink().poll_flush(lw) + self.as_mut().sink().poll_flush(waker) } fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - try_ready!(self.as_mut().try_empty_buffer(lw)); + try_ready!(self.as_mut().try_empty_buffer(waker)); debug_assert!(self.as_mut().buf().is_empty()); - self.as_mut().sink().poll_close(lw) + self.as_mut().sink().poll_close(waker) } } diff --git a/futures-util/src/sink/close.rs b/futures-util/src/sink/close.rs index 51ccd20038..41c6f4a82d 100644 --- a/futures-util/src/sink/close.rs +++ b/futures-util/src/sink/close.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; /// Future for the `close` combinator, which polls the sink until all data has @@ -25,8 +25,8 @@ impl Future for Close<'_, Si> { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - Pin::new(&mut self.sink).poll_close(lw) + Pin::new(&mut self.sink).poll_close(waker) } } diff --git a/futures-util/src/sink/drain.rs b/futures-util/src/sink/drain.rs index 05021f5e11..5e6554fd55 100644 --- a/futures-util/src/sink/drain.rs +++ b/futures-util/src/sink/drain.rs @@ -1,7 +1,7 @@ use core::fmt; use core::marker::PhantomData; use core::pin::Pin; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; /// A sink that will discard all items given to it. @@ -43,7 +43,7 @@ impl Sink for Drain { fn poll_ready( self: Pin<&mut Self>, - _lw: &LocalWaker, + _waker: &Waker, ) -> Poll> { Poll::Ready(Ok(())) } @@ -57,14 +57,14 @@ impl Sink for Drain { fn poll_flush( self: Pin<&mut Self>, - _lw: &LocalWaker, + _waker: &Waker, ) -> Poll> { Poll::Ready(Ok(())) } fn poll_close( self: Pin<&mut Self>, - _lw: &LocalWaker, + _waker: &Waker, ) -> Poll> { Poll::Ready(Ok(())) } diff --git a/futures-util/src/sink/err_into.rs b/futures-util/src/sink/err_into.rs index 99609d55e1..65338d65be 100644 --- a/futures-util/src/sink/err_into.rs +++ b/futures-util/src/sink/err_into.rs @@ -1,7 +1,7 @@ use crate::sink::{SinkExt, SinkMapErr}; use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::{Sink}; use pin_utils::unsafe_pinned; @@ -63,8 +63,8 @@ impl Stream for SinkErrInto fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_next(lw) + self.sink().poll_next(waker) } } diff --git a/futures-util/src/sink/fanout.rs b/futures-util/src/sink/fanout.rs index c2d92c38a0..bb93a45c3f 100644 --- a/futures-util/src/sink/fanout.rs +++ b/futures-util/src/sink/fanout.rs @@ -1,6 +1,6 @@ use core::fmt::{Debug, Formatter, Result as FmtResult}; use core::pin::Pin; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::unsafe_pinned; @@ -52,10 +52,10 @@ impl Sink for Fanout fn poll_ready( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - let sink1_ready = try_poll!(self.as_mut().sink1().poll_ready(lw)).is_ready(); - let sink2_ready = try_poll!(self.as_mut().sink2().poll_ready(lw)).is_ready(); + let sink1_ready = self.as_mut().sink1().poll_ready(waker)?.is_ready(); + let sink2_ready = self.as_mut().sink2().poll_ready(waker)?.is_ready(); let ready = sink1_ready && sink2_ready; if ready { Poll::Ready(Ok(())) } else { Poll::Pending } } @@ -71,20 +71,20 @@ impl Sink for Fanout fn poll_flush( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - let sink1_ready = try_poll!(self.as_mut().sink1().poll_flush(lw)).is_ready(); - let sink2_ready = try_poll!(self.as_mut().sink2().poll_flush(lw)).is_ready(); + let sink1_ready = self.as_mut().sink1().poll_flush(waker)?.is_ready(); + let sink2_ready = self.as_mut().sink2().poll_flush(waker)?.is_ready(); let ready = sink1_ready && sink2_ready; if ready { Poll::Ready(Ok(())) } else { Poll::Pending } } fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - let sink1_ready = try_poll!(self.as_mut().sink1().poll_close(lw)).is_ready(); - let sink2_ready = try_poll!(self.as_mut().sink2().poll_close(lw)).is_ready(); + let sink1_ready = self.as_mut().sink1().poll_close(waker)?.is_ready(); + let sink2_ready = self.as_mut().sink2().poll_close(waker)?.is_ready(); let ready = sink1_ready && sink2_ready; if ready { Poll::Ready(Ok(())) } else { Poll::Pending } } diff --git a/futures-util/src/sink/flush.rs b/futures-util/src/sink/flush.rs index c75dbded4a..814cf05903 100644 --- a/futures-util/src/sink/flush.rs +++ b/futures-util/src/sink/flush.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; /// Future for the `flush` combinator, which polls the sink until all data @@ -31,8 +31,8 @@ impl Future for Flush<'_, Si> { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - Pin::new(&mut self.sink).poll_flush(lw) + Pin::new(&mut self.sink).poll_flush(waker) } } diff --git a/futures-util/src/sink/map_err.rs b/futures-util/src/sink/map_err.rs index 489e375d1d..5618847ce3 100644 --- a/futures-util/src/sink/map_err.rs +++ b/futures-util/src/sink/map_err.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::{Sink}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -60,10 +60,10 @@ impl Sink for SinkMapErr fn poll_ready( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { #[allow(clippy::redundant_closure)] // https://github.com/rust-lang-nursery/rust-clippy/issues/1439 - self.as_mut().sink().poll_ready(lw).map_err(|e| self.as_mut().take_f()(e)) + self.as_mut().sink().poll_ready(waker).map_err(|e| self.as_mut().take_f()(e)) } fn start_send( @@ -76,18 +76,18 @@ impl Sink for SinkMapErr fn poll_flush( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { #[allow(clippy::redundant_closure)] // https://github.com/rust-lang-nursery/rust-clippy/issues/1439 - self.as_mut().sink().poll_flush(lw).map_err(|e| self.as_mut().take_f()(e)) + self.as_mut().sink().poll_flush(waker).map_err(|e| self.as_mut().take_f()(e)) } fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { #[allow(clippy::redundant_closure)] // https://github.com/rust-lang-nursery/rust-clippy/issues/1439 - self.as_mut().sink().poll_close(lw).map_err(|e| self.as_mut().take_f()(e)) + self.as_mut().sink().poll_close(waker).map_err(|e| self.as_mut().take_f()(e)) } } @@ -96,8 +96,8 @@ impl Stream for SinkMapErr { fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_next(lw) + self.sink().poll_next(waker) } } diff --git a/futures-util/src/sink/send.rs b/futures-util/src/sink/send.rs index fff0731e79..82a3cbc510 100644 --- a/futures-util/src/sink/send.rs +++ b/futures-util/src/sink/send.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::Future; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; /// Future for the `Sink::send` combinator, which sends a value to a sink and @@ -29,12 +29,12 @@ impl Future for Send<'_, Si> { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { let this = &mut *self; if let Some(item) = this.item.take() { let mut sink = Pin::new(&mut this.sink); - match sink.as_mut().poll_ready(lw) { + match sink.as_mut().poll_ready(waker) { Poll::Ready(Ok(())) => { if let Err(e) = sink.as_mut().start_send(item) { return Poll::Ready(Err(e)); @@ -50,7 +50,7 @@ impl Future for Send<'_, Si> { // we're done sending the item, but want to block on flushing the // sink - try_ready!(Pin::new(&mut this.sink).poll_flush(lw)); + try_ready!(Pin::new(&mut this.sink).poll_flush(waker)); Poll::Ready(Ok(())) } diff --git a/futures-util/src/sink/send_all.rs b/futures-util/src/sink/send_all.rs index 3f42d5d775..022e55b935 100644 --- a/futures-util/src/sink/send_all.rs +++ b/futures-util/src/sink/send_all.rs @@ -2,7 +2,7 @@ use crate::stream::{StreamExt, Fuse}; use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; /// Future for the `Sink::send_all` combinator, which sends a stream of values @@ -44,11 +44,11 @@ where fn try_start_send( &mut self, - lw: &LocalWaker, + waker: &Waker, item: Si::SinkItem, ) -> Poll> { debug_assert!(self.buffered.is_none()); - match Pin::new(&mut self.sink).poll_ready(lw) { + match Pin::new(&mut self.sink).poll_ready(waker) { Poll::Ready(Ok(())) => { Poll::Ready(Pin::new(&mut self.sink).start_send(item)) } @@ -70,26 +70,26 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { let this = &mut *self; // If we've got an item buffered already, we need to write it to the // sink before we can do anything else if let Some(item) = this.buffered.take() { - try_ready!(this.try_start_send(lw, item)) + try_ready!(this.try_start_send(waker, item)) } loop { - match this.stream.poll_next_unpin(lw) { + match this.stream.poll_next_unpin(waker) { Poll::Ready(Some(item)) => { - try_ready!(this.try_start_send(lw, item)) + try_ready!(this.try_start_send(waker, item)) } Poll::Ready(None) => { - try_ready!(Pin::new(&mut this.sink).poll_flush(lw)); + try_ready!(Pin::new(&mut this.sink).poll_flush(waker)); return Poll::Ready(Ok(())) } Poll::Pending => { - try_ready!(Pin::new(&mut this.sink).poll_flush(lw)); + try_ready!(Pin::new(&mut this.sink).poll_flush(waker)); return Poll::Pending } } diff --git a/futures-util/src/sink/with.rs b/futures-util/src/sink/with.rs index bb5c145243..b363c7d79a 100644 --- a/futures-util/src/sink/with.rs +++ b/futures-util/src/sink/with.rs @@ -3,7 +3,7 @@ use core::mem; use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -88,9 +88,9 @@ impl Stream for With fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_next(lw) + self.sink().poll_next(waker) } } @@ -120,11 +120,11 @@ impl With fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let buffered = match self.as_mut().state().as_pin_mut() { State::Empty => return Poll::Ready(Ok(())), - State::Process(fut) => Some(try_ready!(fut.poll(lw))), + State::Process(fut) => Some(try_ready!(fut.poll(waker))), State::Buffered(_) => None, }; if let Some(buffered) = buffered { @@ -149,9 +149,9 @@ impl Sink for With fn poll_ready( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.poll(lw) + self.poll(waker) } fn start_send( @@ -165,19 +165,19 @@ impl Sink for With fn poll_flush( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - try_ready!(self.as_mut().poll(lw)); - try_ready!(self.as_mut().sink().poll_flush(lw)); + try_ready!(self.as_mut().poll(waker)); + try_ready!(self.as_mut().sink().poll_flush(waker)); Poll::Ready(Ok(())) } fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - try_ready!(self.as_mut().poll(lw)); - try_ready!(self.as_mut().sink().poll_close(lw)); + try_ready!(self.as_mut().poll(waker)); + try_ready!(self.as_mut().sink().poll_close(waker)); Poll::Ready(Ok(())) } } diff --git a/futures-util/src/sink/with_flat_map.rs b/futures-util/src/sink/with_flat_map.rs index 8d0697050a..30d4a6a843 100644 --- a/futures-util/src/sink/with_flat_map.rs +++ b/futures-util/src/sink/with_flat_map.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -76,7 +76,7 @@ where fn try_empty_stream( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let WithFlatMap { sink, stream, buffer, .. } = unsafe { Pin::get_unchecked_mut(self) }; @@ -84,17 +84,15 @@ where let mut stream = unsafe { Pin::new_unchecked(stream) }; if buffer.is_some() { - try_ready!(sink.as_mut().poll_ready(lw)); + try_ready!(sink.as_mut().poll_ready(waker)); let item = buffer.take().unwrap(); try_ready!(Poll::Ready(sink.as_mut().start_send(item))); } if let Some(mut some_stream) = stream.as_mut().as_pin_mut() { - while let Some(x) = ready!(some_stream.as_mut().poll_next(lw)) { + while let Some(x) = ready!(some_stream.as_mut().poll_next(waker)) { let item = try_ready!(Poll::Ready(x)); - match try_poll!(sink.as_mut().poll_ready(lw)) { - Poll::Ready(()) => { - try_poll!(Poll::Ready(sink.as_mut().start_send(item))) - } + match sink.as_mut().poll_ready(waker)? { + Poll::Ready(()) => sink.as_mut().start_send(item)?, Poll::Pending => { *buffer = Some(item); return Poll::Pending; @@ -116,9 +114,9 @@ where type Item = S::Item; fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_next(lw) + self.sink().poll_next(waker) } } @@ -133,9 +131,9 @@ where fn poll_ready( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.try_empty_stream(lw) + self.try_empty_stream(waker) } fn start_send( @@ -150,22 +148,22 @@ where fn poll_flush( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - match self.as_mut().try_empty_stream(lw) { + match self.as_mut().try_empty_stream(waker) { Poll::Pending => Poll::Pending, - Poll::Ready(Ok(())) => self.as_mut().sink().poll_flush(lw), + Poll::Ready(Ok(())) => self.as_mut().sink().poll_flush(waker), Poll::Ready(Err(e)) => Poll::Ready(Err(e)), } } fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - match self.as_mut().try_empty_stream(lw) { + match self.as_mut().try_empty_stream(waker) { Poll::Pending => Poll::Pending, - Poll::Ready(Ok(())) => self.as_mut().sink().poll_close(lw), + Poll::Ready(Ok(())) => self.as_mut().sink().poll_close(waker), Poll::Ready(Err(e)) => Poll::Ready(Err(e)), } } diff --git a/futures-util/src/stream/buffer_unordered.rs b/futures-util/src/stream/buffer_unordered.rs index d6b98dc8a9..f42f977fad 100644 --- a/futures-util/src/stream/buffer_unordered.rs +++ b/futures-util/src/stream/buffer_unordered.rs @@ -1,7 +1,7 @@ use crate::stream::{Fuse, FuturesUnordered}; use futures_core::future::Future; use futures_core::stream::{Stream, FusedStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::fmt; @@ -107,19 +107,19 @@ where fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { // First up, try to spawn off as many futures as possible by filling up // our slab of futures. while self.in_progress_queue.len() < self.max { - match self.as_mut().stream().poll_next(lw) { + match self.as_mut().stream().poll_next(waker) { Poll::Ready(Some(fut)) => self.as_mut().in_progress_queue().push(fut), Poll::Ready(None) | Poll::Pending => break, } } // Attempt to pull the next value from the in_progress_queue - match Pin::new(self.as_mut().in_progress_queue()).poll_next(lw) { + match Pin::new(self.as_mut().in_progress_queue()).poll_next(waker) { x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x, Poll::Ready(None) => {} } diff --git a/futures-util/src/stream/buffered.rs b/futures-util/src/stream/buffered.rs index 284fff4abf..e8b1b128d1 100644 --- a/futures-util/src/stream/buffered.rs +++ b/futures-util/src/stream/buffered.rs @@ -1,7 +1,7 @@ use crate::stream::{Fuse, FuturesOrdered}; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::fmt; @@ -103,19 +103,19 @@ where fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { // Try to spawn off as many futures as possible by filling up // our in_progress_queue of futures. while self.in_progress_queue.len() < self.max { - match self.as_mut().stream().poll_next(lw) { + match self.as_mut().stream().poll_next(waker) { Poll::Ready(Some(fut)) => self.as_mut().in_progress_queue().push(fut), Poll::Ready(None) | Poll::Pending => break, } } // Attempt to pull the next value from the in_progress_queue - let res = Pin::new(self.as_mut().in_progress_queue()).poll_next(lw); + let res = Pin::new(self.as_mut().in_progress_queue()).poll_next(waker); if let Some(val) = ready!(res) { return Poll::Ready(Some(val)) } diff --git a/futures-util/src/stream/catch_unwind.rs b/futures-util/src/stream/catch_unwind.rs index 9e153f8214..ba432662aa 100644 --- a/futures-util/src/stream/catch_unwind.rs +++ b/futures-util/src/stream/catch_unwind.rs @@ -1,5 +1,5 @@ use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::any::Any; use std::pin::Pin; @@ -31,13 +31,13 @@ impl Stream for CatchUnwind fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if *self.as_mut().caught_unwind() { Poll::Ready(None) } else { let res = catch_unwind(AssertUnwindSafe(|| { - self.as_mut().stream().poll_next(lw) + self.as_mut().stream().poll_next(waker) })); match res { diff --git a/futures-util/src/stream/chain.rs b/futures-util/src/stream/chain.rs index 1ee79f5fdf..5ee048c718 100644 --- a/futures-util/src/stream/chain.rs +++ b/futures-util/src/stream/chain.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// An adapter for chaining the output of two streams. @@ -44,14 +44,14 @@ where St1: Stream, fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if let Some(first) = self.as_mut().first().as_pin_mut() { - if let Some(item) = ready!(first.poll_next(lw)) { + if let Some(item) = ready!(first.poll_next(waker)) { return Poll::Ready(Some(item)) } } self.as_mut().first().set(None); - self.as_mut().second().poll_next(lw) + self.as_mut().second().poll_next(waker) } } diff --git a/futures-util/src/stream/chunks.rs b/futures-util/src/stream/chunks.rs index f6407ab8c1..ae54ae60bd 100644 --- a/futures-util/src/stream/chunks.rs +++ b/futures-util/src/stream/chunks.rs @@ -1,6 +1,6 @@ use crate::stream::Fuse; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::mem; use std::pin::Pin; @@ -67,11 +67,11 @@ impl Stream for Chunks { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let cap = self.items.capacity(); loop { - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { // Push the item into the buffer and check whether it is full. // If so, replace our buffer with a new and empty one and return // the full one. diff --git a/futures-util/src/stream/collect.rs b/futures-util/src/stream/collect.rs index 0f6c692293..5324717000 100644 --- a/futures-util/src/stream/collect.rs +++ b/futures-util/src/stream/collect.rs @@ -2,7 +2,7 @@ use core::mem; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A future which collects all of the values of a stream into a vector. @@ -45,9 +45,9 @@ where St: Stream, { type Output = C; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => self.as_mut().collection().extend(Some(e)), None => return Poll::Ready(self.as_mut().finish()), } diff --git a/futures-util/src/stream/concat.rs b/futures-util/src/stream/concat.rs index 6748724ba9..5cc26b4c47 100644 --- a/futures-util/src/stream/concat.rs +++ b/futures-util/src/stream/concat.rs @@ -3,7 +3,7 @@ use core::pin::Pin; use core::default::Default; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator to concatenate the results of a stream into the first @@ -53,10 +53,10 @@ where St: Stream, type Output = St::Item; fn poll( - mut self: Pin<&mut Self>, lw: &LocalWaker + mut self: Pin<&mut Self>, waker: &Waker ) -> Poll { loop { - match self.as_mut().stream().poll_next(lw) { + match self.as_mut().stream().poll_next(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(None) => { return Poll::Ready(self.as_mut().accum().take().unwrap_or_default()) diff --git a/futures-util/src/stream/empty.rs b/futures-util/src/stream/empty.rs index f28edcacfa..67dfa84813 100644 --- a/futures-util/src/stream/empty.rs +++ b/futures-util/src/stream/empty.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A stream which contains no elements. /// @@ -26,7 +26,7 @@ impl Unpin for Empty {} impl Stream for Empty { type Item = T; - fn poll_next(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_next(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(None) } } diff --git a/futures-util/src/stream/filter.rs b/futures-util/src/stream/filter.rs index 9ef220eac9..c343dc3fec 100644 --- a/futures-util/src/stream/filter.rs +++ b/futures-util/src/stream/filter.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator used to filter the results of a stream and only yield @@ -89,11 +89,11 @@ impl Stream for Filter fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { loop { if self.as_mut().pending_fut().as_pin_mut().is_none() { - let item = match ready!(self.as_mut().stream().poll_next(lw)) { + let item = match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => e, None => return Poll::Ready(None), }; @@ -102,7 +102,7 @@ impl Stream for Filter *self.as_mut().pending_item() = Some(item); } - let yield_item = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(lw)); + let yield_item = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(waker)); self.as_mut().pending_fut().set(None); let item = self.as_mut().pending_item().take().unwrap(); diff --git a/futures-util/src/stream/filter_map.rs b/futures-util/src/stream/filter_map.rs index 6ad4192a00..69fe68664e 100644 --- a/futures-util/src/stream/filter_map.rs +++ b/futures-util/src/stream/filter_map.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A combinator used to filter the results of a stream and simultaneously map @@ -82,11 +82,11 @@ impl Stream for FilterMap fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { loop { if self.as_mut().pending().as_pin_mut().is_none() { - let item = match ready!(self.as_mut().stream().poll_next(lw)) { + let item = match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => e, None => return Poll::Ready(None), }; @@ -94,7 +94,7 @@ impl Stream for FilterMap self.as_mut().pending().set(Some(fut)); } - let item = ready!(self.as_mut().pending().as_pin_mut().unwrap().poll(lw)); + let item = ready!(self.as_mut().pending().as_pin_mut().unwrap().poll(waker)); self.as_mut().pending().set(None); if item.is_some() { return Poll::Ready(item); diff --git a/futures-util/src/stream/flatten.rs b/futures-util/src/stream/flatten.rs index 6e34c95022..ac0ff3184f 100644 --- a/futures-util/src/stream/flatten.rs +++ b/futures-util/src/stream/flatten.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// A combinator used to flatten a stream-of-streams into one long stream of @@ -70,16 +70,16 @@ impl Stream for Flatten fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { loop { if self.as_mut().next().as_pin_mut().is_none() { - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => self.as_mut().next().set(Some(e)), None => return Poll::Ready(None), } } - let item = ready!(self.as_mut().next().as_pin_mut().unwrap().poll_next(lw)); + let item = ready!(self.as_mut().next().as_pin_mut().unwrap().poll_next(waker)); if item.is_some() { return Poll::Ready(item); } else { diff --git a/futures-util/src/stream/fold.rs b/futures-util/src/stream/fold.rs index 6ed5f5b48e..ce15260165 100644 --- a/futures-util/src/stream/fold.rs +++ b/futures-util/src/stream/fold.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A future used to collect all the results of a stream into one generic type. @@ -51,16 +51,16 @@ impl Future for Fold { type Output = T; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { // we're currently processing a future to produce a new accum value if self.as_mut().accum().is_none() { - let accum = ready!(self.as_mut().future().as_pin_mut().unwrap().poll(lw)); + let accum = ready!(self.as_mut().future().as_pin_mut().unwrap().poll(waker)); *self.as_mut().accum() = Some(accum); self.as_mut().future().set(None); } - let item = ready!(self.as_mut().stream().poll_next(lw)); + let item = ready!(self.as_mut().stream().poll_next(waker)); let accum = self.as_mut().accum().take() .expect("Fold polled after completion"); diff --git a/futures-util/src/stream/for_each.rs b/futures-util/src/stream/for_each.rs index d5f06d3d76..acddeb89e4 100644 --- a/futures-util/src/stream/for_each.rs +++ b/futures-util/src/stream/for_each.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which executes a unit closure over each item on a @@ -53,14 +53,14 @@ impl Future for ForEach { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> { loop { if let Some(future) = self.as_mut().future().as_pin_mut() { - ready!(future.poll(lw)); + ready!(future.poll(waker)); } self.as_mut().future().as_mut().set(None); - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => { let future = (self.as_mut().f())(e); self.as_mut().future().set(Some(future)); diff --git a/futures-util/src/stream/for_each_concurrent.rs b/futures-util/src/stream/for_each_concurrent.rs index 6b00bdd2cb..4560fb034b 100644 --- a/futures-util/src/stream/for_each_concurrent.rs +++ b/futures-util/src/stream/for_each_concurrent.rs @@ -3,7 +3,7 @@ use core::pin::Pin; use core::num::NonZeroUsize; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which executes a unit closure over each item on a @@ -60,7 +60,7 @@ impl Future for ForEachConcurrent { type Output = (); - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> { loop { let mut made_progress_this_iter = false; @@ -70,7 +70,7 @@ impl Future for ForEachConcurrent if self.limit.map(|limit| limit.get() > current_len).unwrap_or(true) { let mut stream_completed = false; let elem = if let Some(stream) = self.as_mut().stream().as_pin_mut() { - match stream.poll_next(lw) { + match stream.poll_next(waker) { Poll::Ready(Some(elem)) => { made_progress_this_iter = true; Some(elem) @@ -93,7 +93,7 @@ impl Future for ForEachConcurrent } } - match self.as_mut().futures().poll_next_unpin(lw) { + match self.as_mut().futures().poll_next_unpin(waker) { Poll::Ready(Some(())) => made_progress_this_iter = true, Poll::Ready(None) => { if self.as_mut().stream().is_none() { diff --git a/futures-util/src/stream/forward.rs b/futures-util/src/stream/forward.rs index b0f790431f..0ae2dfe52f 100644 --- a/futures-util/src/stream/forward.rs +++ b/futures-util/src/stream/forward.rs @@ -2,7 +2,7 @@ use crate::stream::{StreamExt, Fuse}; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -45,13 +45,13 @@ where fn try_start_send( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, item: Si::SinkItem, ) -> Poll> { debug_assert!(self.buffered_item.is_none()); { let mut sink = self.as_mut().sink().as_pin_mut().unwrap(); - if try_poll!(sink.as_mut().poll_ready(lw)).is_ready() { + if sink.as_mut().poll_ready(waker)?.is_ready() { return Poll::Ready(sink.start_send(item)); } } @@ -75,27 +75,27 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { // If we've got an item buffered already, we need to write it to the // sink before we can do anything else if let Some(item) = self.as_mut().buffered_item().take() { - try_ready!(self.as_mut().try_start_send(lw, item)); + try_ready!(self.as_mut().try_start_send(waker, item)); } loop { - match self.as_mut().stream().poll_next(lw) { + match self.as_mut().stream().poll_next(waker) { Poll::Ready(Some(Ok(item))) => - try_ready!(self.as_mut().try_start_send(lw, item)), + try_ready!(self.as_mut().try_start_send(waker, item)), Poll::Ready(Some(Err(e))) => return Poll::Ready(Err(e)), Poll::Ready(None) => { try_ready!(self.as_mut().sink().as_pin_mut().expect(INVALID_POLL) - .poll_close(lw)); + .poll_close(waker)); return Poll::Ready(Ok(self.as_mut().sink().take().unwrap())) } Poll::Pending => { try_ready!(self.as_mut().sink().as_pin_mut().expect(INVALID_POLL) - .poll_flush(lw)); + .poll_flush(waker)); return Poll::Pending } } diff --git a/futures-util/src/stream/fuse.rs b/futures-util/src/stream/fuse.rs index 1680eaaa58..d26ffa3528 100644 --- a/futures-util/src/stream/fuse.rs +++ b/futures-util/src/stream/fuse.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; @@ -80,13 +80,13 @@ impl Stream for Fuse { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if *self.as_mut().done() { return Poll::Ready(None); } - let item = ready!(self.as_mut().stream().poll_next(lw)); + let item = ready!(self.as_mut().stream().poll_next(waker)); if item.is_none() { *self.as_mut().done() = true; } diff --git a/futures-util/src/stream/futures_ordered.rs b/futures-util/src/stream/futures_ordered.rs index a2f6912238..443b434337 100644 --- a/futures-util/src/stream/futures_ordered.rs +++ b/futures-util/src/stream/futures_ordered.rs @@ -1,7 +1,7 @@ use crate::stream::FuturesUnordered; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; use std::cmp::{Eq, PartialEq, PartialOrd, Ord, Ordering}; use std::collections::binary_heap::{BinaryHeap, PeekMut}; @@ -48,9 +48,9 @@ impl Future for OrderWrapper fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - self.as_mut().data().as_mut().poll(lw) + self.as_mut().data().as_mut().poll(waker) .map(|output| OrderWrapper { data: output, index: self.index }) } } @@ -171,7 +171,7 @@ impl Stream for FuturesOrdered { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { let this = &mut *self; @@ -184,7 +184,7 @@ impl Stream for FuturesOrdered { } loop { - match Pin::new(&mut this.in_progress_queue).poll_next(lw) { + match Pin::new(&mut this.in_progress_queue).poll_next(waker) { Poll::Ready(Some(output)) => { if output.index == this.next_outgoing_index { this.next_outgoing_index += 1; diff --git a/futures-util/src/stream/futures_unordered/mod.rs b/futures-util/src/stream/futures_unordered/mod.rs index 27b7d132ae..29f891c28b 100644 --- a/futures-util/src/stream/futures_unordered/mod.rs +++ b/futures-util/src/stream/futures_unordered/mod.rs @@ -1,9 +1,9 @@ //! An unbounded set of futures. -use crate::task::AtomicWaker; +use crate::task::{AtomicWaker}; use futures_core::future::{Future, FutureObj, LocalFutureObj}; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll, Spawn, LocalSpawn, SpawnError}; +use futures_core::task::{Waker, Poll, Spawn, LocalSpawn, SpawnError}; use std::cell::UnsafeCell; use std::fmt::{self, Debug}; use std::iter::FromIterator; @@ -287,11 +287,11 @@ impl FuturesUnordered { impl Stream for FuturesUnordered { type Item = Fut::Output; - fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) + fn poll_next(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { // Ensure `parent` is correctly set. - self.ready_to_run_queue.waker.register(lw); + self.ready_to_run_queue.waker.register(waker); loop { // Safety: &mut self guarantees the mutual exclusion `dequeue` @@ -311,7 +311,7 @@ impl Stream for FuturesUnordered { // At this point, it may be worth yielding the thread & // spinning a few times... but for now, just yield using the // task system. - lw.wake(); + waker.wake(); return Poll::Pending; } Dequeue::Data(task) => task, @@ -402,12 +402,12 @@ impl Stream for FuturesUnordered { // the internal allocation, appropriately accessing fields and // deallocating the task if need be. let res = { - let lw = Task::local_waker(bomb.task.as_ref().unwrap()); + let waker = Task::waker_ref(bomb.task.as_ref().unwrap()); // Safety: We won't move the future ever again let future = unsafe { Pin::new_unchecked(future) }; - future.poll(&lw) + future.poll(&waker) }; match res { diff --git a/futures-util/src/stream/futures_unordered/task.rs b/futures-util/src/stream/futures_unordered/task.rs index 8acf331419..069c750e61 100644 --- a/futures-util/src/stream/futures_unordered/task.rs +++ b/futures-util/src/stream/futures_unordered/task.rs @@ -1,14 +1,9 @@ use std::cell::UnsafeCell; -use std::marker::PhantomData; -use std::mem; -use std::ptr::{self, NonNull}; use std::sync::{Arc, Weak}; use std::sync::atomic::{AtomicPtr, AtomicBool}; use std::sync::atomic::Ordering::SeqCst; -use futures_core::task::{UnsafeWake, Waker, LocalWaker}; - -use crate::task::LocalWakerRef; +use crate::task::{ArcWake, WakerRef, waker_ref}; use super::ReadyToRunQueue; use super::abort::abort; @@ -32,9 +27,17 @@ pub(super) struct Task { pub(super) queued: AtomicBool, } -impl Task { - pub(super) fn wake(this: &Arc>) { - let inner = match this.ready_to_run_queue.upgrade() { +// `Task` can be sent across threads safely because it ensures that +// the underlying `Fut` type isn't touched from any of its methods. +// +// The parent (`super`) module is trusted not to access `future` +// across different threads. +unsafe impl Send for Task {} +unsafe impl Sync for Task {} + +impl ArcWake for Task { + fn wake(arc_self: &Arc) { + let inner = match arc_self.ready_to_run_queue.upgrade() { Some(inner) => inner, None => return, }; @@ -51,58 +54,18 @@ impl Task { // implementation guarantees that if we set the `queued` flag that // there's a reference count held by the main `FuturesUnordered` queue // still. - let prev = this.queued.swap(true, SeqCst); + let prev = arc_self.queued.swap(true, SeqCst); if !prev { - inner.enqueue(&**this); + inner.enqueue(&**arc_self); inner.waker.wake(); } } +} - /// Returns a waker. - pub(super) fn waker(this: &Arc>) -> Waker { - let clone = this.clone(); - - // Safety: This is save because an `Arc` is a struct which contains - // a single field that is a pointer. - let ptr = unsafe { - mem::transmute::>, - NonNull>>(clone) - }; - - let ptr = ptr as NonNull; - - // Hide lifetime of `Fut` - // Safety: The waker can safely outlive the future because the - // `UnsafeWake` impl is guaranteed to not touch `Fut`. - let ptr = unsafe { - mem::transmute::, - NonNull>(ptr) - }; - - unsafe { Waker::new(ptr) } - } - - /// Returns a local waker for this task without cloning the Arc. - pub(super) fn local_waker<'a>(this: &'a Arc>) -> LocalWakerRef<'a> { - // Safety: This is safe because an `Arc` is a struct which contains - // a single field that is a pointer. - let ptr = unsafe { - *(this as *const _ as *const NonNull>) - }; - - let ptr = ptr as NonNull; - - // Hide lifetime of `self` - // Safety: - // - Since the `Arc` has not been cloned, the local waker must - // not outlive it. This is ensured by the lifetime of `LocalWakerRef`. - // - The local waker can safely outlive the future because the - // `UnsafeWake` impl is guaranteed to not touch `Fut`. - unsafe { - let ptr = mem::transmute::, - NonNull>(ptr); - LocalWakerRef::new(LocalWaker::new(ptr)) - } +impl Task { + /// Returns a waker reference for this task without cloning the Arc. + pub(super) fn waker_ref<'a>(this: &'a Arc>) -> WakerRef<'a> { + waker_ref(this) } } @@ -123,76 +86,3 @@ impl Drop for Task { } } } - -// `ArcTask` represents conceptually the struct an `Arc>` points -// to. `*const ArcTask` is equal to `Arc>` -// It may only be used through references because its layout obviously doesn't -// match the real inner struct of an `Arc` which (currently) has the form -// `{ strong, weak, data }`. -struct ArcTask(PhantomData); - -struct ArcTaskUnowned(PhantomData); // Doesn't drop the `Arc`'s data - -// We should never touch the future `Fut` on any thread other than the one -// owning `FuturesUnordered`, so this should be a safe operation. -unsafe impl Send for ArcTask {} -unsafe impl Sync for ArcTask {} - -unsafe impl Send for ArcTaskUnowned {} -unsafe impl Sync for ArcTaskUnowned {} - -// We need to implement `UnsafeWake` trait directly and can't implement `Wake` -// for `Task` because `Fut`, the future, isn't required to have a static -// lifetime. `UnsafeWake` lets us forget about `Fut` and its lifetime. This is -// safe because neither `drop_raw` nor `wake` touch `Fut`. This is the case even -// though `drop_raw` runs the destructor for `Task` because its destructor -// is guaranteed to not touch `Fut`. `Fut` must already have been dropped by the -// time it runs. See `Drop` impl for `Task` for more details. -unsafe impl UnsafeWake for ArcTask { - #[inline] - unsafe fn clone_raw(&self) -> Waker { - let me: *const ArcTask = self; - let task = &*(&me as *const *const ArcTask - as *const Arc>); - Task::waker(task) - } - - #[inline] - unsafe fn drop_raw(&self) { - let mut me: *const ArcTask = self; - let task_ptr = &mut me as *mut *const ArcTask - as *mut Arc>; - ptr::drop_in_place(task_ptr); - } - - #[inline] - unsafe fn wake(&self) { - let me: *const ArcTask = self; - let task = &*(&me as *const *const ArcTask - as *const Arc>); - Task::wake(task); - } -} - -unsafe impl UnsafeWake for ArcTaskUnowned { - #[inline] - unsafe fn clone_raw(&self) -> Waker { - let me: *const ArcTaskUnowned = self; - let task = &*(&me as *const *const ArcTaskUnowned - as *const Arc>); - // Clones the `Arc` and the returned waker owns the - // clone. (`ArcTask` not `ArcTaskUnowned`) - Task::waker(task) - } - - #[inline] - unsafe fn drop_raw(&self) {} // Does nothing - - #[inline] - unsafe fn wake(&self) { - let me: *const ArcTaskUnowned = self; - let task = &*(&me as *const *const ArcTaskUnowned - as *const Arc>); - Task::wake(task); - } -} diff --git a/futures-util/src/stream/inspect.rs b/futures-util/src/stream/inspect.rs index 1f1e988cc7..cf6a6b2c74 100644 --- a/futures-util/src/stream/inspect.rs +++ b/futures-util/src/stream/inspect.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Do something with the items of a stream, passing it on. @@ -64,9 +64,9 @@ impl Stream for Inspect fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { - let item = ready!(self.as_mut().stream().poll_next(lw)); + let item = ready!(self.as_mut().stream().poll_next(waker)); Poll::Ready(item.map(|e| { (self.as_mut().f())(&e); e diff --git a/futures-util/src/stream/into_future.rs b/futures-util/src/stream/into_future.rs index 0511f5da55..8de0f3ac7d 100644 --- a/futures-util/src/stream/into_future.rs +++ b/futures-util/src/stream/into_future.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A combinator used to temporarily convert a stream into a future. /// @@ -68,11 +68,11 @@ impl Future for StreamFuture { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll { let item = { let s = self.stream.as_mut().expect("polling StreamFuture twice"); - ready!(Pin::new(s).poll_next(lw)) + ready!(Pin::new(s).poll_next(waker)) }; let stream = self.stream.take().unwrap(); Poll::Ready((item, stream)) diff --git a/futures-util/src/stream/iter.rs b/futures-util/src/stream/iter.rs index d32488c564..6302ba248d 100644 --- a/futures-util/src/stream/iter.rs +++ b/futures-util/src/stream/iter.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A stream which is just a shim over an underlying instance of `Iterator`. /// @@ -39,7 +39,7 @@ impl Stream for Iter { type Item = I::Item; - fn poll_next(mut self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_next(mut self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(self.iter.next()) } } diff --git a/futures-util/src/stream/map.rs b/futures-util/src/stream/map.rs index f1d14aa243..3be82d0b9c 100644 --- a/futures-util/src/stream/map.rs +++ b/futures-util/src/stream/map.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which will change the type of a stream from one @@ -65,9 +65,9 @@ impl Stream for Map fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { - let option = ready!(self.as_mut().stream().poll_next(lw)); + let option = ready!(self.as_mut().stream().poll_next(waker)); Poll::Ready(option.map(self.as_mut().f())) } } diff --git a/futures-util/src/stream/mod.rs b/futures-util/src/stream/mod.rs index 52fab0ed7e..28308246d8 100644 --- a/futures-util/src/stream/mod.rs +++ b/futures-util/src/stream/mod.rs @@ -7,7 +7,7 @@ use core::pin::Pin; use either::Either; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; mod iter; @@ -1046,11 +1046,11 @@ pub trait StreamExt: Stream { /// stream types. fn poll_next_unpin( &mut self, - lw: &LocalWaker + waker: &Waker ) -> Poll> where Self: Unpin + Sized { - Pin::new(self).poll_next(lw) + Pin::new(self).poll_next(waker) } /// Returns a [`Future`] that resolves when the next item in this stream is diff --git a/futures-util/src/stream/next.rs b/futures-util/src/stream/next.rs index f393a35ab4..6bef6efbc0 100644 --- a/futures-util/src/stream/next.rs +++ b/futures-util/src/stream/next.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A future of the next element of a stream. #[derive(Debug)] @@ -29,8 +29,8 @@ impl Future for Next<'_, St> { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - Pin::new(&mut *self.stream).poll_next(lw) + Pin::new(&mut *self.stream).poll_next(waker) } } diff --git a/futures-util/src/stream/once.rs b/futures-util/src/stream/once.rs index 88cb01c3d6..27b5e207c8 100644 --- a/futures-util/src/stream/once.rs +++ b/futures-util/src/stream/once.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Creates a stream of single element @@ -39,10 +39,10 @@ impl Stream for Once { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let val = if let Some(f) = self.as_mut().future().as_pin_mut() { - ready!(f.poll(lw)) + ready!(f.poll(waker)) } else { return Poll::Ready(None) }; diff --git a/futures-util/src/stream/peek.rs b/futures-util/src/stream/peek.rs index 4d0e1de494..2810a7775c 100644 --- a/futures-util/src/stream/peek.rs +++ b/futures-util/src/stream/peek.rs @@ -1,7 +1,7 @@ use crate::stream::{StreamExt, Fuse}; use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A `Stream` that implements a `peek` method. @@ -35,13 +35,13 @@ impl Peekable { /// to the next item if the stream is ready or passes through any errors. pub fn peek<'a>( mut self: Pin<&'a mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if self.peeked.is_some() { let this: &Self = self.into_ref().get_ref(); return Poll::Ready(this.peeked.as_ref()) } - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { None => Poll::Ready(None), Some(item) => { *self.as_mut().peeked() = Some(item); @@ -63,12 +63,12 @@ impl Stream for Peekable { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { if let Some(item) = self.as_mut().peeked().take() { return Poll::Ready(Some(item)) } - self.as_mut().stream().poll_next(lw) + self.as_mut().stream().poll_next(waker) } } diff --git a/futures-util/src/stream/poll_fn.rs b/futures-util/src/stream/poll_fn.rs index e15b2fcaf4..85fcfa6f46 100644 --- a/futures-util/src/stream/poll_fn.rs +++ b/futures-util/src/stream/poll_fn.rs @@ -2,7 +2,7 @@ use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// A stream which adapts a function returning `Poll`. /// @@ -36,18 +36,18 @@ impl Unpin for PollFn {} /// ``` pub fn poll_fn(f: F) -> PollFn where - F: FnMut(&LocalWaker) -> Poll>, + F: FnMut(&Waker) -> Poll>, { PollFn { f } } impl Stream for PollFn where - F: FnMut(&LocalWaker) -> Poll>, + F: FnMut(&Waker) -> Poll>, { type Item = T; - fn poll_next(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - (&mut self.f)(lw) + fn poll_next(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { + (&mut self.f)(waker) } } diff --git a/futures-util/src/stream/repeat.rs b/futures-util/src/stream/repeat.rs index 1cefeaf85a..28361482b1 100644 --- a/futures-util/src/stream/repeat.rs +++ b/futures-util/src/stream/repeat.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// Stream that produces the same element repeatedly. /// @@ -37,7 +37,7 @@ impl Stream for Repeat { type Item = T; - fn poll_next(self: Pin<&mut Self>, _: &LocalWaker) -> Poll> { + fn poll_next(self: Pin<&mut Self>, _: &Waker) -> Poll> { Poll::Ready(Some(self.item.clone())) } } diff --git a/futures-util/src/stream/select.rs b/futures-util/src/stream/select.rs index fe48a9cf7d..eea6619e02 100644 --- a/futures-util/src/stream/select.rs +++ b/futures-util/src/stream/select.rs @@ -1,7 +1,7 @@ use crate::stream::{StreamExt, Fuse}; use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; /// An adapter for merging the output of two streams. /// @@ -49,7 +49,7 @@ impl Stream for Select fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { let Select { flag, stream1, stream2 } = unsafe { Pin::get_unchecked_mut(self) }; @@ -57,9 +57,9 @@ impl Stream for Select let stream2 = unsafe { Pin::new_unchecked(stream2) }; if !*flag { - poll_inner(flag, stream1, stream2, lw) + poll_inner(flag, stream1, stream2, waker) } else { - poll_inner(flag, stream2, stream1, lw) + poll_inner(flag, stream2, stream1, waker) } } } @@ -68,11 +68,11 @@ fn poll_inner( flag: &mut bool, a: Pin<&mut St1>, b: Pin<&mut St2>, - lw: &LocalWaker + waker: &Waker ) -> Poll> where St1: Stream, St2: Stream { - let a_done = match a.poll_next(lw) { + let a_done = match a.poll_next(waker) { Poll::Ready(Some(item)) => { // give the other stream a chance to go first next time *flag = !*flag; @@ -82,7 +82,7 @@ fn poll_inner( Poll::Pending => false, }; - match b.poll_next(lw) { + match b.poll_next(waker) { Poll::Ready(Some(item)) => { Poll::Ready(Some(item)) } diff --git a/futures-util/src/stream/select_all.rs b/futures-util/src/stream/select_all.rs index 2dae183fa4..31039c2011 100644 --- a/futures-util/src/stream/select_all.rs +++ b/futures-util/src/stream/select_all.rs @@ -4,7 +4,7 @@ use std::fmt::{self, Debug}; use std::pin::Pin; use futures_core::{Poll, Stream, FusedStream}; -use futures_core::task::LocalWaker; +use futures_core::task::Waker; use crate::stream::{StreamExt, StreamFuture, FuturesUnordered}; @@ -74,9 +74,9 @@ impl Stream for SelectAll { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - match self.inner.poll_next_unpin(lw) { + match self.inner.poll_next_unpin(waker) { Poll::Pending => Poll::Pending, Poll::Ready(Some((Some(item), remaining))) => { self.push(remaining); @@ -86,7 +86,7 @@ impl Stream for SelectAll { // FuturesUnordered thinks it isn't terminated // because it yielded a Some. Here we poll it // so it can realize it is terminated. - self.inner.poll_next_unpin(lw); + let _ = self.inner.poll_next_unpin(waker); Poll::Ready(None) } Poll::Ready(_) => Poll::Ready(None), diff --git a/futures-util/src/stream/select_next_some.rs b/futures-util/src/stream/select_next_some.rs index 010f3071ac..6de0767fa4 100644 --- a/futures-util/src/stream/select_next_some.rs +++ b/futures-util/src/stream/select_next_some.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::stream::{Stream, FusedStream}; use futures_core::future::{Future, FusedFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use crate::stream::StreamExt; /// A future that resolves to the next value yielded from a [`Stream`]. @@ -26,14 +26,14 @@ impl<'a, St: FusedStream> FusedFuture for SelectNextSome<'a, St> { impl<'a, St: Stream + FusedStream + Unpin> Future for SelectNextSome<'a, St> { type Output = St::Item; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { assert!(!self.stream.is_terminated(), "SelectNextSome polled after terminated"); - if let Some(item) = ready!(self.stream.poll_next_unpin(lw)) { + if let Some(item) = ready!(self.stream.poll_next_unpin(waker)) { Poll::Ready(item) } else { debug_assert!(self.stream.is_terminated()); - lw.wake(); + waker.wake(); Poll::Pending } } diff --git a/futures-util/src/stream/skip.rs b/futures-util/src/stream/skip.rs index edc43db452..b8f9adf0a5 100644 --- a/futures-util/src/stream/skip.rs +++ b/futures-util/src/stream/skip.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which skips a number of elements before continuing. @@ -61,16 +61,16 @@ impl Stream for Skip { fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { while self.remaining > 0 { - match ready!(self.as_mut().stream().poll_next(lw)) { + match ready!(self.as_mut().stream().poll_next(waker)) { Some(_) => *self.as_mut().remaining() -= 1, None => return Poll::Ready(None), } } - self.as_mut().stream().poll_next(lw) + self.as_mut().stream().poll_next(waker) } } diff --git a/futures-util/src/stream/skip_while.rs b/futures-util/src/stream/skip_while.rs index cdb8b5e49a..01e3a0886f 100644 --- a/futures-util/src/stream/skip_while.rs +++ b/futures-util/src/stream/skip_while.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which skips elements of a stream while a predicate @@ -80,15 +80,15 @@ impl Stream for SkipWhile fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if self.done_skipping { - return self.as_mut().stream().poll_next(lw); + return self.as_mut().stream().poll_next(waker); } loop { if self.pending_item.is_none() { - let item = match ready!(self.as_mut().stream().poll_next(lw)) { + let item = match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => e, None => return Poll::Ready(None), }; @@ -97,7 +97,7 @@ impl Stream for SkipWhile *self.as_mut().pending_item() = Some(item); } - let skipped = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(lw)); + let skipped = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(waker)); let item = self.as_mut().pending_item().take().unwrap(); self.as_mut().pending_fut().set(None); diff --git a/futures-util/src/stream/split.rs b/futures-util/src/stream/split.rs index 4955efab29..472b820c03 100644 --- a/futures-util/src/stream/split.rs +++ b/futures-util/src/stream/split.rs @@ -1,5 +1,5 @@ use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; use std::any::Any; use std::error::Error; @@ -27,9 +27,9 @@ impl SplitStream { impl Stream for SplitStream { type Item = S::Item; - fn poll_next(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { - match self.0.poll_lock(lw) { - Poll::Ready(mut inner) => inner.as_pin_mut().poll_next(lw), + fn poll_next(self: Pin<&mut Self>, waker: &Waker) -> Poll> { + match self.0.poll_lock(waker) { + Poll::Ready(mut inner) => inner.as_pin_mut().poll_next(waker), Poll::Pending => Poll::Pending, } } @@ -67,12 +67,12 @@ impl Sink for SplitSink { type SinkItem = S::SinkItem; type SinkError = S::SinkError; - fn poll_ready(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_ready(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { loop { if self.slot.is_none() { return Poll::Ready(Ok(())); } - try_ready!(self.as_mut().poll_flush(lw)); + try_ready!(self.as_mut().poll_flush(waker)); } } @@ -81,33 +81,33 @@ impl Sink for SplitSink { Ok(()) } - fn poll_flush(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_flush(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { let this = &mut *self; - match this.lock.poll_lock(lw) { + match this.lock.poll_lock(waker) { Poll::Ready(mut inner) => { if this.slot.is_some() { - try_ready!(inner.as_pin_mut().poll_ready(lw)); + try_ready!(inner.as_pin_mut().poll_ready(waker)); if let Err(e) = inner.as_pin_mut().start_send(this.slot.take().unwrap()) { return Poll::Ready(Err(e)); } } - inner.as_pin_mut().poll_flush(lw) + inner.as_pin_mut().poll_flush(waker) } Poll::Pending => Poll::Pending, } } - fn poll_close(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll> { + fn poll_close(mut self: Pin<&mut Self>, waker: &Waker) -> Poll> { let this = &mut *self; - match this.lock.poll_lock(lw) { + match this.lock.poll_lock(waker) { Poll::Ready(mut inner) => { if this.slot.is_some() { - try_ready!(inner.as_pin_mut().poll_ready(lw)); + try_ready!(inner.as_pin_mut().poll_ready(waker)); if let Err(e) = inner.as_pin_mut().start_send(this.slot.take().unwrap()) { return Poll::Ready(Err(e)); } } - inner.as_pin_mut().poll_close(lw) + inner.as_pin_mut().poll_close(waker) } Poll::Pending => Poll::Pending, } diff --git a/futures-util/src/stream/take.rs b/futures-util/src/stream/take.rs index 3df6e4d3e5..2355d035cd 100644 --- a/futures-util/src/stream/take.rs +++ b/futures-util/src/stream/take.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which returns a maximum number of elements. @@ -57,12 +57,12 @@ impl Stream for Take fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { if self.remaining == 0 { Poll::Ready(None) } else { - let next = ready!(self.as_mut().stream().poll_next(lw)); + let next = ready!(self.as_mut().stream().poll_next(waker)); match next { Some(_) => *self.as_mut().remaining() -= 1, None => *self.as_mut().remaining() = 0, diff --git a/futures-util/src/stream/take_while.rs b/futures-util/src/stream/take_while.rs index 349e3b415b..21166458c7 100644 --- a/futures-util/src/stream/take_while.rs +++ b/futures-util/src/stream/take_while.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which takes elements from a stream while a predicate @@ -74,14 +74,14 @@ impl Stream for TakeWhile fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if self.done_taking { return Poll::Ready(None); } if self.pending_item.is_none() { - let item = match ready!(self.as_mut().stream().poll_next(lw)) { + let item = match ready!(self.as_mut().stream().poll_next(waker)) { Some(e) => e, None => return Poll::Ready(None), }; @@ -90,7 +90,7 @@ impl Stream for TakeWhile *self.as_mut().pending_item() = Some(item); } - let take = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(lw)); + let take = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().poll(waker)); self.as_mut().pending_fut().set(None); let item = self.as_mut().pending_item().take().unwrap(); diff --git a/futures-util/src/stream/then.rs b/futures-util/src/stream/then.rs index 0bb9b882b6..91161d7a5f 100644 --- a/futures-util/src/stream/then.rs +++ b/futures-util/src/stream/then.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which chains a computation onto each item produced by a @@ -50,10 +50,10 @@ impl Stream for Then fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { if self.as_mut().future().as_pin_mut().is_none() { - let item = match ready!(self.as_mut().stream().poll_next(lw)) { + let item = match ready!(self.as_mut().stream().poll_next(waker)) { None => return Poll::Ready(None), Some(e) => e, }; @@ -61,7 +61,7 @@ impl Stream for Then self.as_mut().future().set(Some(fut)); } - let e = ready!(self.as_mut().future().as_pin_mut().unwrap().poll(lw)); + let e = ready!(self.as_mut().future().as_pin_mut().unwrap().poll(waker)); self.as_mut().future().set(None); Poll::Ready(Some(e)) } diff --git a/futures-util/src/stream/unfold.rs b/futures-util/src/stream/unfold.rs index 0e99de5c97..413ef8b682 100644 --- a/futures-util/src/stream/unfold.rs +++ b/futures-util/src/stream/unfold.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Creates a `Stream` from a seed and a closure returning a `Future`. @@ -91,14 +91,14 @@ impl Stream for Unfold fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { if let Some(state) = self.as_mut().state().take() { let fut = (self.as_mut().f())(state); Pin::set(&mut self.as_mut().fut(), Some(fut)); } - let step = ready!(self.as_mut().fut().as_pin_mut().unwrap().poll(lw)); + let step = ready!(self.as_mut().fut().as_pin_mut().unwrap().poll(waker)); self.as_mut().fut().set(None); if let Some((item, next_state)) = step { diff --git a/futures-util/src/stream/zip.rs b/futures-util/src/stream/zip.rs index ae74911eb4..4307f71bac 100644 --- a/futures-util/src/stream/zip.rs +++ b/futures-util/src/stream/zip.rs @@ -1,7 +1,7 @@ use crate::stream::{StreamExt, Fuse}; use core::pin::Pin; use futures_core::stream::{FusedStream, Stream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// An adapter for merging the output of two streams. @@ -50,16 +50,16 @@ impl Stream for Zip fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker + waker: &Waker ) -> Poll> { if self.queued1.is_none() { - match self.as_mut().stream1().poll_next(lw) { + match self.as_mut().stream1().poll_next(waker) { Poll::Ready(Some(item1)) => *self.as_mut().queued1() = Some(item1), Poll::Ready(None) | Poll::Pending => {} } } if self.as_mut().queued2().is_none() { - match self.as_mut().stream2().poll_next(lw) { + match self.as_mut().stream2().poll_next(waker) { Poll::Ready(Some(item2)) => *self.as_mut().queued2() = Some(item2), Poll::Ready(None) | Poll::Pending => {} } diff --git a/futures-util/src/task/arc_wake.rs b/futures-util/src/task/arc_wake.rs new file mode 100644 index 0000000000..b0b88f98b4 --- /dev/null +++ b/futures-util/src/task/arc_wake.rs @@ -0,0 +1,118 @@ +use std::sync::Arc; +use std::task::{Waker, RawWaker, RawWakerVTable}; + +macro_rules! waker_vtable { + ($ty:ident) => { + &RawWakerVTable { + clone: clone_arc_raw::<$ty>, + drop: drop_arc_raw::<$ty>, + wake: wake_arc_raw::<$ty>, + } + }; +} + +/// A way of waking up a specific task. +/// +/// By implementing this trait, types that are expected to be wrapped in an `Arc` +/// can be converted into `Waker` objects. +/// Those Wakers can be used to signal executors that a task it owns +/// is ready to be `poll`ed again. +pub trait ArcWake { + /// Indicates that the associated task is ready to make progress and should + /// be `poll`ed. + /// + /// This function can be called from an arbitrary thread, including threads which + /// did not create the `ArcWake` based `Waker`. + /// + /// Executors generally maintain a queue of "ready" tasks; `wake` should place + /// the associated task onto this queue. + fn wake(arc_self: &Arc); + + /// Creates a `Waker` from an Arc, if T implements `ArcWake`. + /// + /// If `wake()` is called on the returned `Waker`, + /// the `wake()` function that is defined inside this trait will get called. + fn into_waker(wake: Arc) -> Waker where Self: Sized + { + let ptr = Arc::into_raw(wake) as *const(); + + unsafe { + Waker::new_unchecked(RawWaker::new(ptr, waker_vtable!(Self))) + } + } +} + +unsafe fn increase_refcount(data: *const()) { + // Retain Arc by creating a copy + let arc: Arc = Arc::from_raw(data as *const T); + let arc_clone = arc.clone(); + // Forget the Arcs again, so that the refcount isn't decrased + let _ = Arc::into_raw(arc); + let _ = Arc::into_raw(arc_clone); +} + +unsafe fn clone_arc_raw(data: *const()) -> RawWaker { + increase_refcount::(data); + RawWaker::new(data, waker_vtable!(T)) +} + +unsafe fn drop_arc_raw(data: *const()) { + // Drop Arc + let _: Arc = Arc::from_raw(data as *const T); +} + +unsafe fn wake_arc_raw(data: *const()) { + let arc: Arc = Arc::from_raw(data as *const T); + ArcWake::wake(&arc); // TODO: If this panics, the refcount is too big + let _ = Arc::into_raw(arc); +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::Mutex; + + struct CountingWaker { + nr_wake: Mutex, + } + + impl CountingWaker { + fn new() -> CountingWaker { + CountingWaker { + nr_wake: Mutex::new(0), + } + } + + pub fn wakes(&self) -> i32 { + *self.nr_wake.lock().unwrap() + } + } + + impl ArcWake for CountingWaker { + fn wake(arc_self: &Arc) { + let mut lock = arc_self.nr_wake.lock().unwrap(); + *lock += 1; + } + } + + #[test] + fn create_waker_from_arc() { + let some_w = Arc::new(CountingWaker::new()); + + let w1: Waker = ArcWake::into_waker(some_w.clone()); + assert_eq!(2, Arc::strong_count(&some_w)); + w1.wake(); + assert_eq!(1, some_w.wakes()); + + let w2 = w1.clone(); + assert_eq!(3, Arc::strong_count(&some_w)); + + w2.wake(); + assert_eq!(2, some_w.wakes()); + + drop(w2); + assert_eq!(2, Arc::strong_count(&some_w)); + drop(w1); + assert_eq!(1, Arc::strong_count(&some_w)); + } +} \ No newline at end of file diff --git a/futures-util/src/task/local_waker_ref.rs b/futures-util/src/task/local_waker_ref.rs deleted file mode 100644 index ea0e360d44..0000000000 --- a/futures-util/src/task/local_waker_ref.rs +++ /dev/null @@ -1,140 +0,0 @@ -#![allow(clippy::cast_ptr_alignment)] // clippy is too strict here - -use std::marker::PhantomData; -use std::ops::Deref; -use std::ptr::NonNull; -use std::sync::Arc; -use std::task::{LocalWaker, Waker, Wake, UnsafeWake}; - -/// A [`LocalWaker`](::std::task::LocalWaker) that is only valid for a given lifetime. -/// -/// Note: this type implements [`Deref`](::std::ops::Deref), -/// so it can be used to get a `&LocalWaker`. -#[derive(Debug)] -pub struct LocalWakerRef<'a> { - local_waker: LocalWaker, - _marker: PhantomData<&'a ()>, -} - -impl<'a> LocalWakerRef<'a> { - /// Create a new [`LocalWakerRef`] from a [`LocalWaker`]. - /// - /// Note: this function is safe, but it is generally only used - /// from `unsafe` contexts that need to create a `LocalWaker` - /// that is guaranteed not to outlive a particular lifetime. - pub fn new(local_waker: LocalWaker) -> Self { - LocalWakerRef { - local_waker, - _marker: PhantomData, - } - } -} - -impl<'a> Deref for LocalWakerRef<'a> { - type Target = LocalWaker; - - fn deref(&self) -> &LocalWaker { - &self.local_waker - } -} - -// Pointers to this type below are really pointers to `Arc` -struct ReferencedArc { - _marker: PhantomData, -} - -unsafe impl UnsafeWake for ReferencedArc { - #[inline] - unsafe fn clone_raw(&self) -> Waker { - let me = self as *const ReferencedArc as *const Arc; - Arc::clone(&*me).into() - } - - #[inline] - unsafe fn drop_raw(&self) {} - - #[inline] - unsafe fn wake(&self) { - let me = self as *const ReferencedArc as *const Arc; - W::wake(&*me) - } - - #[inline] - unsafe fn wake_local(&self) { - let me = self as *const ReferencedArc as *const Arc; - W::wake_local(&*me) - } -} - -/// Creates a reference to a [`LocalWaker`](::std::task::LocalWaker) -/// from a local [`wake`](::std::task::Wake). -/// -/// # Safety -/// -/// This function requires that `wake` is "local" (created on the current thread). -/// The resulting [`LocalWaker`](::std::task::LocalWaker) will call -/// [`wake.wake_local()`](::std::task::Wake::wake_local) -/// when awoken, and will call [`wake.wake()`](::std::task::Wake::wake) if -/// awoken after being converted to a [`Waker`](::std::task::Waker). -#[inline] -pub unsafe fn local_waker_ref(wake: &Arc) -> LocalWakerRef<'_> -where - W: Wake + 'static, -{ - let ptr = wake - as *const Arc - as *const ReferencedArc - as *const dyn UnsafeWake - as *mut dyn UnsafeWake; - let local_waker = LocalWaker::new(NonNull::new_unchecked(ptr)); - LocalWakerRef::new(local_waker) -} - -// Pointers to this type below are really pointers to `Arc`, -struct NonlocalReferencedArc { - _marker: PhantomData, -} - -unsafe impl UnsafeWake for NonlocalReferencedArc { - #[inline] - unsafe fn clone_raw(&self) -> Waker { - let me = self as *const NonlocalReferencedArc as *const Arc; - Arc::clone(&*me).into() - } - - #[inline] - unsafe fn drop_raw(&self) {} - - #[inline] - unsafe fn wake(&self) { - let me = self as *const NonlocalReferencedArc as *const Arc; - W::wake(&*me) - } - - #[inline] - unsafe fn wake_local(&self) { - let me = self as *const NonlocalReferencedArc as *const Arc; - W::wake(&*me) - } -} - -/// Creates a reference to a [`LocalWaker`](::std::task::LocalWaker) -/// from a non-local [`wake`](::std::task::Wake). -/// -/// This function is similar to [`local_waker_ref()`], but does not require -/// that `wake` is local to the current thread. The resulting -/// [`LocalWaker`](::std::task::LocalWaker) will call -/// [`wake.wake()`](::std::task::Wake::wake) when awoken. -#[inline] -pub fn local_waker_ref_from_nonlocal(wake: &Arc) -> LocalWakerRef<'_> -where - W: Wake + 'static, -{ - let ptr = wake - as *const Arc - as *const NonlocalReferencedArc - as *const dyn UnsafeWake - as *mut dyn UnsafeWake; - let local_waker = unsafe { LocalWaker::new(NonNull::new_unchecked(ptr)) }; - LocalWakerRef::new(local_waker) -} diff --git a/futures-util/src/task/mod.rs b/futures-util/src/task/mod.rs index 9991a96cde..4867f9cb9b 100644 --- a/futures-util/src/task/mod.rs +++ b/futures-util/src/task/mod.rs @@ -1,15 +1,20 @@ //! Task notification +#[cfg(feature = "std")] +mod arc_wake; +#[cfg(feature = "std")] +pub use self::arc_wake::ArcWake; + mod noop_waker; -pub use self::noop_waker::{noop_local_waker, noop_local_waker_ref}; +pub use self::noop_waker::{noop_waker, noop_waker_ref}; mod spawn; pub use self::spawn::{SpawnExt, LocalSpawnExt}; #[cfg(feature = "std")] -mod local_waker_ref; +mod waker_ref; #[cfg(feature = "std")] -pub use self::local_waker_ref::{local_waker_ref, local_waker_ref_from_nonlocal, LocalWakerRef}; +pub use self::waker_ref::{waker_ref, WakerRef}; #[cfg_attr( feature = "cfg-target-has-atomic", @@ -19,4 +24,4 @@ pub use futures_core::task::__internal::AtomicWaker; // re-export for `select!` #[doc(hidden)] -pub use futures_core::task::{LocalWaker, Poll}; +pub use futures_core::task::{Waker, Poll}; diff --git a/futures-util/src/task/noop_waker.rs b/futures-util/src/task/noop_waker.rs index 704b0d041b..4e720bec62 100644 --- a/futures-util/src/task/noop_waker.rs +++ b/futures-util/src/task/noop_waker.rs @@ -1,62 +1,62 @@ //! Utilities for creating zero-cost wakers that don't do anything. -use futures_core::task::{LocalWaker, UnsafeWake, Waker}; -use core::ptr::NonNull; +use futures_core::task::{RawWaker, RawWakerVTable, Waker}; +use core::ptr::null; +use core::cell::UnsafeCell; -#[derive(Debug)] -struct NoopWake { - _reserved: (), +unsafe fn noop_clone(_data: *const()) -> RawWaker { + noop_raw_waker() } -unsafe impl UnsafeWake for NoopWake { - unsafe fn clone_raw(&self) -> Waker { - noop_waker() - } - - unsafe fn drop_raw(&self) {} - - unsafe fn wake(&self) {} +unsafe fn noop(_data: *const()) { } -fn noop_unsafe_wake() -> NonNull { - static mut INSTANCE: NoopWake = NoopWake { _reserved: () }; - unsafe { NonNull::new_unchecked(&mut INSTANCE as *mut dyn UnsafeWake) } -} +const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable { + clone: noop_clone, + drop: noop, + wake: noop, +}; -fn noop_waker() -> Waker { - unsafe { Waker::new(noop_unsafe_wake()) } +fn noop_raw_waker() -> RawWaker { + RawWaker::new(null(), &NOOP_WAKER_VTABLE) } -/// Create a new [`LocalWaker`](futures_core::task::LocalWaker) referencing a -/// singleton instance of [`NoopWake`]. +/// Create a new [`Waker`](futures_core::task::Waker) which does +/// nothing when `wake()` is called on it. The [`Waker`] can be converted +/// into a [`Waker`] which will behave the same way. +/// +/// # Examples +/// +/// ``` +/// #![feature(futures_api)] +/// use futures::task::noop_waker; +/// let lw = noop_waker(); +/// lw.wake(); +/// ``` #[inline] -pub fn noop_local_waker() -> LocalWaker { - unsafe { LocalWaker::new(noop_unsafe_wake()) } +pub fn noop_waker() -> Waker { + unsafe { + Waker::new_unchecked(noop_raw_waker()) + } } /// Get a thread local reference to a -/// [`LocalWaker`](futures_core::task::LocalWaker) referencing a singleton -/// instance of [`NoopWake`]. +/// [`Waker`](futures_core::task::Waker) referencing a singleton +/// instance of a [`Waker`] which panics when woken. /// /// # Examples /// /// ``` /// #![feature(futures_api)] -/// use futures::task::noop_local_waker_ref; -/// let lw = noop_local_waker_ref(); +/// use futures::task::noop_waker_ref; +/// let lw = noop_waker_ref(); /// lw.wake(); /// ``` #[inline] -pub fn noop_local_waker_ref() -> &'static LocalWaker { - static NOOP_WAKE_REF: &(dyn UnsafeWake + Sync) = &NoopWake { _reserved: () }; - // Unsafety: `Waker` and `LocalWaker` are `repr(transparent)` wrappers around - // `NonNull`, which has the same repr as `&(dyn UnsafeWake + Sync)` - // So an &'static &(dyn UnsafeWake + Sync) can be unsafely cast to a - // &'static LocalWaker - #[allow(clippy::transmute_ptr_to_ptr)] - unsafe { - core::mem::transmute::< - &&(dyn UnsafeWake + Sync), - &'static LocalWaker, - >(&NOOP_WAKE_REF) +pub fn noop_waker_ref() -> &'static Waker { + thread_local! { + static NOOP_WAKER_INSTANCE: UnsafeCell = + UnsafeCell::new(noop_waker()); } + NOOP_WAKER_INSTANCE.with(|l| unsafe { &*l.get() }) } + diff --git a/futures-util/src/task/waker_ref.rs b/futures-util/src/task/waker_ref.rs new file mode 100644 index 0000000000..1b39b3e66c --- /dev/null +++ b/futures-util/src/task/waker_ref.rs @@ -0,0 +1,110 @@ +#![allow(clippy::cast_ptr_alignment)] // clippy is too strict here + +use std::marker::PhantomData; +use std::ops::Deref; +use std::sync::Arc; +use std::task::{Waker, RawWaker, RawWakerVTable}; +use super::ArcWake; + +/// A [`Waker`](::std::task::Waker) that is only valid for a given lifetime. +/// +/// Note: this type implements [`Deref`](::std::ops::Deref), +/// so it can be used to get a `&Waker`. +#[derive(Debug)] +pub struct WakerRef<'a> { + waker: Waker, + _marker: PhantomData<&'a ()>, +} + +impl<'a> WakerRef<'a> { + /// Create a new [`WakerRef`] from a [`Waker`]. + /// + /// Note: this function is safe, but it is generally only used + /// from `unsafe` contexts that need to create a `Waker` + /// that is guaranteed not to outlive a particular lifetime. + pub fn new(waker: Waker) -> Self { + WakerRef { + waker, + _marker: PhantomData, + } + } +} + +impl<'a> Deref for WakerRef<'a> { + type Target = Waker; + + fn deref(&self) -> &Waker { + &self.waker + } +} + +// Another reference vtable which doesn't do decrement the refcount on drop. +// However on clone it will create a vtable which equals a Waker, and on wake +// it will call the nonlocal wake function. +macro_rules! ref_vtable { + ($ty:ident) => { + &RawWakerVTable { + clone: clone_arc_raw::<$ty>, + drop: noop, + wake: wake_arc_raw::<$ty>, + } + }; +} + +macro_rules! owned_vtable { + ($ty:ident) => { + &RawWakerVTable { + clone: clone_arc_raw::<$ty>, + drop: drop_arc_raw::<$ty>, + wake: wake_arc_raw::<$ty>, + } + }; +} + +/// Creates a reference to a [`Waker`](::std::task::Waker) +/// from a local [`wake`](::std::task::Wake). +/// +/// The resulting [`Waker`](::std::task::Waker) will call +/// [`wake.wake()`](::std::task::Wake::wake) if awoken. +#[inline] +pub fn waker_ref(wake: &Arc) -> WakerRef<'_> +where + W: ArcWake +{ + // This uses the same mechanism as Arc::into_raw, without needing a reference. + // This is potentially not stable + let ptr = &*wake as &W as *const W as *const(); + + let waker = unsafe { + Waker::new_unchecked(RawWaker::new(ptr, ref_vtable!(W))) + }; + WakerRef::new(waker) +} + +unsafe fn noop(_data: *const()) { +} + +unsafe fn increase_refcount(data: *const()) { + // Retain Arc by creating a copy + let arc: Arc = Arc::from_raw(data as *const T); + let arc_clone = arc.clone(); + // Forget the Arcs again, so that the refcount isn't decrased + let _ = Arc::into_raw(arc); + let _ = Arc::into_raw(arc_clone); +} + +unsafe fn clone_arc_raw(data: *const()) -> RawWaker { + increase_refcount::(data); + RawWaker::new(data, owned_vtable!(T)) +} + +unsafe fn drop_arc_raw(data: *const()) { + // Drop Arc + let _: Arc = Arc::from_raw(data as *const T); +} + +unsafe fn wake_arc_raw(data: *const()) { + let arc: Arc = Arc::from_raw(data as *const T); + ArcWake::wake(&arc); // TODO: If this panics, the refcount is too big + let _ = Arc::into_raw(arc); +} \ No newline at end of file diff --git a/futures-util/src/try_future/and_then.rs b/futures-util/src/try_future/and_then.rs index b6a2ab46f4..01ee812446 100644 --- a/futures-util/src/try_future/and_then.rs +++ b/futures-util/src/try_future/and_then.rs @@ -1,7 +1,7 @@ use super::{TryChain, TryChainAction}; use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the [`and_then`](super::TryFutureExt::and_then) combinator. @@ -42,8 +42,8 @@ impl Future for AndThen { type Output = Result; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.try_chain().poll(lw, |result, async_op| { + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.try_chain().poll(waker, |result, async_op| { match result { Ok(ok) => TryChainAction::Future(async_op(ok)), Err(err) => TryChainAction::Output(Err(err)), diff --git a/futures-util/src/try_future/err_into.rs b/futures-util/src/try_future/err_into.rs index 5865d6b552..b6d5a9250b 100644 --- a/futures-util/src/try_future/err_into.rs +++ b/futures-util/src/try_future/err_into.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the [`err_into`](super::TryFutureExt::err_into) combinator. @@ -37,9 +37,9 @@ impl Future for ErrInto fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - self.future().try_poll(lw) + self.future().try_poll(waker) .map(|res| res.map_err(Into::into)) } } diff --git a/futures-util/src/try_future/flatten_sink.rs b/futures-util/src/try_future/flatten_sink.rs index 6538e40571..7c207fdb60 100644 --- a/futures-util/src/try_future/flatten_sink.rs +++ b/futures-util/src/try_future/flatten_sink.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::TryFuture; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_sink::Sink; #[derive(Debug)] @@ -51,16 +51,16 @@ where fn poll_ready( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let resolved_stream = match self.as_mut().project_pin() { - Ready(s) => return s.poll_ready(lw), - Waiting(f) => try_ready!(f.try_poll(lw)), + Ready(s) => return s.poll_ready(waker), + Waiting(f) => try_ready!(f.try_poll(waker)), Closed => panic!("poll_ready called after eof"), }; Pin::set(&mut self.as_mut(), FlattenSink(Ready(resolved_stream))); if let Ready(resolved_stream) = self.project_pin() { - resolved_stream.poll_ready(lw) + resolved_stream.poll_ready(waker) } else { unreachable!() } @@ -79,10 +79,10 @@ where fn poll_flush( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { match self.project_pin() { - Ready(s) => s.poll_flush(lw), + Ready(s) => s.poll_flush(waker), // if sink not yet resolved, nothing written ==> everything flushed Waiting(_) => Poll::Ready(Ok(())), Closed => panic!("poll_flush called after eof"), @@ -91,10 +91,10 @@ where fn poll_close( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { let res = match self.as_mut().project_pin() { - Ready(s) => s.poll_close(lw), + Ready(s) => s.poll_close(waker), Waiting(_) | Closed => Poll::Ready(Ok(())), }; if res.is_ready() { diff --git a/futures-util/src/try_future/into_future.rs b/futures-util/src/try_future/into_future.rs index ee13012b03..aa7e20c2f4 100644 --- a/futures-util/src/try_future/into_future.rs +++ b/futures-util/src/try_future/into_future.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the [`into_future`](super::TryFutureExt::into_future) combinator. @@ -29,8 +29,8 @@ impl Future for IntoFuture { #[inline] fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - self.future().try_poll(lw) + self.future().try_poll(waker) } } diff --git a/futures-util/src/try_future/map_err.rs b/futures-util/src/try_future/map_err.rs index 1394ec60a4..00e06e6ef0 100644 --- a/futures-util/src/try_future/map_err.rs +++ b/futures-util/src/try_future/map_err.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Future for the [`map_err`](super::TryFutureExt::map_err) combinator. @@ -35,9 +35,9 @@ impl Future for MapErr fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - match self.as_mut().future().try_poll(lw) { + match self.as_mut().future().try_poll(waker) { Poll::Pending => Poll::Pending, Poll::Ready(result) => { let f = self.as_mut().f().take() diff --git a/futures-util/src/try_future/map_ok.rs b/futures-util/src/try_future/map_ok.rs index d0aa4006b2..daf77a4617 100644 --- a/futures-util/src/try_future/map_ok.rs +++ b/futures-util/src/try_future/map_ok.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Future for the [`map_ok`](super::TryFutureExt::map_ok) combinator. @@ -37,9 +37,9 @@ impl Future for MapOk fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - match self.as_mut().future().try_poll(lw) { + match self.as_mut().future().try_poll(waker) { Poll::Pending => Poll::Pending, Poll::Ready(result) => { let op = self.as_mut().f().take() diff --git a/futures-util/src/try_future/or_else.rs b/futures-util/src/try_future/or_else.rs index 713637c82e..7a76609d49 100644 --- a/futures-util/src/try_future/or_else.rs +++ b/futures-util/src/try_future/or_else.rs @@ -1,7 +1,7 @@ use super::{TryChain, TryChainAction}; use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Future for the [`or_else`](super::TryFutureExt::or_else) combinator. @@ -44,9 +44,9 @@ impl Future for OrElse fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - self.try_chain().poll(lw, |result, async_op| { + self.try_chain().poll(waker, |result, async_op| { match result { Ok(ok) => TryChainAction::Output(Ok(ok)), Err(err) => TryChainAction::Future(async_op(err)), diff --git a/futures-util/src/try_future/try_chain.rs b/futures-util/src/try_future/try_chain.rs index f545df90c6..2a2356cef7 100644 --- a/futures-util/src/try_future/try_chain.rs +++ b/futures-util/src/try_future/try_chain.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::TryFuture; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; #[must_use = "futures do nothing unless polled"] #[derive(Debug)] @@ -34,7 +34,7 @@ impl TryChain pub(crate) fn poll( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, f: F, ) -> Poll> where F: FnOnce(Result, Data) -> TryChainAction, @@ -48,14 +48,14 @@ impl TryChain let (output, data) = match this { TryChain::First(fut1, data) => { // Poll the first future - match unsafe { Pin::new_unchecked(fut1) }.try_poll(lw) { + match unsafe { Pin::new_unchecked(fut1) }.try_poll(waker) { Poll::Pending => return Poll::Pending, Poll::Ready(output) => (output, data.take().unwrap()), } } TryChain::Second(fut2) => { // Poll the second future - return unsafe { Pin::new_unchecked(fut2) }.try_poll(lw) + return unsafe { Pin::new_unchecked(fut2) }.try_poll(waker) } TryChain::Empty => { panic!("future must not be polled after it returned `Poll::Ready`"); diff --git a/futures-util/src/try_future/try_join.rs b/futures-util/src/try_future/try_join.rs index 8d6d426e37..26c4fe5134 100644 --- a/futures-util/src/try_future/try_join.rs +++ b/futures-util/src/try_future/try_join.rs @@ -5,7 +5,7 @@ use crate::try_future::{TryFutureExt, IntoFuture}; use core::fmt; use core::pin::Pin; use futures_core::future::{Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; macro_rules! generate { @@ -70,17 +70,17 @@ macro_rules! generate { #[allow(clippy::useless_let_if_seq)] fn poll( - mut self: Pin<&mut Self>, lw: &LocalWaker + mut self: Pin<&mut Self>, waker: &Waker ) -> Poll { let mut all_done = true; - if self.as_mut().Fut1().poll(lw).is_pending() { + if self.as_mut().Fut1().poll(waker).is_pending() { all_done = false; } else if self.as_mut().Fut1().output_mut().unwrap().is_err() { return Poll::Ready(Err( self.as_mut().Fut1().take_output().unwrap().err().unwrap())); } $( - if self.as_mut().$Fut().poll(lw).is_pending() { + if self.as_mut().$Fut().poll(waker).is_pending() { all_done = false; } else if self.as_mut().$Fut().output_mut().unwrap().is_err() { return Poll::Ready(Err( diff --git a/futures-util/src/try_future/try_join_all.rs b/futures-util/src/try_future/try_join_all.rs index 3610cf35da..f26625c9cb 100644 --- a/futures-util/src/try_future/try_join_all.rs +++ b/futures-util/src/try_future/try_join_all.rs @@ -139,13 +139,13 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &::std::task::LocalWaker, + waker: &::std::task::Waker, ) -> Poll { let mut state = FinalState::AllDone; for mut elem in iter_pin_mut(self.elems.as_mut()) { if let Some(pending) = elem.as_mut().pending_pin_mut() { - match pending.try_poll(lw) { + match pending.try_poll(waker) { Poll::Pending => state = FinalState::Pending, Poll::Ready(output) => match output { Ok(item) => elem.set(ElemState::Done(Some(item))), diff --git a/futures-util/src/try_future/unwrap_or_else.rs b/futures-util/src/try_future/unwrap_or_else.rs index 542070a6df..94fe3553ec 100644 --- a/futures-util/src/try_future/unwrap_or_else.rs +++ b/futures-util/src/try_future/unwrap_or_else.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Future for the [`unwrap_or_else`](super::TryFutureExt::unwrap_or_else) @@ -38,9 +38,9 @@ impl Future for UnwrapOrElse fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - match self.as_mut().future().try_poll(lw) { + match self.as_mut().future().try_poll(waker) { Poll::Pending => Poll::Pending, Poll::Ready(result) => { let op = self.as_mut().f().take() diff --git a/futures-util/src/try_stream/err_into.rs b/futures-util/src/try_stream/err_into.rs index ccbe0315b5..c39cb05f4c 100644 --- a/futures-util/src/try_stream/err_into.rs +++ b/futures-util/src/try_stream/err_into.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use core::pin::Pin; use futures_core::stream::{FusedStream, Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Stream for the [`err_into`](super::TryStreamExt::err_into) combinator. @@ -37,9 +37,9 @@ where fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.stream().try_poll_next(lw) + self.stream().try_poll_next(waker) .map(|res| res.map(|some| some.map_err(Into::into))) } } diff --git a/futures-util/src/try_stream/into_async_read.rs b/futures-util/src/try_stream/into_async_read.rs index 2792142411..627245dc09 100644 --- a/futures-util/src/try_stream/into_async_read.rs +++ b/futures-util/src/try_stream/into_async_read.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use futures_io::AsyncRead; use std::cmp; use std::io::{Error, Result}; @@ -51,7 +51,7 @@ where { fn poll_read( &mut self, - lw: &LocalWaker, + waker: &Waker, buf: &mut [u8], ) -> Poll> { loop { @@ -72,7 +72,7 @@ where return Poll::Ready(Ok(len)); } ReadState::PendingChunk => { - match ready!(Pin::new(&mut self.stream).try_poll_next(lw)) { + match ready!(Pin::new(&mut self.stream).try_poll_next(waker)) { Some(Ok(chunk)) => { self.state = ReadState::Ready { chunk, @@ -103,12 +103,12 @@ mod tests { use super::*; use futures::stream::{self, StreamExt, TryStreamExt}; use futures_io::AsyncRead; - use futures_test::task::noop_local_waker_ref; + use futures_test::task::noop_waker_ref; macro_rules! assert_read { ($reader:expr, $buf:expr, $item:expr) => { - let lw = noop_local_waker_ref(); - match $reader.poll_read(lw, $buf) { + let waker = noop_waker_ref(); + match $reader.poll_read(waker, $buf) { Poll::Ready(Ok(x)) => { assert_eq!(x, $item); } diff --git a/futures-util/src/try_stream/into_stream.rs b/futures-util/src/try_stream/into_stream.rs index cfd4cda63c..8fdb01d526 100644 --- a/futures-util/src/try_stream/into_stream.rs +++ b/futures-util/src/try_stream/into_stream.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::unsafe_pinned; /// Stream for the [`into_stream`](super::TryStreamExt::into_stream) combinator. @@ -48,8 +48,8 @@ impl Stream for IntoStream { #[inline] fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.stream().try_poll_next(lw) + self.stream().try_poll_next(waker) } } diff --git a/futures-util/src/try_stream/map_err.rs b/futures-util/src/try_stream/map_err.rs index 8e85a03966..aac4cd61ef 100644 --- a/futures-util/src/try_stream/map_err.rs +++ b/futures-util/src/try_stream/map_err.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Stream for the [`map_err`](super::TryStreamExt::map_err) combinator. @@ -39,9 +39,9 @@ where #[allow(clippy::redundant_closure)] // https://github.com/rust-lang-nursery/rust-clippy/issues/1439 fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - match self.as_mut().stream().try_poll_next(lw) { + match self.as_mut().stream().try_poll_next(waker) { Poll::Pending => Poll::Pending, Poll::Ready(opt) => Poll::Ready(opt.map(|res| res.map_err(|e| self.as_mut().f()(e)))), diff --git a/futures-util/src/try_stream/map_ok.rs b/futures-util/src/try_stream/map_ok.rs index b8c3e2b700..637ef50645 100644 --- a/futures-util/src/try_stream/map_ok.rs +++ b/futures-util/src/try_stream/map_ok.rs @@ -1,6 +1,6 @@ use core::pin::Pin; use futures_core::stream::{FusedStream, Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// Stream for the [`map_ok`](super::TryStreamExt::map_ok) combinator. @@ -39,9 +39,9 @@ where #[allow(clippy::redundant_closure)] // https://github.com/rust-lang-nursery/rust-clippy/issues/1439 fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - match self.as_mut().stream().try_poll_next(lw) { + match self.as_mut().stream().try_poll_next(waker) { Poll::Pending => Poll::Pending, Poll::Ready(opt) => Poll::Ready(opt.map(|res| res.map(|x| self.as_mut().f()(x)))), diff --git a/futures-util/src/try_stream/mod.rs b/futures-util/src/try_stream/mod.rs index 2bfd013e87..2dc47fa47c 100644 --- a/futures-util/src/try_stream/mod.rs +++ b/futures-util/src/try_stream/mod.rs @@ -6,7 +6,7 @@ use core::pin::Pin; use futures_core::future::TryFuture; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; #[cfg(feature = "compat")] use crate::compat::Compat; @@ -560,11 +560,11 @@ pub trait TryStreamExt: TryStream { /// stream types. fn try_poll_next_unpin( &mut self, - lw: &LocalWaker + waker: &Waker ) -> Poll>> where Self: Unpin, { - Pin::new(self).try_poll_next(lw) + Pin::new(self).try_poll_next(waker) } /// Wraps a [`TryStream`] into a stream compatible with libraries using diff --git a/futures-util/src/try_stream/try_buffer_unordered.rs b/futures-util/src/try_stream/try_buffer_unordered.rs index 4a745481f3..6b1f3e5bfa 100644 --- a/futures-util/src/try_stream/try_buffer_unordered.rs +++ b/futures-util/src/try_stream/try_buffer_unordered.rs @@ -3,7 +3,7 @@ use crate::try_future::{IntoFuture, TryFutureExt}; use crate::try_stream::IntoStream; use futures_core::future::TryFuture; use futures_core::stream::{Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::pin::Pin; @@ -70,12 +70,12 @@ impl Stream for TryBufferUnordered fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { // First up, try to spawn off as many futures as possible by filling up // our slab of futures. Propagate errors from the stream immediately. while self.in_progress_queue.len() < self.max { - match self.as_mut().stream().poll_next(lw) { + match self.as_mut().stream().poll_next(waker) { Poll::Ready(Some(Ok(fut))) => self.as_mut().in_progress_queue().push(fut.into_future()), Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))), Poll::Ready(None) | Poll::Pending => break, @@ -83,7 +83,7 @@ impl Stream for TryBufferUnordered } // Attempt to pull the next value from the in_progress_queue - match Pin::new(self.as_mut().in_progress_queue()).poll_next(lw) { + match Pin::new(self.as_mut().in_progress_queue()).poll_next(waker) { x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x, Poll::Ready(None) => {} } diff --git a/futures-util/src/try_stream/try_collect.rs b/futures-util/src/try_stream/try_collect.rs index a78743f18b..7b9f273b6b 100644 --- a/futures-util/src/try_stream/try_collect.rs +++ b/futures-util/src/try_stream/try_collect.rs @@ -1,6 +1,6 @@ use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; use std::mem; use std::pin::Pin; @@ -47,10 +47,10 @@ impl Future for TryCollect fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { loop { - match ready!(self.as_mut().stream().try_poll_next(lw)) { + match ready!(self.as_mut().stream().try_poll_next(waker)) { Some(Ok(x)) => self.as_mut().items().extend(Some(x)), Some(Err(e)) => return Poll::Ready(Err(e)), None => return Poll::Ready(Ok(self.as_mut().finish())), diff --git a/futures-util/src/try_stream/try_concat.rs b/futures-util/src/try_stream/try_concat.rs index 60f1d0a645..3dbdee63ca 100644 --- a/futures-util/src/try_stream/try_concat.rs +++ b/futures-util/src/try_stream/try_concat.rs @@ -2,7 +2,7 @@ use core::default::Default; use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which attempts to concatenate the results of a stream into the @@ -41,9 +41,9 @@ where { type Output = Result; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { - match ready!(self.as_mut().stream().try_poll_next(lw)) { + match ready!(self.as_mut().stream().try_poll_next(waker)) { Some(Ok(x)) => { let accum = self.as_mut().accum(); if let Some(a) = accum { diff --git a/futures-util/src/try_stream/try_filter_map.rs b/futures-util/src/try_stream/try_filter_map.rs index f4d752d6fe..8e40a0fe23 100644 --- a/futures-util/src/try_stream/try_filter_map.rs +++ b/futures-util/src/try_stream/try_filter_map.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{TryFuture}; use futures_core::stream::{Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A combinator that attempts to filter the results of a stream @@ -63,11 +63,11 @@ impl Stream for TryFilterMap fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll>> { loop { if self.pending.is_none() { - let item = match ready!(self.as_mut().stream().try_poll_next(lw)) { + let item = match ready!(self.as_mut().stream().try_poll_next(waker)) { Some(Ok(x)) => x, Some(Err(e)) => return Poll::Ready(Some(Err(e))), None => return Poll::Ready(None), @@ -76,7 +76,7 @@ impl Stream for TryFilterMap self.as_mut().pending().set(Some(fut)); } - let result = ready!(self.as_mut().pending().as_pin_mut().unwrap().try_poll(lw)); + let result = ready!(self.as_mut().pending().as_pin_mut().unwrap().try_poll(waker)); self.as_mut().pending().set(None); match result { Ok(Some(x)) => return Poll::Ready(Some(Ok(x))), diff --git a/futures-util/src/try_stream/try_fold.rs b/futures-util/src/try_stream/try_fold.rs index 5552ecdd4e..b21580899e 100644 --- a/futures-util/src/try_stream/try_fold.rs +++ b/futures-util/src/try_stream/try_fold.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// The future for the `TryStream::fold` method. @@ -49,14 +49,14 @@ impl Future for TryFold { type Output = Result; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { // we're currently processing a future to produce a new accum value if self.as_mut().accum().is_none() { let accum = match ready!( self.as_mut().future().as_pin_mut() .expect("TryFold polled after completion") - .try_poll(lw) + .try_poll(waker) ) { Ok(accum) => accum, Err(e) => { @@ -69,7 +69,7 @@ impl Future for TryFold self.as_mut().future().set(None); } - let item = match ready!(self.as_mut().stream().try_poll_next(lw)) { + let item = match ready!(self.as_mut().stream().try_poll_next(waker)) { Some(Ok(item)) => Some(item), Some(Err(e)) => { // Indicate that the future can no longer be polled. diff --git a/futures-util/src/try_stream/try_for_each.rs b/futures-util/src/try_stream/try_for_each.rs index ee300d7bfd..abbfc3fff7 100644 --- a/futures-util/src/try_stream/try_for_each.rs +++ b/futures-util/src/try_stream/try_for_each.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::{Future, TryFuture}; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which attempts to execute an async closure over each @@ -43,14 +43,14 @@ impl Future for TryForEach { type Output = Result<(), St::Error>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { if let Some(future) = self.as_mut().future().as_pin_mut() { - try_ready!(future.try_poll(lw)); + try_ready!(future.try_poll(waker)); } Pin::set(&mut self.as_mut().future(), None); - match ready!(self.as_mut().stream().try_poll_next(lw)) { + match ready!(self.as_mut().stream().try_poll_next(waker)) { Some(Ok(e)) => { let future = (self.as_mut().f())(e); Pin::set(&mut self.as_mut().future(), Some(future)); diff --git a/futures-util/src/try_stream/try_for_each_concurrent.rs b/futures-util/src/try_stream/try_for_each_concurrent.rs index 32d0d96d72..532fcf46ca 100644 --- a/futures-util/src/try_stream/try_for_each_concurrent.rs +++ b/futures-util/src/try_stream/try_for_each_concurrent.rs @@ -4,7 +4,7 @@ use core::pin::Pin; use core::num::NonZeroUsize; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::TryStream; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which executes a unit closure over each item on a @@ -61,7 +61,7 @@ impl Future for TryForEachConcurrent { type Output = Result<(), St::Error>; - fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll { loop { let mut made_progress_this_iter = false; @@ -70,7 +70,7 @@ impl Future for TryForEachConcurrent // Check if we've already created a number of futures greater than `limit` if self.limit.map(|limit| limit.get() > current_len).unwrap_or(true) { let poll_res = match self.as_mut().stream().as_pin_mut() { - Some(stream) => stream.try_poll_next(lw), + Some(stream) => stream.try_poll_next(waker), None => Poll::Ready(None), }; @@ -99,7 +99,7 @@ impl Future for TryForEachConcurrent } } - match self.as_mut().futures().poll_next_unpin(lw) { + match self.as_mut().futures().poll_next_unpin(waker) { Poll::Ready(Some(Ok(()))) => made_progress_this_iter = true, Poll::Ready(None) => { if self.as_mut().stream().is_none() { diff --git a/futures-util/src/try_stream/try_next.rs b/futures-util/src/try_stream/try_next.rs index e45fd17547..136ef096e9 100644 --- a/futures-util/src/try_stream/try_next.rs +++ b/futures-util/src/try_stream/try_next.rs @@ -1,6 +1,6 @@ use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use core::pin::Pin; /// A future which attempts to collect all of the values of a stream. @@ -31,9 +31,9 @@ impl Future for TryNext<'_, St> { fn poll( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll { - match Pin::new(&mut *self.stream).try_poll_next(lw) { + match Pin::new(&mut *self.stream).try_poll_next(waker) { Poll::Ready(Some(Ok(x))) => Poll::Ready(Ok(Some(x))), Poll::Ready(Some(Err(e))) => Poll::Ready(Err(e)), Poll::Ready(None) => Poll::Ready(Ok(None)), diff --git a/futures-util/src/try_stream/try_skip_while.rs b/futures-util/src/try_stream/try_skip_while.rs index 3e85be78bb..9f42fa22c9 100644 --- a/futures-util/src/try_stream/try_skip_while.rs +++ b/futures-util/src/try_stream/try_skip_while.rs @@ -1,7 +1,7 @@ use core::pin::Pin; use futures_core::future::TryFuture; use futures_core::stream::{Stream, TryStream}; -use futures_core::task::{LocalWaker, Poll}; +use futures_core::task::{Waker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// A stream combinator which skips elements of a stream while a predicate @@ -76,15 +76,15 @@ impl Stream for TrySkipWhile fn poll_next( mut self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { if self.done_skipping { - return self.as_mut().stream().try_poll_next(lw); + return self.as_mut().stream().try_poll_next(waker); } loop { if self.pending_item.is_none() { - let item = match ready!(self.as_mut().stream().try_poll_next(lw)?) { + let item = match ready!(self.as_mut().stream().try_poll_next(waker)?) { Some(e) => e, None => return Poll::Ready(None), }; @@ -93,7 +93,7 @@ impl Stream for TrySkipWhile *self.as_mut().pending_item() = Some(item); } - let skipped = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().try_poll(lw)?); + let skipped = ready!(self.as_mut().pending_fut().as_pin_mut().unwrap().try_poll(waker)?); let item = self.as_mut().pending_item().take().unwrap(); self.as_mut().pending_fut().set(None); diff --git a/futures-util/tests/futures_unordered.rs b/futures-util/tests/futures_unordered.rs index 1ea1fab6b7..867b13b69e 100644 --- a/futures-util/tests/futures_unordered.rs +++ b/futures-util/tests/futures_unordered.rs @@ -3,11 +3,11 @@ use futures::future; use futures::task::Poll; use futures::stream::{FusedStream, FuturesUnordered, StreamExt}; -use futures_test::task::noop_local_waker_ref; +use futures_test::task::noop_waker_ref; #[test] fn is_terminated() { - let lw = noop_local_waker_ref(); + let lw = noop_waker_ref(); let mut tasks = FuturesUnordered::new(); assert_eq!(tasks.is_terminated(), false); diff --git a/futures-util/tests/mutex.rs b/futures-util/tests/mutex.rs index 9a578fafc7..8a332d6d75 100644 --- a/futures-util/tests/mutex.rs +++ b/futures-util/tests/mutex.rs @@ -6,14 +6,14 @@ use futures::lock::Mutex; use futures::stream::StreamExt; use futures::task::SpawnExt; use futures_test::future::FutureTestExt; -use futures_test::task::{panic_local_waker_ref, new_count_waker}; +use futures_test::task::{panic_waker_ref, new_count_waker}; use std::sync::Arc; #[test] fn mutex_acquire_uncontested() { let mutex = Mutex::new(()); for _ in 0..10 { - assert!(mutex.lock().poll_unpin(panic_local_waker_ref()).is_ready()); + assert!(mutex.lock().poll_unpin(panic_waker_ref()).is_ready()); } } @@ -21,7 +21,7 @@ fn mutex_acquire_uncontested() { fn mutex_wakes_waiters() { let mutex = Mutex::new(()); let (lw, counter) = new_count_waker(); - let lock = mutex.lock().poll_unpin(panic_local_waker_ref()); + let lock = mutex.lock().poll_unpin(panic_waker_ref()); assert!(lock.is_ready()); let mut waiter = mutex.lock(); @@ -31,7 +31,7 @@ fn mutex_wakes_waiters() { drop(lock); assert_eq!(counter, 1); - assert!(waiter.poll_unpin(panic_local_waker_ref()).is_ready()); + assert!(waiter.poll_unpin(panic_waker_ref()).is_ready()); } #[test] diff --git a/futures-util/tests/select_all.rs b/futures-util/tests/select_all.rs index 85707fc2b3..5150f6f54c 100644 --- a/futures-util/tests/select_all.rs +++ b/futures-util/tests/select_all.rs @@ -5,11 +5,11 @@ use futures::FutureExt; use futures::task::Poll; use futures::stream::FusedStream; use futures::stream::{SelectAll, StreamExt}; -use futures_test::task::noop_local_waker_ref; +use futures_test::task::noop_waker_ref; #[test] fn is_terminated() { - let lw = noop_local_waker_ref(); + let lw = noop_waker_ref(); let mut tasks = SelectAll::new(); assert_eq!(tasks.is_terminated(), false); diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 0ec4aa0cbb..51b03dabf1 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -53,7 +53,7 @@ compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` featu // Macro reexports pub use futures_util::{ // Error/readiness propagation - try_ready, try_poll, ready, + try_ready, ready, }; #[cfg(feature = "std")] pub use futures_util::{ @@ -361,22 +361,17 @@ pub mod task { pub use futures_core::task::{ Poll, Spawn, LocalSpawn, SpawnError, - Waker, LocalWaker, UnsafeWake, - }; - - #[cfg(feature = "std")] - pub use futures_core::task::{ - Wake, local_waker, local_waker_from_nonlocal + Waker, RawWaker, RawWakerVTable }; #[cfg(feature = "std")] pub use futures_util::task::{ - LocalWakerRef, local_waker_ref, local_waker_ref_from_nonlocal, + WakerRef, waker_ref, ArcWake, SpawnExt, LocalSpawnExt, }; pub use futures_util::task::{ - noop_local_waker, noop_local_waker_ref, + noop_waker, noop_waker_ref, }; #[cfg_attr( diff --git a/futures/tests/eager_drop.rs b/futures/tests/eager_drop.rs index 891b9652ff..c6bcb460ae 100644 --- a/futures/tests/eager_drop.rs +++ b/futures/tests/eager_drop.rs @@ -2,7 +2,7 @@ use futures::channel::oneshot; use futures::future::{self, Future, FutureExt, TryFutureExt}; -use futures::task::{LocalWaker, Poll}; +use futures::task::{Waker, Poll}; use futures_test::future::FutureTestExt; use pin_utils::unsafe_pinned; use std::pin::Pin; @@ -56,8 +56,8 @@ impl FutureData { impl Future for FutureData { type Output = F::Output; - fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { - self.future().poll(lw) + fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll { + self.future().poll(waker) } } diff --git a/futures/tests/fuse.rs b/futures/tests/fuse.rs index dd679b333f..8ac18535ba 100644 --- a/futures/tests/fuse.rs +++ b/futures/tests/fuse.rs @@ -1,12 +1,12 @@ #![feature(futures_api)] use futures::future::{self, FutureExt}; -use futures_test::task::panic_local_waker; +use futures_test::task::panic_waker; #[test] fn fuse() { let mut future = future::ready::(2).fuse(); - let lw = &mut panic_local_waker(); + let lw = &mut panic_waker(); assert!(future.poll_unpin(lw).is_ready()); assert!(future.poll_unpin(lw).is_pending()); } diff --git a/futures/tests/future_obj.rs b/futures/tests/future_obj.rs index 433d5c2166..19da856af2 100644 --- a/futures/tests/future_obj.rs +++ b/futures/tests/future_obj.rs @@ -2,7 +2,7 @@ use futures::future::{Future, FutureExt, FutureObj}; use std::pin::Pin; -use futures::task::{LocalWaker, Poll}; +use futures::task::{Waker, Poll}; #[test] fn dropping_does_not_segfault() { @@ -18,7 +18,7 @@ fn dropping_drops_the_future() { impl<'a> Future for Inc<'a> { type Output = (); - fn poll(self: Pin<&mut Self>, _: &LocalWaker) -> Poll<()> { + fn poll(self: Pin<&mut Self>, _: &Waker) -> Poll<()> { unimplemented!() } } diff --git a/futures/tests/futures_ordered.rs b/futures/tests/futures_ordered.rs index e03beae37d..1bce15cd50 100644 --- a/futures/tests/futures_ordered.rs +++ b/futures/tests/futures_ordered.rs @@ -4,7 +4,7 @@ use futures::channel::oneshot; use futures::executor::{block_on, block_on_stream}; use futures::future::{self, FutureExt, FutureObj}; use futures::stream::{StreamExt, futures_ordered, FuturesOrdered}; -use futures_test::task::noop_local_waker_ref; +use futures_test::task::noop_waker_ref; #[test] fn works_1() { @@ -15,7 +15,7 @@ fn works_1() { let mut stream = futures_ordered(vec![a_rx, b_rx, c_rx]); b_tx.send(99).unwrap(); - assert!(stream.poll_next_unpin(&noop_local_waker_ref()).is_pending()); + assert!(stream.poll_next_unpin(&noop_waker_ref()).is_pending()); a_tx.send(33).unwrap(); c_tx.send(33).unwrap(); @@ -38,7 +38,7 @@ fn works_2() { FutureObj::new(Box::new(b_rx.join(c_rx).map(|(a, b)| Ok(a? + b?)))), ]); - let lw = &noop_local_waker_ref(); + let lw = &noop_waker_ref(); a_tx.send(33).unwrap(); b_tx.send(33).unwrap(); assert!(stream.poll_next_unpin(lw).is_ready()); diff --git a/futures/tests/futures_unordered.rs b/futures/tests/futures_unordered.rs index f6522ae98c..c257b7124b 100644 --- a/futures/tests/futures_unordered.rs +++ b/futures/tests/futures_unordered.rs @@ -7,7 +7,7 @@ use futures::stream::{StreamExt, futures_unordered, FuturesUnordered}; use futures::task::Poll; use futures_test::{assert_stream_done, assert_stream_next}; use futures_test::future::FutureTestExt; -use futures_test::task::noop_local_waker_ref; +use futures_test::task::noop_waker_ref; use std::boxed::Box; #[test] @@ -42,7 +42,7 @@ fn works_2() { a_tx.send(9).unwrap(); b_tx.send(10).unwrap(); - let lw = &noop_local_waker_ref(); + let lw = &noop_waker_ref(); assert_eq!(stream.poll_next_unpin(lw), Poll::Ready(Some(Ok(9)))); c_tx.send(20).unwrap(); assert_eq!(stream.poll_next_unpin(lw), Poll::Ready(Some(Ok(30)))); diff --git a/futures/tests/split.rs b/futures/tests/split.rs index 3ff77dbef4..12c14cacfb 100644 --- a/futures/tests/split.rs +++ b/futures/tests/split.rs @@ -3,7 +3,7 @@ use futures::executor::block_on; use futures::sink::{Sink, SinkExt}; use futures::stream::{self, Stream, StreamExt}; -use futures::task::{LocalWaker, Poll}; +use futures::task::{Waker, Poll}; use pin_utils::unsafe_pinned; use std::pin::Pin; @@ -22,9 +22,9 @@ impl Stream for Join { fn poll_next( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.stream().poll_next(lw) + self.stream().poll_next(waker) } } @@ -34,9 +34,9 @@ impl Sink for Join { fn poll_ready( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_ready(lw) + self.sink().poll_ready(waker) } fn start_send( @@ -48,14 +48,14 @@ impl Sink for Join { fn poll_flush( self: Pin<&mut Self>, - lw: &LocalWaker, + waker: &Waker, ) -> Poll> { - self.sink().poll_flush(lw) + self.sink().poll_flush(waker) } fn poll_close( self: Pin<&mut Self>, - lw: &LocalWaker, + lw: &Waker, ) -> Poll> { self.sink().poll_close(lw) } diff --git a/futures/tests_disabled/future_flatten_stream.rs b/futures/tests_disabled/future_flatten_stream.rs index 481b9b7a40..3592d4d9cb 100644 --- a/futures/tests_disabled/future_flatten_stream.rs +++ b/futures/tests_disabled/future_flatten_stream.rs @@ -25,7 +25,7 @@ impl Stream for PanickingStream { type Item = T; type Error = E; - fn poll_next(&mut self, _: &LocalWaker) -> Poll, Self::Error> { + fn poll_next(&mut self, _: &Waker) -> Poll, Self::Error> { panic!() } } diff --git a/futures/tests_disabled/sink.rs b/futures/tests_disabled/sink.rs index 9c44ddad06..59989880fd 100644 --- a/futures/tests_disabled/sink.rs +++ b/futures/tests_disabled/sink.rs @@ -98,14 +98,14 @@ impl Wake for Flag { } fn flag_lw(f: F) -> R - where F: FnOnce(Arc, &LocalWaker) -> R + where F: FnOnce(Arc, &Waker) -> R { let flag = Flag::new(); let map = &mut task::LocalMap::new(); let waker = Waker::from(flag.clone()); let exec = &mut support::PanicExec; - let lw = &LocalWaker::new(map, &waker, exec); + let lw = &Waker::new(map, &waker, exec); f(flag, lw) } @@ -122,10 +122,10 @@ impl Future for StartSendFut { type Item = S; type Error = S::SinkError; - fn poll(&mut self, lw: &LocalWaker) -> Poll { + fn poll(&mut self, waker: &Waker) -> Poll { { let inner = self.0.as_mut().unwrap(); - try_ready!(inner.poll_ready(lw)); + try_ready!(inner.poll_ready(waker)); inner.start_send(self.1.take().unwrap())?; } Ok(Async::Ready(self.0.take().unwrap())) @@ -141,15 +141,15 @@ fn mpsc_blocking_start_send() { block_on(futures::future::lazy(|_| { tx.start_send(0).unwrap(); - flag_lw(|flag, lw| { + flag_lw(|flag, waker| { let mut task = StartSendFut::new(tx, 1); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); assert!(!flag.get()); sassert_next(&mut rx, 0); assert!(flag.get()); flag.set(false); - assert!(task.poll(lw).unwrap().is_ready()); + assert!(task.poll(waker).unwrap().is_ready()); assert!(!flag.get()); sassert_next(&mut rx, 1); @@ -171,13 +171,13 @@ fn with_flush() { assert_eq!(sink.start_send(0), Ok(())); - flag_lw(|flag, lw| { + flag_lw(|flag, waker| { let mut task = sink.flush(); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); tx.send(()).unwrap(); assert!(flag.get()); - let sink = match task.poll(lw).unwrap() { + let sink = match task.poll(waker).unwrap() { Async::Ready(sink) => sink, _ => panic!() }; @@ -222,7 +222,7 @@ impl Sink for ManualFlush { type SinkItem = Option; // Pass None to flush type SinkError = (); - fn poll_ready(&mut self, _: &LocalWaker) -> Poll<(), Self::SinkError> { + fn poll_ready(&mut self, _: &Waker) -> Poll<(), Self::SinkError> { Ok(Async::Ready(())) } @@ -235,17 +235,17 @@ impl Sink for ManualFlush { Ok(()) } - fn poll_flush(&mut self, lw: &LocalWaker) -> Poll<(), Self::SinkError> { + fn poll_flush(&mut self, waker: &Waker) -> Poll<(), Self::SinkError> { if self.data.is_empty() { Ok(Async::Ready(())) } else { - self.waiting_tasks.push(lw.waker().clone()); + self.waiting_tasks.push(waker.waker().clone()); Ok(Async::Pending) } } - fn poll_close(&mut self, lw: &LocalWaker) -> Poll<(), Self::SinkError> { - self.poll_flush(lw) + fn poll_close(&mut self, waker: &Waker) -> Poll<(), Self::SinkError> { + self.poll_flush(waker) } } @@ -270,18 +270,18 @@ impl ManualFlush { // but doesn't claim to be flushed until the underlying sink is fn with_flush_propagate() { let mut sink = ManualFlush::new().with(|x| -> Result, ()> { Ok(x) }); - flag_lw(|flag, lw| { - assert!(sink.poll_ready(lw).unwrap().is_ready()); + flag_lw(|flag, waker| { + assert!(sink.poll_ready(waker).unwrap().is_ready()); sink.start_send(Some(0)).unwrap(); - assert!(sink.poll_ready(lw).unwrap().is_ready()); + assert!(sink.poll_ready(waker).unwrap().is_ready()); sink.start_send(Some(1)).unwrap(); let mut task = sink.flush(); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); assert!(!flag.get()); assert_eq!(task.get_mut().unwrap().get_mut().force_flush(), vec![0, 1]); assert!(flag.get()); - assert!(task.poll(lw).unwrap().is_ready()); + assert!(task.poll(waker).unwrap().is_ready()); }) } @@ -317,11 +317,11 @@ impl Allow { } } - fn check(&self, lw: &LocalWaker) -> bool { + fn check(&self, waker: &Waker) -> bool { if self.flag.get() { true } else { - self.tasks.borrow_mut().push(lw.waker().clone()); + self.tasks.borrow_mut().push(waker.waker().clone()); false } } @@ -339,8 +339,8 @@ impl Sink for ManualAllow { type SinkItem = T; type SinkError = Never; - fn poll_ready(&mut self, lw: &LocalWaker) -> Poll<(), Self::SinkError> { - if self.allow.check(lw) { + fn poll_ready(&mut self, waker: &Waker) -> Poll<(), Self::SinkError> { + if self.allow.check(waker) { Ok(Async::Ready(())) } else { Ok(Async::Pending) @@ -352,11 +352,11 @@ impl Sink for ManualAllow { Ok(()) } - fn poll_flush(&mut self, _: &LocalWaker) -> Poll<(), Self::SinkError> { + fn poll_flush(&mut self, _: &Waker) -> Poll<(), Self::SinkError> { Ok(Async::Ready(())) } - fn poll_close(&mut self, _: &LocalWaker) -> Poll<(), Self::SinkError> { + fn poll_close(&mut self, _: &Waker) -> Poll<(), Self::SinkError> { Ok(Async::Ready(())) } } @@ -380,13 +380,13 @@ fn buffer() { let sink = block_on(StartSendFut::new(sink, 0)).unwrap(); let sink = block_on(StartSendFut::new(sink, 1)).unwrap(); - flag_lw(|flag, lw| { + flag_lw(|flag, waker| { let mut task = sink.send(2); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); assert!(!flag.get()); allow.start(); assert!(flag.get()); - match task.poll(lw).unwrap() { + match task.poll(waker).unwrap() { Async::Ready(sink) => { assert_eq!(sink.get_ref().data, vec![0, 1, 2]); } @@ -415,18 +415,18 @@ fn fanout_backpressure() { let sink = block_on(StartSendFut::new(sink, 0)).unwrap(); - flag_lw(|flag, lw| { + flag_lw(|flag, waker| { let mut task = sink.send(2); assert!(!flag.get()); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); let (item, left_recv) = block_on(left_recv.next()).unwrap(); assert_eq!(item, Some(0)); assert!(flag.get()); - assert!(task.poll(lw).unwrap().is_pending()); + assert!(task.poll(waker).unwrap().is_pending()); let (item, right_recv) = block_on(right_recv.next()).unwrap(); assert_eq!(item, Some(0)); assert!(flag.get()); - assert!(task.poll(lw).unwrap().is_ready()); + assert!(task.poll(waker).unwrap().is_ready()); // make sure receivers live until end of test to prevent send errors drop(left_recv); drop(right_recv); @@ -435,11 +435,11 @@ fn fanout_backpressure() { #[test] fn map_err() { - panic_waker_lw(|lw| { + panic_waker_lw(|waker| { let (tx, _rx) = mpsc::channel(1); let mut tx = tx.sink_map_err(|_| ()); assert_eq!(tx.start_send(()), Ok(())); - assert_eq!(tx.poll_flush(lw), Ok(Async::Ready(()))); + assert_eq!(tx.poll_flush(waker), Ok(Async::Ready(()))); }); let tx = mpsc::channel(0).0; diff --git a/futures/tests_disabled/stream.rs b/futures/tests_disabled/stream.rs index 59df9a3730..d65b293436 100644 --- a/futures/tests_disabled/stream.rs +++ b/futures/tests_disabled/stream.rs @@ -25,7 +25,7 @@ impl Stream for Iter type Item = T; type Error = E; - fn poll_next(&mut self, _: &LocalWaker) -> Poll, E> { + fn poll_next(&mut self, _: &Waker) -> Poll, E> { match self.iter.next() { Some(Ok(e)) => Ok(Async::Ready(Some(e))), Some(Err(e)) => Err(e), @@ -288,13 +288,13 @@ fn peek() { type Item = (); type Error = u32; - fn poll(&mut self, lw: &LocalWaker) -> Poll<(), u32> { + fn poll(&mut self, waker: &Waker) -> Poll<(), u32> { { - let res = try_ready!(self.inner.peek(lw)); + let res = try_ready!(self.inner.peek(waker)); assert_eq!(res, Some(&1)); } - assert_eq!(self.inner.peek(lw).unwrap(), Some(&1).into()); - assert_eq!(self.inner.poll_next(lw).unwrap(), Some(1).into()); + assert_eq!(self.inner.peek(waker).unwrap(), Some(&1).into()); + assert_eq!(self.inner.poll_next(waker).unwrap(), Some(1).into()); Ok(Async::Ready(())) } }