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

Feature/wasm #351

Merged
merged 8 commits into from Dec 1, 2018
Merged

Feature/wasm #351

merged 8 commits into from Dec 1, 2018

Conversation

zrzka
Copy link
Contributor

@zrzka zrzka commented Nov 27, 2018

Description

This PR adds two new features:

  • stdweb
  • wasm-bindgen

These features are kind of passthrough features, because they do nothing in the uuid crate itself. They're just passed to the rand crate to make the uuid crate working for the wasm32-unknown-unknown target.

Motivation

I'm unable to generate random UUID (v4) when this crate is compiled for the wasm32-unknown-unknown target.

Tests

I just added these features ...

- cargo test --features "v3"
- cargo test --features "v3 stdweb"
- cargo test --features "v3 wasm-bindgen"

... for all v3 & v4 & v5 (rand crate is used in all these features) to the script section in the .travis.yml.

Not sure if it makes sense, but it can demonstrate that it's buildable at least.

I don't think that more tests are required unless you'd like to bring the whole wasm-bindgen, wasm-pack, ... machinery here. And it has no sense to do it, because goal of this PR is not to publish uuid-rs NPM package, just add the ability to compile & use it from the wasm32-unknown-unknown target.

Related Issue(s)

Manual tests

My Cargo.toml:

[dependencies]
uuid = { features = ["v4"], git = "https://github.com/zrzka/uuid.git", branch = "feature/wasm" }

[target.wasm32-unknown-unknown.dependencies]
uuid = { features = ["wasm-bindgen"], git = "https://github.com/zrzka/uuid.git", branch = "feature/wasm" }

My index.js:

const bt = require('balena-temen');

console.log(
    bt.evaluate({
        "id": {
            "$$eval": "uuidv4()"
        }
    })
);

uuidv4() implementation.

Output:

{ id: 'cefa2919-ff48-48ef-a231-e13697e23ed2' }

@Dylan-DPC-zz
Copy link
Member

Thanks. But I don't think it is a good idea to depend on docs for messages that it is supported only on certain targets with a feature gate. I'm looking for a better way of doing this.

@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

Agreed, I don't like it too. Maybe hide new_v4() & friends ...

    #[cfg(any(
        not(target_arch = "wasm32"),
        all(
            target_arch = "wasm32",
            any(feature = "stdweb", feature = "wasm-bindgen")
        )
    ))]
    pub fn new_v4() -> Self { ... }

... , remove # Panics docstring and add some note to the lib.rs what to do with the wasm32?

@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

Did update this PR and gated all v3, v4, v5 modules behind features. If you target wasm32-... and you forget to enable either stdweb or wasm-bindgen feature, it fails to compile:

warning: unused manifest key: package.maintenance
   Compiling rand v0.6.1
   Compiling uuid v0.7.1 (https://github.com/zrzka/uuid.git?branch=feature/wasm#0ccfd12f)
   Compiling balena-temen v0.0.20 (/Users/robertvojta/Work/balena/balena-temen)
error[E0599]: no function or associated item named `new_v4` found for type `uuid::Uuid` in the current scope
  --> src/builtin/function/uuidv4.rs:10:22
   |
10 |     Ok(Value::String(Uuid::new_v4().to_hyphenated().to_string()))
   |                      ^^^^^^^^^^^^ function or associated item not found in `uuid::Uuid`

src/lib.rs Outdated Show resolved Hide resolved
wasm-pack.log Outdated Show resolved Hide resolved
Signed-off-by: Robert Vojta <rvojta@me.com>
Signed-off-by: Robert Vojta <rvojta@me.com>
Signed-off-by: Robert Vojta <rvojta@me.com>
Signed-off-by: Robert Vojta <rvojta@me.com>
@Dylan-DPC-zz
Copy link
Member

Currently this doesn't test the crate on wasm-* but just the features. We need to add another build to the CI so that it will run all the tests (or just the feature-gated ones) on wasm.

@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

@Dylan-DPC can you elaborate on tests pls? There're two options:

a) make a crate buildable for wasm32-... and that's it, test the features only,
b) make a crate buildable for wasm32-... and provide "NPM package" (JS wrapper) and test it with the wasm-pack test ..., something like this

If a) is the right answer then I don't understand what do you mean with tests. Because you can't do things like cargo test --target wasm32-unknown-unknown --features "v5 stdweb" for example.

Also I don't think b) needs to be added.

Signed-off-by: Robert Vojta <rvojta@me.com>
@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

rand is doing this for WASM, it takes quite a long time and the output is:

test result (async): ok. 0 passed; 0 failed
test result (async): ok. 0 passed; 0 failed

Signed-off-by: Robert Vojta <rvojta@me.com>
@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

Did add builds for the wasm32 arch with features as a minimum.

@KodrAus
Copy link
Member

KodrAus commented Nov 27, 2018

Thanks for the PR @zrzka. Personally I'm not a fan of adding transitive stdweb or wasm-bindgen features to uuid for the sake of rand. It just passes the problem of figuring out feature gates upstream to the next consumer of uuid.

Right now, I think depending on rand alongside uuid in your project with one of these features set is a better way to go.

If we're going to support random uuids in wasm then I think we should do it without needing any special feature gates.

@Dylan-DPC-zz
Copy link
Member

@Dylan-DPC can you elaborate on tests pls? There're two options:

a) make a crate buildable for wasm32-... and that's it, test the features only,
b) make a crate buildable for wasm32-... and provide "NPM package" (JS wrapper) and test it with the wasm-pack test ..., something like this

This will test only the features, it won't test whether uuid works on the wasm architectures or not, which could lead to false truths.

@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

@KodrAus not sure if you can do it without feature gates now, because there's stdweb and wasm-bindgen, which are different things / approaches. That doesn't mean I like it, because it's weird and it looks like that it's fixing an issue which lies elsewhere.

Anyway, I'm fine with adding rand as a dependency and copy & pasting new_v4.

I'm going to close this PR, trash branch. Decide if you'd like to keep #350 open and if not, feel free to close it.

@zrzka zrzka closed this Nov 27, 2018
@zrzka zrzka deleted the feature/wasm branch November 27, 2018 13:29
@Dylan-DPC-zz
Copy link
Member

@KodrAus I don't think you can do it without the feature gates, and since the rand features are different depending on whether they want stdweb or bindgen. I don't see another way of doing this at this point.

@zrzka
Copy link
Contributor Author

zrzka commented Nov 27, 2018

@Dylan-DPC is right :)

Anyway, I have fixed my issue with this commit, works for me. Ugly as well, but at least it works.

@KodrAus
Copy link
Member

KodrAus commented Nov 30, 2018

@zrzka So it looks like this is actually the way we're recommending doing this right now, so if you'd like to re-open this PR as-is then I'm on board with the approach.

Sorry about the misdirection here :) I can live with a solution that has drawbacks if it's consistent with what other libraries are doing.

@zrzka
Copy link
Contributor Author

zrzka commented Nov 30, 2018

@KodrAus np, next week.

@zrzka zrzka restored the feature/wasm branch December 1, 2018 12:19
Signed-off-by: Robert Vojta <rvojta@me.com>
@zrzka zrzka reopened this Dec 1, 2018
@zrzka
Copy link
Contributor Author

zrzka commented Dec 1, 2018

Restored & reopened as requested. Summary:

  • rand version bumped
  • stdweb & wasm-bindgen features added, they just transitively enables same features in the rand crate
  • if anyone wants to use v3, v4, v5 on wasm32 arch, stdweb and/or wasm-bindgen feature(s) must be enabled otherwise these modules wont be available
  • Travis CI builds these combinations for wasm32-unknown-unknown
  • Note added to lib.rs about this behavior

@Dylan-DPC-zz
Copy link
Member

This fails on stable & beta with an issue on stdweb:

error[E0554]: #![feature] may not be used on the stable release channel
   --> /home/travis/.cargo/registry/src/github.com-1ecc6299db9ec823/stdweb-0.4.10/src/lib.rs:115:5
    |
115 | /     feature(use_extern_macros)
116 | | )]
    | |_^

Signed-off-by: Robert Vojta <rvojta@me.com>
@zrzka
Copy link
Contributor Author

zrzka commented Dec 1, 2018

Yep, stdweb requires nightly. I reschuffled .travis.yml little bit. Also 1.22 doesn't have support form wasm32-unknown-unknown, so, I removed it from the main script.

@Dylan-DPC-zz
Copy link
Member

Dylan-DPC-zz commented Dec 1, 2018

Yeah they fixed the issue in this commit (koute/stdweb@f1fc5e3) and it will work on stable once it is released.

@Dylan-DPC-zz Dylan-DPC-zz added this to the 0.7.2 milestone Dec 1, 2018
@Dylan-DPC-zz Dylan-DPC-zz added this to In progress in 0.7.x via automation Dec 1, 2018
0.7.x automation moved this from In progress to Reviewer approved Dec 1, 2018
@Dylan-DPC-zz
Copy link
Member

bors: r+

bors bot added a commit that referenced this pull request Dec 1, 2018
351: Feature/wasm r=Dylan-DPC a=zrzka

# Description

This PR adds two new features:

* `stdweb`
* `wasm-bindgen`

These features are kind of _passthrough_ features, because they do nothing in the `uuid` crate itself. They're just passed to the `rand` crate to make the `uuid` crate working for the `wasm32-unknown-unknown` target.

# Motivation

I'm unable to generate random UUID (v4) when this crate is compiled for the `wasm32-unknown-unknown` target.

# Tests

I just added these features ...

```
- cargo test --features "v3"
- cargo test --features "v3 stdweb"
- cargo test --features "v3 wasm-bindgen"
```

... for all `v3` & `v4` & `v5` (`rand` crate is used in all these features) to the `script` section in the `.travis.yml`.

Not sure if it makes sense, but it can demonstrate that it's buildable at least.

I don't think that more tests are required unless you'd like to bring the whole `wasm-bindgen`, `wasm-pack`, ... machinery here. And it has no sense to do it, because goal of this PR is not to publish `uuid-rs` NPM package, just add the ability to compile & use it from the `wasm32-unknown-unknown` target.

# Related Issue(s)

* #350
* [now() doesn't work](balena-io-modules/balena-temen#37)

# Manual tests

My Cargo.toml:

```
[dependencies]
uuid = { features = ["v4"], git = "https://github.com/zrzka/uuid.git", branch = "feature/wasm" }

[target.wasm32-unknown-unknown.dependencies]
uuid = { features = ["wasm-bindgen"], git = "https://github.com/zrzka/uuid.git", branch = "feature/wasm" }
```

My `index.js`:

```
const bt = require('balena-temen');

console.log(
    bt.evaluate({
        "id": {
            "$$eval": "uuidv4()"
        }
    })
);
```

[uuidv4() implementation](https://github.com/balena-io-modules/balena-temen/blob/master/src/builtin/function/uuidv4.rs#L9-L11).

Output:

```
{ id: 'cefa2919-ff48-48ef-a231-e13697e23ed2' }
```


Co-authored-by: Robert Vojta <rvojta@me.com>
@bors
Copy link
Contributor

bors bot commented Dec 1, 2018

@bors bors bot merged commit d7e35f9 into uuid-rs:master Dec 1, 2018
0.7.x automation moved this from Reviewer approved to Done Dec 1, 2018
LinusU pushed a commit to LinusU/uuid that referenced this pull request Feb 26, 2020
LinusU pushed a commit to LinusU/uuid that referenced this pull request Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
0.7.x
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants