]> git.lizzy.rs Git - rust.git/blob - tests/run-make/coverage/async2.rs
Auto merge of #103761 - chenyukang:yukang/fix-103320-must-use, r=compiler-errors
[rust.git] / tests / run-make / coverage / async2.rs
1 // compile-flags: --edition=2018
2
3 use core::{
4     future::Future,
5     marker::Send,
6     pin::Pin,
7 };
8
9 fn non_async_func() {
10     println!("non_async_func was covered");
11     let b = true;
12     if b {
13         println!("non_async_func println in block");
14     }
15 }
16
17
18
19
20 async fn async_func() {
21     println!("async_func was covered");
22     let b = true;
23     if b {
24         println!("async_func println in block");
25     }
26 }
27
28
29
30
31 async fn async_func_just_println() {
32     println!("async_func_just_println was covered");
33 }
34
35 fn main() {
36     println!("codecovsample::main");
37
38     non_async_func();
39
40     executor::block_on(async_func());
41     executor::block_on(async_func_just_println());
42 }
43
44 mod executor {
45     use core::{
46         future::Future,
47         pin::Pin,
48         task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
49     };
50
51     pub fn block_on<F: Future>(mut future: F) -> F::Output {
52         let mut future = unsafe { Pin::new_unchecked(&mut future) };
53         use std::hint::unreachable_unchecked;
54         static VTABLE: RawWakerVTable = RawWakerVTable::new(
55             |_| unsafe { unreachable_unchecked() }, // clone
56             |_| unsafe { unreachable_unchecked() }, // wake
57             |_| unsafe { unreachable_unchecked() }, // wake_by_ref
58             |_| (),
59         );
60         let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
61         let mut context = Context::from_waker(&waker);
62
63         loop {
64             if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
65                 break val;
66             }
67         }
68     }
69 }