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

fix(neon): Relax lifetime constraints on With and provide helper for forcing HRTB with JS values #1086

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kjvalencik
Copy link
Member

@kjvalencik kjvalencik commented Dec 17, 2024

With is primarily intended as a helper for providing a thunk executed on the main thread after an asynchronous operation. However, it's not currently possible to return JavaScript values due to unnecessarily restrictive lifetimes.

// DOES NOT COMPILE
use neon::types::extract::{TryIntoJs, With};

#[neon::export(task)]
fn example() -> impl for<'cx> TryIntoJs<'cx> {
    With(|cx| cx.number(42.0))
}

These changes relax thoe bounds, but unfortunately causes Rust to no longer correctly infer the correct HRTB. As a workaround, an extract::with helper is provided.

use neon::types::extract::{self, TryIntoJs};

// Compiles :)
#[neon::export(task)]
fn example() -> impl for<'cx> TryIntoJs<'cx> {
    extract::with(|cx| Ok(cx.number(42.0)))
}

I could not find a way to make a helper that works with both JsResult and Rust data without a lifetime. Since someone returning Rust data always has the option to call .try_into_js(cx), I chose to keep the API simple by only providing the one helper.

Open Questions

  • Is it possible to implement a helper that supports generic return types of O: TryIntoJs<'cx> that aren't 'static?
  • Should we make With private? The with type would return an opaque impl for<'cx> TryIntoJs<'cx>. This could simplify the API.

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.

1 participant