wtfm_rs_tokio/lib.rs
1#![doc(html_playground_url = "https://play.rust-lang.org/")]
2//! # Introduction
3//! This is the tokio version of <https://wtfm-rs.github.io/doc/wtfm_rs/>.
4//! ## Why do we need a separate repo?
5//! We want to use [tokio] runtime for async functions.
6//! Our minimum `Cargo.toml` only pulls in [tokio] crate
7//! so that we can write tests and deep dive into the dependencies.
8//! ```toml
9//! [package]
10//! name = "wtfm-rs-tokio"
11//! version = "0.1.0"
12//! edition = "2024"
13//!
14//! [dependencies]
15//! tokio = { version = "1.49.0", features = ["full"] }
16//! ```
17//! ```text
18//! % cargo tree
19//! └── tokio v1.49.0
20//! ├── bytes v1.11.0
21//! ├── libc v0.2.180
22//! ├── mio v1.1.1
23//! │ └── libc v0.2.180
24//! ├── parking_lot v0.12.5
25//! │ ├── lock_api v0.4.14
26//! │ │ └── scopeguard v1.2.0
27//! │ └── parking_lot_core v0.9.12
28//! │ ├── cfg-if v1.0.4
29//! │ ├── libc v0.2.180
30//! │ └── smallvec v1.15.1
31//! ├── pin-project-lite v0.2.16
32//! ├── signal-hook-registry v1.4.8
33//! │ ├── errno v0.3.14
34//! │ │ └── libc v0.2.180
35//! │ └── libc v0.2.180
36//! ├── socket2 v0.6.2
37//! │ └── libc v0.2.180
38//! └── tokio-macros v2.6.0 (proc-macro)
39//! ├── proc-macro2 v1.0.106
40//! │ └── unicode-ident v1.0.22
41//! ├── quote v1.0.44
42//! │ └── proc-macro2 v1.0.106 (*)
43//! └── syn v2.0.114
44//! ├── proc-macro2 v1.0.106 (*)
45//! ├── quote v1.0.44 (*)
46//! └── unicode-ident v1.0.22
47//! ```
48//! ## current thread
49//! ```
50//! tokio::runtime::Builder::new_current_thread()
51//! .enable_all()
52//! .build()
53//! .unwrap()
54//! .block_on(async {
55//! assert!(true);
56//! })
57//!
58//! ```
59//! ## multi-thread
60//! ```
61//! tokio::runtime::Builder::new_multi_thread()
62//! .enable_all()
63//! .build()
64//! .unwrap()
65//! .block_on(async {
66//! assert!(true);
67//! })
68//!
69//! ```
70
71//! ## [tokio::main]
72//! ```
73//! use tokio::process::Command;
74//!
75//! async fn hello_world() -> String {
76//! let output = Command::new("echo").arg("Hello,").arg("world!").output();
77//! let output = output.await.expect("No such file or directory");
78//! String::from_utf8(output.stdout).expect("Format error")
79//! }
80
81//! #[tokio::main]
82//! async fn main() {
83//! assert_eq!(hello_world().await, "Hello, world!\n");
84//! }
85//! ```
86//!
87#[cfg(test)]
88#[tokio::test]
89async fn current_thread_test() {
90 assert!(true);
91}
92
93#[test]
94fn current_thread_expanded_test() {
95 tokio::runtime::Builder::new_current_thread()
96 .enable_all()
97 .build()
98 .unwrap()
99 .block_on(async {
100 assert!(true);
101 })
102}
103
104#[tokio::test(flavor = "multi_thread")]
105async fn multi_thread_test() {
106 assert!(true);
107}
108
109#[test]
110fn multi_thread_expanded_test() {
111 tokio::runtime::Builder::new_multi_thread()
112 .enable_all()
113 .build()
114 .unwrap()
115 .block_on(async {
116 assert!(true);
117 })
118}