Skip to content

Commit c14f058

Browse files
srijscramertj
authored andcommitted
Expose task::Context to lazy
1 parent 2eb3e07 commit c14f058

File tree

8 files changed

+24
-24
lines changed

8 files changed

+24
-24
lines changed

futures-executor/tests/local_pool.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn run_until_single_future() {
4040
{
4141
let mut pool = LocalPool::new();
4242
let mut exec = pool.executor();
43-
let fut = lazy(|| {
43+
let fut = lazy(|_| {
4444
cnt += 1;
4545
DONE
4646
});
@@ -55,15 +55,15 @@ fn run_until_ignores_spawned() {
5555
let mut pool = LocalPool::new();
5656
let mut exec = pool.executor();
5757
exec.spawn_local(Box::new(pending())).unwrap();
58-
pool.run_until(lazy(|| DONE), &mut exec).unwrap();
58+
pool.run_until(lazy(|_| DONE), &mut exec).unwrap();
5959
}
6060

6161
#[test]
6262
fn run_until_executes_spawned() {
6363
let (tx, rx) = oneshot::channel();
6464
let mut pool = LocalPool::new();
6565
let mut exec = pool.executor();
66-
exec.spawn_local(Box::new(lazy(move || {
66+
exec.spawn_local(Box::new(lazy(move |_| {
6767
tx.send(()).unwrap();
6868
DONE
6969
}))).unwrap();
@@ -79,8 +79,8 @@ fn run_executes_spawned() {
7979
let mut exec = pool.executor();
8080
let mut exec2 = pool.executor();
8181

82-
exec.spawn_local(Box::new(lazy(move || {
83-
exec2.spawn_local(Box::new(lazy(move || {
82+
exec.spawn_local(Box::new(lazy(move |_| {
83+
exec2.spawn_local(Box::new(lazy(move |_| {
8484
cnt2.set(cnt2.get() + 1);
8585
DONE
8686
}))).unwrap();
@@ -103,7 +103,7 @@ fn run_spawn_many() {
103103

104104
for _ in 0..ITER {
105105
let cnt = cnt.clone();
106-
exec.spawn_local(Box::new(lazy(move || {
106+
exec.spawn_local(Box::new(lazy(move |_| {
107107
cnt.set(cnt.get() + 1);
108108
DONE
109109
}))).unwrap();
@@ -120,7 +120,7 @@ fn nesting_run() {
120120
let mut pool = LocalPool::new();
121121
let mut exec = pool.executor();
122122

123-
exec.spawn(Box::new(lazy(|| {
123+
exec.spawn(Box::new(lazy(|_| {
124124
let mut pool = LocalPool::new();
125125
let mut exec = pool.executor();
126126
pool.run(&mut exec);

futures-util/src/future/lazy.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ enum _Lazy<R, F> {
3737
/// use futures::future::{self, FutureResult};
3838
///
3939
/// # fn main() {
40-
/// let a = future::lazy(|| future::ok::<u32, u32>(1));
40+
/// let a = future::lazy(|_| future::ok::<u32, u32>(1));
4141
///
42-
/// let b = future::lazy(|| -> FutureResult<u32, u32> {
42+
/// let b = future::lazy(|_| -> FutureResult<u32, u32> {
4343
/// panic!("oh no!")
4444
/// });
4545
/// drop(b); // closure is never run
4646
/// # }
4747
/// ```
4848
pub fn lazy<R, F>(f: F) -> Lazy<R, F>
49-
where F: FnOnce() -> R,
49+
where F: FnOnce(&mut task::Context) -> R,
5050
R: IntoFuture
5151
{
5252
Lazy {
@@ -55,17 +55,17 @@ pub fn lazy<R, F>(f: F) -> Lazy<R, F>
5555
}
5656

5757
impl<R, F> Lazy<R, F>
58-
where F: FnOnce() -> R,
58+
where F: FnOnce(&mut task::Context) -> R,
5959
R: IntoFuture,
6060
{
61-
fn get(&mut self) -> &mut R::Future {
61+
fn get(&mut self, cx: &mut task::Context) -> &mut R::Future {
6262
match self.inner {
6363
_Lazy::First(_) => {}
6464
_Lazy::Second(ref mut f) => return f,
6565
_Lazy::Moved => panic!(), // can only happen if `f()` panics
6666
}
6767
match mem::replace(&mut self.inner, _Lazy::Moved) {
68-
_Lazy::First(f) => self.inner = _Lazy::Second(f().into_future()),
68+
_Lazy::First(f) => self.inner = _Lazy::Second(f(cx).into_future()),
6969
_ => panic!(), // we already found First
7070
}
7171
match self.inner {
@@ -76,13 +76,13 @@ impl<R, F> Lazy<R, F>
7676
}
7777

7878
impl<R, F> Future for Lazy<R, F>
79-
where F: FnOnce() -> R,
79+
where F: FnOnce(&mut task::Context) -> R,
8080
R: IntoFuture,
8181
{
8282
type Item = R::Item;
8383
type Error = R::Error;
8484

8585
fn poll(&mut self, cx: &mut task::Context) -> Poll<R::Item, R::Error> {
86-
self.get().poll(cx)
86+
self.get(cx).poll(cx)
8787
}
8888
}

futures-util/src/future/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ pub trait FutureExt: Future {
741741
/// let mut future = future::ok::<i32, u32>(2);
742742
/// assert!(block_on(future.catch_unwind()).is_ok());
743743
///
744-
/// let mut future = future::lazy(|| -> FutureResult<i32, u32> {
744+
/// let mut future = future::lazy(|_| -> FutureResult<i32, u32> {
745745
/// panic!();
746746
/// future::ok::<i32, u32>(2)
747747
/// });

futures/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub mod executor {
119119
//! ```
120120
//! use futures::executor::ThreadPool;
121121
//! # use futures::future::{Future, lazy};
122-
//! # let my_app: Box<Future<Item = (), Error = ()>> = Box::new(lazy(|| Ok(())));
122+
//! # let my_app: Box<Future<Item = (), Error = ()>> = Box::new(lazy(|_| Ok(())));
123123
//!
124124
//! // assumping `my_app: Future`
125125
//! ThreadPool::new().run(my_app);

tests/bilock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use support::*;
1414

1515
#[test]
1616
fn smoke() {
17-
let future = future::lazy(|| {
17+
let future = future::lazy(|_| {
1818
let (a, b) = BiLock::new(1);
1919

2020
{

tests/ready_queue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl AssertSendSync for FuturesUnordered<()> {}
1313

1414
#[test]
1515
fn basic_usage() {
16-
future::lazy(move || {
16+
future::lazy(move |_| {
1717
let mut queue = FuturesUnordered::new();
1818
let (tx1, rx1) = oneshot::channel();
1919
let (tx2, rx2) = oneshot::channel();
@@ -43,7 +43,7 @@ fn basic_usage() {
4343

4444
#[test]
4545
fn resolving_errors() {
46-
future::lazy(move || {
46+
future::lazy(move |_| {
4747
let mut queue = FuturesUnordered::new();
4848
let (tx1, rx1) = oneshot::channel();
4949
let (tx2, rx2) = oneshot::channel();
@@ -73,7 +73,7 @@ fn resolving_errors() {
7373

7474
#[test]
7575
fn dropping_ready_queue() {
76-
future::lazy(move || {
76+
future::lazy(move |_| {
7777
let mut queue = FuturesUnordered::new();
7878
let (mut tx1, rx1) = oneshot::channel::<()>();
7979
let (mut tx2, rx2) = oneshot::channel::<()>();
@@ -148,7 +148,7 @@ fn stress() {
148148

149149
#[test]
150150
fn panicking_future_dropped() {
151-
future::lazy(move || {
151+
future::lazy(move |_| {
152152
let mut queue = FuturesUnordered::new();
153153
queue.push(future::poll_fn(|| -> Poll<i32, i32> {
154154
panic!()

tests/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn recursive_poll_with_unpark() {
185185
let f1 = run_stream.shared();
186186
let f2 = f1.clone();
187187
let f3 = f1.clone();
188-
tx0.unbounded_send(Box::new(future::lazy(move || {
188+
tx0.unbounded_send(Box::new(future::lazy(move |_| {
189189
task::current().notify();
190190
f1.map(|_|()).map_err(|_|())
191191
.select(rx1.map_err(|_|()))

tests/sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<S: Sink> Future for StartSendFut<S> {
109109
fn mpsc_blocking_start_send() {
110110
let (mut tx, mut rx) = mpsc::channel::<i32>(0);
111111

112-
futures::future::lazy(|| {
112+
futures::future::lazy(|_| {
113113
assert_eq!(tx.start_send(0).unwrap(), AsyncSink::Ready);
114114

115115
let flag = Flag::new();

0 commit comments

Comments
 (0)