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

feat(neon): Add tokio async runtime support #1055

Merged
merged 2 commits into from
Sep 20, 2024
Merged

Conversation

kjvalencik
Copy link
Member

@kjvalencik kjvalencik commented Jul 5, 2024

This PR adds support for exporting async fn and future return fn in #[neon::export].

#[neon::export]
async fn example1(a: f64, b: f64) -> f64 {
    a + b
}

#[neon::export(async)]
fn example2(a: f64, b: f64) -> impl Future<Output = f64> {
    async move {
        a + b
    }
}

It is intentionally very conservative in semver guarantees. It requires registering a global future executor. The executor must implement the private Runtime trait. Currently, only a tokio implementation is included. Additionally, Future trait bounds are strict enough to allow for multi-threaded executors.

As we see demand, we can add support for additional executors (e.g., async-std) and possibly even single threaded executors (#[neon::export(?Sync)]) or a built-in libuv driven executor. The eventual goal is to make the Runtime trait public so that users can bring their own executor.

@kjvalencik kjvalencik changed the title feat(neon): At tokio async runtime support feat(neon): Add tokio async runtime support Jul 8, 2024
@kjvalencik kjvalencik force-pushed the kv/async-runtime branch 2 times, most recently from 3da8a9e to 3cfbd7a Compare July 16, 2024 19:43
@kjvalencik kjvalencik force-pushed the kv/async-runtime branch 2 times, most recently from c17a9ad to 394b695 Compare September 10, 2024 16:37
@kjvalencik kjvalencik marked this pull request as ready for review September 13, 2024 16:33
crates/neon/src/macros.rs Outdated Show resolved Hide resolved
@kjvalencik kjvalencik force-pushed the kv/async-runtime branch 2 times, most recently from e1f61ca to 1cc69a4 Compare September 16, 2024 21:33
Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small changes, but nothing major. TBH I'm a little worried my lack of expertise at tokio and async means my review might be a little superficial. But the API is so easy to understand and fits so nicely with the #[export] macro syntax. This is a really exciting addition.

crates/neon/src/context/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/lib.rs Show resolved Hide resolved
crates/neon/src/lib.rs Show resolved Hide resolved
crates/neon/src/lifecycle.rs Outdated Show resolved Hide resolved
crates/neon/src/macro_internal/futures.rs Outdated Show resolved Hide resolved
crates/neon/src/sys/bindings/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/extract/private.rs Outdated Show resolved Hide resolved
test/napi/src/lib.rs Outdated Show resolved Hide resolved
crates/neon/src/macros.rs Outdated Show resolved Hide resolved
@kjvalencik kjvalencik changed the base branch from main to kv/clippy September 19, 2024 21:28
@kjvalencik kjvalencik force-pushed the kv/async-runtime branch 2 times, most recently from ce2eed4 to 40e1e3d Compare September 19, 2024 21:35
Base automatically changed from kv/clippy to main September 20, 2024 11:58
Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@kjvalencik kjvalencik merged commit 68c48ef into main Sep 20, 2024
9 checks passed
@kjvalencik kjvalencik deleted the kv/async-runtime branch September 20, 2024 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants