@@ -14,9 +14,21 @@ use std::{
14
14
fn main ( ) { }
15
15
16
16
#[ kani:: proof]
17
- #[ kani:: unwind( 10 ) ]
17
+ #[ kani:: unwind( 2 ) ]
18
18
fn test_async_await ( ) {
19
- poll_loop ( async {
19
+ // Test using the `block_on` implementation in Kani's library
20
+ kani:: block_on ( async {
21
+ let async_block_result = async { 42 } . await ;
22
+ let async_fn_result = async_fn ( ) . await ;
23
+ assert_eq ! ( async_block_result, async_fn_result) ;
24
+ } )
25
+ }
26
+
27
+ #[ kani:: proof]
28
+ #[ kani:: unwind( 2 ) ]
29
+ fn test_async_await_manually ( ) {
30
+ // Test using the manual `block_on` implementation
31
+ block_on ( async {
20
32
let async_block_result = async { 42 } . await ;
21
33
let async_fn_result = async_fn ( ) . await ;
22
34
assert_eq ! ( async_block_result, async_fn_result) ;
@@ -28,12 +40,12 @@ pub async fn async_fn() -> i32 {
28
40
}
29
41
30
42
/// A very simple executor that just polls the future in a loop
31
- pub fn poll_loop < F : Future > ( mut fut : F ) -> < F as Future > :: Output {
43
+ pub fn block_on < T > ( mut fut : impl Future < Output = T > ) -> T {
32
44
let waker = unsafe { Waker :: from_raw ( NOOP_RAW_WAKER ) } ;
33
45
let cx = & mut Context :: from_waker ( & waker) ;
46
+ let mut fut = unsafe { Pin :: new_unchecked ( & mut fut) } ;
34
47
loop {
35
- let pinned = unsafe { Pin :: new_unchecked ( & mut fut) } ;
36
- match pinned. poll ( cx) {
48
+ match fut. as_mut ( ) . poll ( cx) {
37
49
std:: task:: Poll :: Ready ( res) => return res,
38
50
std:: task:: Poll :: Pending => continue ,
39
51
}
@@ -45,11 +57,6 @@ const NOOP_RAW_WAKER: RawWaker = {
45
57
unsafe fn clone_waker ( _: * const ( ) ) -> RawWaker {
46
58
NOOP_RAW_WAKER
47
59
}
48
- unsafe fn wake ( _: * const ( ) ) { }
49
- unsafe fn wake_by_ref ( _: * const ( ) ) { }
50
- unsafe fn drop_waker ( _: * const ( ) ) { }
51
- RawWaker :: new (
52
- std:: ptr:: null ( ) ,
53
- & RawWakerVTable :: new ( clone_waker, wake, wake_by_ref, drop_waker) ,
54
- )
60
+ unsafe fn noop ( _: * const ( ) ) { }
61
+ RawWaker :: new ( std:: ptr:: null ( ) , & RawWakerVTable :: new ( clone_waker, noop, noop, noop) )
55
62
} ;
0 commit comments