From 42e6716802898e3840735b30b99630e799cbb3f3 Mon Sep 17 00:00:00 2001 From: Sam Rijs Date: Sun, 18 Mar 2018 17:59:48 +1100 Subject: [PATCH] Expose task::Context to lazy --- futures-executor/tests/local_pool.rs | 14 +++++++------- futures-util/src/future/lazy.rs | 16 ++++++++-------- futures-util/src/future/mod.rs | 2 +- futures/src/lib.rs | 2 +- tests/bilock.rs | 2 +- tests/ready_queue.rs | 8 ++++---- tests/shared.rs | 2 +- tests/sink.rs | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/futures-executor/tests/local_pool.rs b/futures-executor/tests/local_pool.rs index c097dbecc5..8634cd0054 100755 --- a/futures-executor/tests/local_pool.rs +++ b/futures-executor/tests/local_pool.rs @@ -40,7 +40,7 @@ fn run_until_single_future() { { let mut pool = LocalPool::new(); let mut exec = pool.executor(); - let fut = lazy(|| { + let fut = lazy(|_| { cnt += 1; DONE }); @@ -55,7 +55,7 @@ fn run_until_ignores_spawned() { let mut pool = LocalPool::new(); let mut exec = pool.executor(); exec.spawn_local(Box::new(pending())).unwrap(); - pool.run_until(lazy(|| DONE), &mut exec).unwrap(); + pool.run_until(lazy(|_| DONE), &mut exec).unwrap(); } #[test] @@ -63,7 +63,7 @@ fn run_until_executes_spawned() { let (tx, rx) = oneshot::channel(); let mut pool = LocalPool::new(); let mut exec = pool.executor(); - exec.spawn_local(Box::new(lazy(move || { + exec.spawn_local(Box::new(lazy(move |_| { tx.send(()).unwrap(); DONE }))).unwrap(); @@ -79,8 +79,8 @@ fn run_executes_spawned() { let mut exec = pool.executor(); let mut exec2 = pool.executor(); - exec.spawn_local(Box::new(lazy(move || { - exec2.spawn_local(Box::new(lazy(move || { + exec.spawn_local(Box::new(lazy(move |_| { + exec2.spawn_local(Box::new(lazy(move |_| { cnt2.set(cnt2.get() + 1); DONE }))).unwrap(); @@ -103,7 +103,7 @@ fn run_spawn_many() { for _ in 0..ITER { let cnt = cnt.clone(); - exec.spawn_local(Box::new(lazy(move || { + exec.spawn_local(Box::new(lazy(move |_| { cnt.set(cnt.get() + 1); DONE }))).unwrap(); @@ -120,7 +120,7 @@ fn nesting_run() { let mut pool = LocalPool::new(); let mut exec = pool.executor(); - exec.spawn(Box::new(lazy(|| { + exec.spawn(Box::new(lazy(|_| { let mut pool = LocalPool::new(); let mut exec = pool.executor(); pool.run(&mut exec); diff --git a/futures-util/src/future/lazy.rs b/futures-util/src/future/lazy.rs index 448f999ac9..4c1dc0468a 100644 --- a/futures-util/src/future/lazy.rs +++ b/futures-util/src/future/lazy.rs @@ -37,16 +37,16 @@ enum _Lazy { /// use futures::future::{self, FutureResult}; /// /// # fn main() { -/// let a = future::lazy(|| future::ok::(1)); +/// let a = future::lazy(|_| future::ok::(1)); /// -/// let b = future::lazy(|| -> FutureResult { +/// let b = future::lazy(|_| -> FutureResult { /// panic!("oh no!") /// }); /// drop(b); // closure is never run /// # } /// ``` pub fn lazy(f: F) -> Lazy - where F: FnOnce() -> R, + where F: FnOnce(&mut task::Context) -> R, R: IntoFuture { Lazy { @@ -55,17 +55,17 @@ pub fn lazy(f: F) -> Lazy } impl Lazy - where F: FnOnce() -> R, + where F: FnOnce(&mut task::Context) -> R, R: IntoFuture, { - fn get(&mut self) -> &mut R::Future { + fn get(&mut self, cx: &mut task::Context) -> &mut R::Future { match self.inner { _Lazy::First(_) => {} _Lazy::Second(ref mut f) => return f, _Lazy::Moved => panic!(), // can only happen if `f()` panics } match mem::replace(&mut self.inner, _Lazy::Moved) { - _Lazy::First(f) => self.inner = _Lazy::Second(f().into_future()), + _Lazy::First(f) => self.inner = _Lazy::Second(f(cx).into_future()), _ => panic!(), // we already found First } match self.inner { @@ -76,13 +76,13 @@ impl Lazy } impl Future for Lazy - where F: FnOnce() -> R, + where F: FnOnce(&mut task::Context) -> R, R: IntoFuture, { type Item = R::Item; type Error = R::Error; fn poll(&mut self, cx: &mut task::Context) -> Poll { - self.get().poll(cx) + self.get(cx).poll(cx) } } diff --git a/futures-util/src/future/mod.rs b/futures-util/src/future/mod.rs index a1f1148f92..e463abb041 100644 --- a/futures-util/src/future/mod.rs +++ b/futures-util/src/future/mod.rs @@ -741,7 +741,7 @@ pub trait FutureExt: Future { /// let mut future = future::ok::(2); /// assert!(block_on(future.catch_unwind()).is_ok()); /// - /// let mut future = future::lazy(|| -> FutureResult { + /// let mut future = future::lazy(|_| -> FutureResult { /// panic!(); /// future::ok::(2) /// }); diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 957694807f..045aabd2b4 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -119,7 +119,7 @@ pub mod executor { //! ``` //! use futures::executor::ThreadPool; //! # use futures::future::{Future, lazy}; - //! # let my_app: Box> = Box::new(lazy(|| Ok(()))); + //! # let my_app: Box> = Box::new(lazy(|_| Ok(()))); //! //! // assumping `my_app: Future` //! ThreadPool::new().run(my_app); diff --git a/tests/bilock.rs b/tests/bilock.rs index 595b48b873..d42e769c36 100644 --- a/tests/bilock.rs +++ b/tests/bilock.rs @@ -14,7 +14,7 @@ use support::*; #[test] fn smoke() { - let future = future::lazy(|| { + let future = future::lazy(|_| { let (a, b) = BiLock::new(1); { diff --git a/tests/ready_queue.rs b/tests/ready_queue.rs index b0dc2375ba..d3db20aee2 100644 --- a/tests/ready_queue.rs +++ b/tests/ready_queue.rs @@ -13,7 +13,7 @@ impl AssertSendSync for FuturesUnordered<()> {} #[test] fn basic_usage() { - future::lazy(move || { + future::lazy(move |_| { let mut queue = FuturesUnordered::new(); let (tx1, rx1) = oneshot::channel(); let (tx2, rx2) = oneshot::channel(); @@ -43,7 +43,7 @@ fn basic_usage() { #[test] fn resolving_errors() { - future::lazy(move || { + future::lazy(move |_| { let mut queue = FuturesUnordered::new(); let (tx1, rx1) = oneshot::channel(); let (tx2, rx2) = oneshot::channel(); @@ -73,7 +73,7 @@ fn resolving_errors() { #[test] fn dropping_ready_queue() { - future::lazy(move || { + future::lazy(move |_| { let mut queue = FuturesUnordered::new(); let (mut tx1, rx1) = oneshot::channel::<()>(); let (mut tx2, rx2) = oneshot::channel::<()>(); @@ -148,7 +148,7 @@ fn stress() { #[test] fn panicking_future_dropped() { - future::lazy(move || { + future::lazy(move |_| { let mut queue = FuturesUnordered::new(); queue.push(future::poll_fn(|| -> Poll { panic!() diff --git a/tests/shared.rs b/tests/shared.rs index 99d2b381ea..1027563d0f 100644 --- a/tests/shared.rs +++ b/tests/shared.rs @@ -185,7 +185,7 @@ fn recursive_poll_with_unpark() { let f1 = run_stream.shared(); let f2 = f1.clone(); let f3 = f1.clone(); - tx0.unbounded_send(Box::new(future::lazy(move || { + tx0.unbounded_send(Box::new(future::lazy(move |_| { task::current().notify(); f1.map(|_|()).map_err(|_|()) .select(rx1.map_err(|_|())) diff --git a/tests/sink.rs b/tests/sink.rs index fb5f7b3738..dbd96711e4 100644 --- a/tests/sink.rs +++ b/tests/sink.rs @@ -109,7 +109,7 @@ impl Future for StartSendFut { fn mpsc_blocking_start_send() { let (mut tx, mut rx) = mpsc::channel::(0); - futures::future::lazy(|| { + futures::future::lazy(|_| { assert_eq!(tx.start_send(0).unwrap(), AsyncSink::Ready); let flag = Flag::new();