Giter Site home page Giter Site logo

Accessing JS arrays from Rust about stdweb HOT 5 CLOSED

koute avatar koute commented on May 12, 2024
Accessing JS arrays from Rust

from stdweb.

Comments (5)

koute avatar koute commented on May 12, 2024

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.

from stdweb.

TilBlechschmidt avatar TilBlechschmidt commented on May 12, 2024

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 ...

from stdweb.

koute avatar koute commented on May 12, 2024

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.

from stdweb.

TilBlechschmidt avatar TilBlechschmidt commented on May 12, 2024

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!

from stdweb.

koute avatar koute commented on May 12, 2024

No problem; thanks. (:

from stdweb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.