1 unstable release
0.9.0 | May 9, 2024 |
---|
#1367 in Procedural macros
518 downloads per month
Used in 2 crates
(via winter-prover)
10KB
133 lines
Winter maybe-async
This crate contains a maybe_async
proc macro that abstracts away sync/async. It is heavily based on maybe-async
.
The maybe_async
macro will generate a synchronous or asynchronous version of the trait it is marking. To generate the asynchronous version, enable the async
feature on the crate. If the async
feature is off, the synchronous version will be generated. For example,
#[maybe_async]
trait ExampleTrait {
async fn say_hello(&self) {
let hello = self.get_hello().await;
println!("{}", hello);
}
async fn get_hello(&self) -> String {
"hello".into()
}
}
// Generate code when `async` feature is turned ON
#[async_trait]
trait ExampleTrait {
async fn say_hello(&self) {
let hello = self.get_hello().await;
println!("{}", hello);
}
async fn get_hello(&self) -> String {
"hello".into()
}
}
// Generate code when `async` feature is turned OFF
trait ExampleTrait {
fn say_hello(&self) {
let hello = self.get_hello();
println!("{}", hello);
}
fn get_hello(&self) -> String {
"hello".into()
}
}
where #[async_trait]
is the proc macro provided by the async-trait
crate. Notice how #[maybe_async]
took care of removing the .await
in the synchronous version of say_hello()
.
#[maybe_async]
can also mark impl
blocks in a similar manner. For example,
struct ExampleStruct;
#[maybe_async]
impl ExampleTrait for ExampleStruct {
async fn say_hello(&self) {
println!("hello!");
}
}
// Generate code when `async` feature is turned ON
#[async_trait]
impl ExampleTrait for ExampleStruct {
async fn say_hello(&self) {
println!("hello!");
}
}
// Generate code when `async` feature is turned OFF
impl ExampleTrait for ExampleStruct {
fn say_hello(&self) {
println!("hello!");
}
}
Finally, #[maybe_async]
can be used on fn
items, which works in an analogous way to the previous examples.
#[maybe_async]
async fn say_hello() {
// ...
}
License
This project is MIT licensed.
Dependencies
~300–760KB
~18K SLoC