Skip to content

Commit 4f0faed

Browse files
committed
Auto merge of #1922 - RalfJung:async-run-fut, r=RalfJung
async-fn test: make run_fut more general and entirely safe
2 parents 2cd6fe8 + c4502cb commit 4f0faed

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tests/run-pass/async-fn.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![feature(never_type)]
22

3-
use std::{future::Future, pin::Pin, task::Poll};
4-
use std::task::{Wake, Waker, Context};
5-
use std::sync::Arc;
3+
use std::future::Future;
64

75
// See if we can run a basic `async fn`
86
pub async fn foo(x: &u32, y: u32) -> u32 {
@@ -47,7 +45,10 @@ async fn partial_init(x: u32) -> u32 {
4745
let _x: (String, !) = (String::new(), return async { x + x }.await);
4846
}
4947

50-
fn run_fut(mut fut: impl Future<Output=u32>, output: u32) {
48+
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
49+
use std::sync::Arc;
50+
use std::task::{Context, Poll, Wake, Waker};
51+
5152
struct MyWaker;
5253
impl Wake for MyWaker {
5354
fn wake(self: Arc<Self>) {
@@ -57,16 +58,20 @@ fn run_fut(mut fut: impl Future<Output=u32>, output: u32) {
5758

5859
let waker = Waker::from(Arc::new(MyWaker));
5960
let mut context = Context::from_waker(&waker);
60-
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(output));
61+
62+
let mut pinned = Box::pin(fut);
63+
loop {
64+
match pinned.as_mut().poll(&mut context) {
65+
Poll::Pending => continue,
66+
Poll::Ready(v) => return v,
67+
}
68+
}
6169
}
6270

6371
fn main() {
6472
let x = 5;
65-
run_fut(foo(&x, 7), 31);
66-
67-
run_fut(build_aggregate(1, 2, 3, 4), 10);
68-
69-
run_fut(includes_never(false, 4), 16);
70-
71-
run_fut(partial_init(4), 8);
73+
assert_eq!(run_fut(foo(&x, 7)), 31);
74+
assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10);
75+
assert_eq!(run_fut(includes_never(false, 4)), 16);
76+
assert_eq!(run_fut(partial_init(4)), 8);
7277
}

0 commit comments

Comments
 (0)