-
Notifications
You must be signed in to change notification settings - Fork 282
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): Extractors #1024
feat(neon): Extractors #1024
Conversation
Extractors allow quickly getting JavaScript arguments as Rust values. In addition to immediate ergonomics and performance improvements, extractors set a foundation for future proc macros.
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.
Mostly a bunch of questions, so maybe it won't require any changes in the end, but I'm marking it as "request changes" just to talk through the comments.
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.
LGTM -- this one is super exciting, ship it!
Extractors allow quickly getting JavaScript arguments as Rust values. In addition to immediate ergonomics and performance improvements, extractors set a foundation for future proc macros.
The Neon API surface area increases by two traits:
TryFromJs
FromArgs
TryFromJs
The
TryFromJs
trait provides a standardized way of extracting aRust
value from aJsValue
. It is initiallySealed
while we determine if changes are needed. In a future release, we will likely make it available for users to implement.FromArgs
The
FromArgs
trait is an implementation detail and will remainSealed
. It is implemented by a macro for tuples up to size 32.Extractors
TryFromJs
is implemented for many standard library values (f64
,String
) and wrapper types (Option
). It also has blanket implementations forHandle
andNeonResult
. Additionally, I introduce the concept of an "extractor". The idea is inspired byaxum
. These extractors allow for specialization where extracted types would otherwise overlap. For example, getting anf64
from aDate
.Most extractors are implemented using the
sys
crate directly in order to avoid duplicate type checks. In the future, we will likely want to convert existing functions to use theTryFromJs
implementations.Json
As found previously, serialization to JSON is nearly always faster than manually reading JavaScript objects. Working with JSON, thanks to
serde
, is also much easier. Community crates likeneon-serde
have demonstrated a high demand for this functionality.The
Json
extractor leveragesserde
andserde_json
to convert to a Rust type. The ✨ magic ✨ is that it doesn't require the value to already be stringified on the JavaScript side. It leverage's Neon's newLocalKey
functionality for fast access toJSON.stringify
.JSON.stringify
andserde_json::from_str
are implicitly called when extracting.