aboutsummaryrefslogtreecommitdiff
path: root/examples/example-async/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-25 19:12:00 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-25 19:19:54 +0800
commitbcbf0506ff255129d5a66d709bdb6aafbfbe2331 (patch)
treec733cb33f701852e84667a4cf9d12d7ad1813da9 /examples/example-async/src
parentd4634b01e3f33b3ee52b1501f5ade739a1796d08 (diff)
Add async example demonstrating tokio integration
Diffstat (limited to 'examples/example-async/src')
-rw-r--r--examples/example-async/src/main.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/example-async/src/main.rs b/examples/example-async/src/main.rs
new file mode 100644
index 0000000..29aee7e
--- /dev/null
+++ b/examples/example-async/src/main.rs
@@ -0,0 +1,53 @@
+//! `Mingling` Example - Async
+//!
+//! After enabling the `async` feature:
+//! 1. The `chain!` macro will support using **async** functions,
+//! 2. The `exec` function of `Program` will return a `Future` for you to use with an async runtime
+//!
+//! ## Enable Feature
+//! Enable the `async` feature for mingling in `Cargo.toml`
+//! ```toml
+//! [dependencies]
+//! mingling = { version = "...", features = ["async"] }
+//! ```
+//!
+//! # How to Run
+//! ```bash
+//! cargo run --manifest-path ./examples/example-async/Cargo.toml -- hello World
+//! ```
+
+use mingling::{
+ macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
+ marker::NextProcess,
+};
+
+dispatcher!("hello", HelloCommand => HelloEntry);
+
+// Use Tokio async runtime
+#[tokio::main]
+async fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(HelloCommand);
+
+ // Run program
+ program.exec().await;
+}
+
+pack!(Hello = String);
+
+// You can freely use async / non-async functions to declare your Chain
+
+#[chain]
+// fn parse_name(prev: HelloEntry) -> NextProcess {
+async fn parse_name(prev: HelloEntry) -> NextProcess {
+ let name = prev.first().cloned().unwrap_or_else(|| "World".to_string());
+ Hello::new(name).to_render()
+}
+
+// For renderers, you can still only use synchronous functions
+#[renderer]
+fn render_hello_who(prev: Hello) {
+ r_println!("Hello, {}!", *prev);
+}
+
+gen_program!();