Crates.io | winter-maybe-async |
lib.rs | winter-maybe-async |
version | 0.11.0 |
source | src |
created_at | 2024-05-09 21:23:02.506018 |
updated_at | 2024-11-24 22:39:51.958534 |
description | sync/async macro for winterfell |
homepage | |
repository | https://github.com/facebook/winterfell |
max_upload_size | |
id | 1235487 |
size | 11,206 |
This crate contains the maybe_async
procedural attribute macro and the maybe_await
procedural macro which abstract away Rust sync/async.
The maybe_async
macro will conditionally add the async
keyword to a function it marks depending on the async
feature being enabled. 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,
// Adding `maybe_async` to trait functions
trait ExampleTrait {
#[maybe_async]
fn say_hello(&self);
#[maybe_async]
fn get_hello(&self) -> String;
}
// Adding `maybe_async` to regular functions
#[maybe_async]
fn hello_world() {
// ...
}
When the async
feature is enabled, the above code will be transformed into:
trait ExampleTrait {
async fn say_hello(&self);
async fn get_hello(&self) -> String;
}
async fn hello_world() {
// ...
}
To compliment maybe_async
we also have the maybe_await
procedural macro that conditionally adds the .await
keyword to the end of an expression depending on the async
feature flag.
#[maybe_async]
fn hello_world() {
// Adding `maybe_await` to an expression
let w = maybe_await!(world());
println!("hello {}", w);
}
#[maybe_async]
fn world() -> String {
"world".to_string()
}
When the async
feature is enabled, the above code will be transformed into:
async fn hello_world() {
let w = world().await;
println!("hello {}", w);
}
async fn world() -> String {
"world".to_string()
}
The maybe_async_trait
macro can be applied to traits, and it will conditionally add the async
keyword to trait methods annotated with #[maybe_async]
, depending on the async feature being enabled. It also applies #[async_trait::async_trait(?Send)]
to the trait or impl block when the async feature is on.
For example:
// Adding `maybe_async_trait` to a trait definition
#[maybe_async_trait]
trait ExampleTrait {
#[maybe_async]
fn hello_world(&self);
fn get_hello(&self) -> String;
}
// Adding `maybe_async_trait` to an implementation of the trait
#[maybe_async_trait]
impl ExampleTrait for MyStruct {
#[maybe_async]
fn hello_world(&self) {
// ...
}
fn get_hello(&self) -> String {
// ...
}
}
When async
is set, it gets transformed into:
#[async_trait::async_trait(?Send)]
trait ExampleTrait {
async fn hello_world(&self);
fn get_hello(&self) -> String;
}
#[async_trait::async_trait(?Send)]
impl ExampleTrait for MyStruct {
async fn hello_world(&self) {
// ...
}
fn get_hello(&self) -> String {
// ...
}
}
This project is MIT licensed.