]> git.proxmox.com Git - rustc.git/blob - tests/coverage/closure_macro_async.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / tests / coverage / closure_macro_async.rs
1 // compile-flags: --edition=2018
2 #![feature(coverage_attribute)]
3
4 macro_rules! bail {
5 ($msg:literal $(,)?) => {
6 if $msg.len() > 0 {
7 println!("no msg");
8 } else {
9 println!($msg);
10 }
11 return Err(String::from($msg));
12 };
13 }
14
15 macro_rules! on_error {
16 ($value:expr, $error_message:expr) => {
17 $value.or_else(|e| { // FIXME(85000): no coverage in closure macros
18 let message = format!($error_message, e);
19 if message.len() > 0 {
20 println!("{}", message);
21 Ok(String::from("ok"))
22 } else {
23 bail!("error");
24 }
25 })
26 };
27 }
28
29 fn load_configuration_files() -> Result<String, String> {
30 Ok(String::from("config"))
31 }
32
33 pub async fn test() -> Result<(), String> {
34 println!("Starting service");
35 let config = on_error!(load_configuration_files(), "Error loading configs: {}")?;
36
37 let startup_delay_duration = String::from("arg");
38 let _ = (config, startup_delay_duration);
39 Ok(())
40 }
41
42 #[coverage(off)]
43 fn main() {
44 executor::block_on(test()).unwrap();
45 }
46
47 mod executor {
48 use core::{
49 future::Future,
50 pin::Pin,
51 task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
52 };
53
54 #[coverage(off)]
55 pub fn block_on<F: Future>(mut future: F) -> F::Output {
56 let mut future = unsafe { Pin::new_unchecked(&mut future) };
57 use std::hint::unreachable_unchecked;
58 static VTABLE: RawWakerVTable = RawWakerVTable::new(
59 #[coverage(off)]
60 |_| unsafe { unreachable_unchecked() }, // clone
61 #[coverage(off)]
62 |_| unsafe { unreachable_unchecked() }, // wake
63 #[coverage(off)]
64 |_| unsafe { unreachable_unchecked() }, // wake_by_ref
65 #[coverage(off)]
66 |_| (),
67 );
68 let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
69 let mut context = Context::from_waker(&waker);
70
71 loop {
72 if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
73 break val;
74 }
75 }
76 }
77 }