Neon: Automatic conversion of types between Rust and Javascript via serde

Created on 28 Jun 2017  路  5Comments  路  Source: neon-bindings/neon

Hi!

I've been using neon successfully in my side-project and everything works great, except that there's quite a bit of boilerplate required to marshal types between Rust and JS. I wonder if this can be automated away with the help of serde. It's sort of possible today, if you literally serialize and deserialize JSON from strings, but it would be cool to be able to use serde with JsValues directly!

Most helpful comment

I've been working on this https://github.com/GabrielCastro/neon-serde , It's got most of the uses cases covered, but is lacking in documentation

All 5 comments

I've been working on this https://github.com/GabrielCastro/neon-serde , It's got most of the uses cases covered, but is lacking in documentation

Thanks a lot, @GabrielCastro! I've used it in my project and it worked great!

Here's some feedback:

1) Actually, I find the example in Readme the most helpful, so the docs are great to me 馃憤

2) Looks like it is in theory possible to eliminate some boilerplate from argument parsing, so that instead of doing call.arguments.require(scope, 0)?.check::<JsInteger>(), one can use something like

let (foo, bar, baz): (i32, String, MyType) = call.arguments.deserialize()?

3) to_value function is constrained only to the RootScope, and not T: Scope, so it's impossible to use it, for example, inside Task's complete method.

@matklad Thanks for the feed back, I've made a couple issues from your comments. Please feel free to comment there

Just stumbled upon this.

I think in principle it should be possible for neon to implement a function wrapper with generic implementations for:

fn<R>() -> R
fn<A, R>(A) -> R
fn<A, B, R>(A, B) -> R
fn<A, B, C, R>(A, B, C) -> R
...
fn<A, B, C, D, E, F, G, H, R>(A, B, C, D, E, F, G, H) -> R

With generics being:

where
    A: Deserialize,
    B: Deserialize,
    C: Deserialize,
    D: Deserialize,
    E: Deserialize,
    F: Deserialize,
    G: Deserialize,
    H: Deserialize,
    R: Serialize,

With that in place the end user can just write a fn foo(String, i32) -> String or anything really, without writing any JsValue glue code by hand. With some inlining and ingenuity I don't see why this couldn't also be completely zero cost.

Turns out doing it with traits as I intended wasn't possible (at least not without some nightly features to create a 0-type structs that implement Fn and then pass those as a generic parameter...). Macros do, however, and it looks pretty clean: https://github.com/GabrielCastro/neon-serde/pull/11

Was this page helpful?
0 / 5 - 0 ratings

Related issues

usagi picture usagi  路  3Comments

manuel2258 picture manuel2258  路  5Comments

chpio picture chpio  路  6Comments

hyperfekt picture hyperfekt  路  4Comments

ghost picture ghost  路  4Comments