-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathexample_async.rs
59 lines (48 loc) · 985 Bytes
/
example_async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use async_std::task;
use trace::trace;
trace::init_depth_var!();
#[trace]
async fn squared(x: i32) -> i32 {
x * x
}
#[async_trait::async_trait]
trait Log {
async fn log(&self, message: &str) -> String;
}
#[async_trait::async_trait]
trait Cubed {
async fn cubed(x: i32) -> i32;
}
struct Logger {
level: String,
}
struct Math {}
#[trace]
#[async_trait::async_trait]
impl Log for Logger {
async fn log(&self, message: &str) -> String {
format!("[{}] {message}", self.level)
}
}
#[trace]
#[async_trait::async_trait]
impl Cubed for Math {
async fn cubed(x: i32) -> i32 {
squared(squared(x).await).await
}
}
fn main() {
task::block_on(async {
squared(64).await;
let logger = Logger {
level: "DEBUG".to_string(),
};
logger.log("something happened").await;
Math::cubed(32).await;
});
}
#[cfg(test)]
#[macro_use]
mod trace_test;
#[cfg(test)]
trace_test!(test_async, main());