-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swapped maybe-async
for maybe-async-await
#283
Conversation
Hi @phklive! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
```rust | ||
// Adding `maybe_async` to trait functions | ||
trait ExampleTrait { | ||
async fn say_hello(&self) { | ||
let hello = self.get_hello().await; | ||
|
||
println!("{}", hello); | ||
} | ||
#[maybe_async] | ||
fn say_hello(&self); | ||
|
||
async fn get_hello(&self) -> String { | ||
"hello".into() | ||
} | ||
#[maybe_async] | ||
fn get_hello(&self) -> String; | ||
} | ||
|
||
// 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() | ||
} | ||
// Adding `maybe_async` to regular functions | ||
#[maybe_async] | ||
fn hello_world() { | ||
// ... | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably add how the output would look like if async
feature is on. Something like:
When the async
feature is enabled, the above code will be transformed into:
async trait ExampleTrait {
async fn say_hello(&self);
async fn get_hello(&self) -> String;
}
async fn hello_world() {
// ...
}
```rust | ||
#[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!"); | ||
} | ||
} | ||
fn hello_world() { | ||
// Adding `maybe_await` to an expression | ||
let w = maybe_await!(world()); | ||
|
||
// Generate code when `async` feature is turned OFF | ||
impl ExampleTrait for ExampleStruct { | ||
fn say_hello(&self) { | ||
println!("hello!"); | ||
} | ||
println!("hello {}", w); | ||
} | ||
|
||
``` | ||
|
||
Finally, `#[maybe_async]` can be used on `fn` items, which works in an analogous way to the previous examples. | ||
|
||
```rs | ||
#[maybe_async] | ||
async fn say_hello() { | ||
// ... | ||
fn world() -> String { | ||
"world".to_string() | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the last comment, I'd add. something like this after this:
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()
}
/// maybe_async procedural attribute macro | ||
/// | ||
/// Parses a function (regular or trait) and conditionally adds the `async` keyword | ||
/// depending on the `async` feature flag being enabled. | ||
#[proc_macro_attribute] | ||
pub fn maybe_async(_args: TokenStream, input: TokenStream) -> TokenStream { | ||
let mut item = parse_macro_input!(input as Item); | ||
|
||
let token = if cfg!(feature = "async") { | ||
convert_async(&mut item) | ||
} else { | ||
convert_sync(&mut item) | ||
}; | ||
|
||
token.into() | ||
} | ||
|
||
fn convert_sync(input: &mut Item) -> TokenStream2 { | ||
match input { | ||
Item::Impl(item) => { | ||
for inner in &mut item.items { | ||
if let ImplItem::Fn(ref mut method) = inner { | ||
if method.sig.asyncness.is_some() { | ||
method.sig.asyncness = None; | ||
} | ||
} | ||
pub fn maybe_async(_attr: TokenStream, input: TokenStream) -> TokenStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would get rid of line 10 and add an example (same as in readme):
/// Parses a function (regular or trait) and conditionally adds the `async` keyword
/// depending on the `async` feature flag being enabled.
///
/// For example:
/// ```ignore
/// trait ExampleTrait {
/// #[maybe_async]
/// fn say_hello(&self);
///
/// #[maybe_async]
/// fn get_hello(&self) -> String;
/// }
///
///
/// #[maybe_async]
/// fn hello_world() {
/// // ...
/// }
/// ```
/// When the `async` feature is enabled, will be transformed into:
/// ```ignore
/// trait ExampleTrait {
/// async fn say_hello(&self);
///
/// async fn get_hello(&self) -> String;
/// }
///
///
/// async fn hello_world() {
/// // ...
/// }
/// ```
/// maybe_await procedural macro | ||
/// | ||
/// Parses an expression and conditionally adds the `.await` keyword at the end of it | ||
/// depending on the `async` feature flag being enabled. | ||
#[proc_macro] | ||
pub fn maybe_await(input: TokenStream) -> TokenStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment as the one above re adding an example and removing line 41.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you! I'll make some of the remaining changes directly.
In this PR I propose the swap from the
maybe-async
crate tomaybe-async-await
crate.Instead of relying on
maybe-async-rs
andasync-trait
we have implemented our own conditionalasync/await
macros.These
#[maybe-async]
andmaybe_await!()
macros enable easy conditional asynchronicity and can be used as a standalone crate.