aboutsummaryrefslogtreecommitdiff
path: root/examples/example-async-support/src/main.rs
blob: 12b1b9c42838b1c146149f98eeba52e76e05ea90 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
//! Example Async Runtime Support
//!
//! > This example shows how to drive an async runtime using the `async` feature
//!
//! ## Note
//!
//! When the `async` feature is enabled, **Mingling** provides a different framework implementation,
//! allowing you to use the `async` keyword directly within `#[chain]`.
//!
//! However, you will lose some capabilities:
//!
//! 1. `&mut` resource injection is not available in async chain functions
//! 2. The program will not be able to use panic unwind functionality
//!
//! Run:
//! ```bash
//! cargo run --manifest-path examples/example-async-support/Cargo.toml --quiet -- download README.md
//! ```
//!
//! Output:
//! ```plaintext
//! Download begin
//! # (Will pause for 1 second here)
//! "README.md" downloaded.
//! ```

use mingling::{hook::ProgramHook, prelude::*};

#[tokio::main]
async fn main() {
    let mut program = ThisProgram::new();

    program.with_dispatcher(CMDDownload);

    // Add a hook to display when the download begins
    program.with_hook(ProgramHook::empty().on_begin(|| println!("Download begin")));

    // --------- IMPORTANT ---------
    // The return values of `exec_*()` related functions have been replaced with Futures
    program.exec_and_exit().await;
    // --------- IMPORTANT ---------
}

dispatcher!("download", CMDDownload => EntryDownload);

pack!(ResultDownloaded = String);

// --------- IMPORTANT ---------
#[chain]
//  vvvvv_ `async` keyword can be used directly here
pub async fn handle_download(args: EntryDownload) -> Next {
    let file_name = args.pick(()).unpack();
    fake_download(file_name).await
}

#[renderer]
// But renderers cannot use the `async` keyword
pub fn render_downloaded(result: ResultDownloaded) {
    r_println!("\"{}\" downloaded.", *result);
}
// --------- IMPORTANT ---------

gen_program!();

async fn fake_download(file_name: String) -> ResultDownloaded {
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    ResultDownloaded::new(file_name)
}