Skip to content
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

Merged
merged 6 commits into from
Jun 11, 2024

Conversation

phklive
Copy link

@phklive phklive commented Jun 10, 2024

In this PR I propose the swap from the maybe-async crate to maybe-async-await crate.

Instead of relying on maybe-async-rs and async-trait we have implemented our own conditional async/await macros.

These #[maybe-async] and maybe_await!() macros enable easy conditional asynchronicity and can be used as a standalone crate.

@facebook-github-bot
Copy link

Hi @phklive!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@phklive phklive marked this pull request as ready for review June 11, 2024 07:30
@phklive phklive requested a review from irakliyk June 11, 2024 07:30
Comment on lines +9 to 24
```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() {
// ...
}
```
Copy link
Collaborator

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() {
    // ...
}

Comment on lines +30 to 43
```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()
}
```
Copy link
Collaborator

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()
}

Comment on lines +10 to +15
/// 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 {
Copy link
Collaborator

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() {
///     // ...
/// }
/// ```

Comment on lines +41 to +46
/// 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 {
Copy link
Collaborator

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.

Copy link
Collaborator

@irakliyk irakliyk left a 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.

@irakliyk irakliyk merged commit 39bca59 into facebook:next Jun 11, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants