blob: e75cb4526597a199603f3ec556038ae262db65d6 (
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# might_be_async
> A proc macro that gen both async & sync fn, toggle via feature flag.
## Installation
Add the dependency in `Cargo.toml`:
```toml
[dependencies]
might_be_async = "0.1.0"
```
Or use the `cargo add` command:
```bash
cargo add might_be_async
```
# Usage
First, choose a feature name for your async toggle. By default `might_be_async` uses the feature name `"async"`, but you can use any name you like.
```toml
# Cargo.toml
[package.metadata.might_be_async]
default_feature_name = "async" # Default
[dependencies]
might_be_async = "0.1.0"
```
Now you can annotate your functions with `#[func]`:
```rust
#[might_be_async::func]
fn greet(name: &str) -> String {
format!("Hello, {name}!")
}
```
When the `async` feature is **off**, `greet` remains a plain synchronous `fn`.
When the `async` feature is **on**, `greet` becomes an `async fn` — its signature changes to `async fn greet(name: &str) -> String`.
## Calling annotated functions
If one `#[func]` function calls another, use the `invoke!` macro to handle
the `.await` automatically:
```rust
#[might_be_async::func]
fn double(x: i32) -> i32 {
x * 2
}
#[might_be_async::func]
fn compute(a: i32, b: i32) -> i32 {
let sum = a + b;
might_be_async::invoke!(double(sum)) // adds .await in async mode, does nothing in sync mode
}
```
## Feature-specific logic with `select!`
Use `select!` to write code that differs between sync and async modes:
```rust
#[might_be_async::func]
fn load_data() -> Vec<u8> {
might_be_async::select!("async" => fetch_async().await else ! => fetch_sync())
}
async fn fetch_async() -> Vec<u8> {
// ...
vec![]
}
fn fetch_sync() -> Vec<u8> {
// ...
vec![]
}
```
When the feature `"async"` is active, the first branch runs. When it's not,
the second branch (marked with `!`) runs.
## Switching modes
Run in sync mode (default):
```bash
cargo build
cargo run
```
Run in async mode:
```bash
cargo build --features async
cargo run --features async
```
## License
MIT OR Apache-2.0"
|