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

Instructions for using with wasm #382

Closed
liquidscorpio opened this issue Dec 30, 2019 · 8 comments
Closed

Instructions for using with wasm #382

liquidscorpio opened this issue Dec 30, 2019 · 8 comments

Comments

@liquidscorpio
Copy link

While there are some issues/PRs (#243, #284, etc.) relating to wasm, there is no specific section in documentation properly detailing steps to use it with something like wasm-pack.

For now (v0.4.10), the following code fails to compile:

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;
use chrono::{NaiveDate};

#[wasm_bindgen]
pub struct Request {
    date: NaiveDate,
}

The error message is:

   | #[wasm_bindgen]
   | ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::FromWasmAbi` 
   | is not implemented for `chrono::naive::date::NaiveDate`
@quodlibetor
Copy link
Contributor

Ah, yeah I actually have almost no experience with wasm other than getting the tests running. A PR adding wasm instructions (or pointing to them) would be most welcome!

@liquidscorpio
Copy link
Author

If I do manage to get going, I will send in a PR documenting the same.
Still scratching my head for now in search of a work around related to the above issue. 😅

@quodlibetor
Copy link
Contributor

You do need to build chrono with the wasmbind feature and (I believe) build using the wasm-pack tool, see the ci/travis.sh script, look for wasmbind.

@liquidscorpio
Copy link
Author

This is the declaration I am using in the Cargo.toml file.

[dependencies.chrono]
version = "0.4"
default-features = false
features = ["wasmbind", "js-sys"]

On the other hand, the error message seems to be correct as an impl for wasm_bindgen::convert::FromWasmAbi is nowhere to be found in the code base. I'll dig further on how other datetime libs (outside of Rust) are handling this.

@da-x
Copy link

da-x commented Mar 28, 2020

A way to get system time in wasm until both std's SystemTime and chrono are fixed, is the following, using stdweb:

std::time::Duration::from_millis(stdweb::web::Date::now() as u64)

@liquidscorpio
Copy link
Author

@da-x That is one good way. My idea though was to leverage chrono's convenient date manipulation APIs.

@jonknapp
Copy link

In case it helps anyone else who stumbles here from the web like I did, I changed my wasm-pack built app's dependencies from:

[dependencies]
chrono = { version = "0.4", features = ["serde"] }

to

[dependencies.chrono]
version = "0.4"
features = ["serde", "wasmbind"]

and I'm able to serialize / deserialize DateTime<Utc> with:

use serde_derive::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
stuct MyStruct {
    last_saved_at: DateTime<Utc>,
    other_field: usize,
}

Prior to adding the wasmbind feature, it would panic during runtime.

@pitdicker
Copy link
Collaborator

The wasmbind feature is enabled by default since #771.

So since then it should 'just work'?

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

5 participants