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

Accessing JS arrays from Rust #45

Closed
TilBlechschmidt opened this issue Dec 22, 2017 · 5 comments
Closed

Accessing JS arrays from Rust #45

TilBlechschmidt opened this issue Dec 22, 2017 · 5 comments

Comments

@TilBlechschmidt
Copy link

TilBlechschmidt commented Dec 22, 2017

According to the documentation it is possible to convert a Rust Vector into JavaScript arrays.

I wasn't able to find any documentation on how to do that the other way around e.g. iterate a JS array or possibly convert it into a Rust vector if possible (obviously requires that all array entries can be converted to the same type).

Trying to pass an Array to Rust

fn print_vec(vec: stdweb::Array) {
    let x: Vec<stdweb::Value> = Vec::from(&vec);
    println!("This is a vector: {:?}", x);
}

fn main() {
    stdweb::initialize();

    js! {
        Module.exports.printArray = @{print_vec};
    }
}
const vector = require( './target/wasm32-unknown-unknown/release/vector.js' );
vector..printArray([1, 2, 3, 4]);

I get the following error twice:

error[E0277]: the trait bound `stdweb::Array: stdweb::unstable::TryFrom<stdweb::Value>` is not satisfied
  --> src/main.rs:21:5
   |
21 | /     js! {
22 | |         Module.exports.printArray = @{print_vec};
23 | |     }
   | |_____^ the trait `stdweb::unstable::TryFrom<stdweb::Value>` is not implemented for `stdweb::Array`

   = note: required because of the requirements on the impl of `stdweb::private::JsSerializableOwned` for `stdweb::private::Newtype<(stdweb::webcore::serialization::FunctionTag, (stdweb::Array,)), fn(stdweb::Array) {print_vec}>`
   = note: required by `stdweb::private::JsSerializableOwned::memory_required_owned`
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
@koute
Copy link
Owner

koute commented Dec 22, 2017

You can convert either a Value or an Array into a Vec<T> using the TryFrom trait (Specifically this and this impl.)

This should work:

let array: Vec< String > = js! { return ["foo", "bar"]; }.try_into().unwrap();

You can't currently access a JavaScript array in Rust without converting it to a vector first.

@TilBlechschmidt
Copy link
Author

TilBlechschmidt commented Dec 22, 2017

Thanks for the quick answer!

I wasn't aware of the .try_into() function! Looks good.

The above error seems to persist though:

error[E0277]: the trait bound `std::vec::Vec<std::string::String>: std::convert::From<stdweb::Value>` is not satisfied
  --> src/main.rs:25:63
   |
25 |     let array: Vec< String > = js! { return ["foo", "bar"]; }.try_into().unwrap();
   |                                                               ^^^^^^^^ the trait `std::convert::From<stdweb::Value>` is not implemented for `std::vec::Vec<std::string::String>`
   |
   = help: the following implementations were found:
             <std::vec::Vec<stdweb::Value> as std::convert::From<&'a stdweb::Array>>
             <std::vec::Vec<u8> as std::convert::From<&'a stdweb::web::TypedArray<u8>>>
             <std::vec::Vec<stdweb::Value> as std::convert::From<&'a mut stdweb::Array>>
             <std::vec::Vec<stdweb::Value> as std::convert::From<stdweb::Array>>
           and 10 others
   = note: required because of the requirements on the impl of `std::convert::TryFrom<stdweb::Value>` for `std::vec::Vec<std::string::String>`
   = note: required because of the requirements on the impl of `std::convert::TryInto<std::vec::Vec<std::string::String>>` for `stdweb::Value`

Seems like I am missing a piece of the puzzle there ...

@koute
Copy link
Owner

koute commented Dec 22, 2017

You're using the wrong TryFrom. (:

The std::convert::TryFrom is still unstable, so we have our own in stdweb::unstable::TryFrom/stdweb::unstable::TryInto.

@TilBlechschmidt
Copy link
Author

That did the trick!
Thanks for your swift help 😃

This might as well be the next big thing for Rust ;)
Props to you good sir and this project! Keep up the amazing work!

@koute
Copy link
Owner

koute commented Dec 22, 2017

No problem; thanks. (:

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

No branches or pull requests

2 participants