Skip to content

Commit

Permalink
Merge #351
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and zrzka committed Dec 1, 2018
2 parents f2e2463 + d7e35f9 commit f8c83f9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Expand Up @@ -11,19 +11,37 @@ matrix:
- rust: nightly
before_script:
- rustup component add rustfmt-preview
- rustup target add wasm32-unknown-unknown
script:
- cargo fmt --all -- --check
- cargo test --features "serde std v1 v3 v4 v5"
- cargo bench --features "serde std v1 v3 v4 v5"
- cargo build --target wasm32-unknown-unknown --features "v3 stdweb"
- cargo build --target wasm32-unknown-unknown --features "v4 stdweb"
- cargo build --target wasm32-unknown-unknown --features "v5 stdweb"
- cargo build --target wasm32-unknown-unknown --features "v3 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v4 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v5 wasm-bindgen"
- os: linux
rust: nightly
script:
- cargo test --features "const_fn"
- rust: beta
before_script:
- rustup target add wasm32-unknown-unknown
script:
- cargo build --target wasm32-unknown-unknown --features "v3 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v4 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v5 wasm-bindgen"
- rust: stable
before_script:
- rustup component add clippy-preview
- rustup target add wasm32-unknown-unknown
script:
- cargo clippy --features "u128 v1 v3 v4 v5 slog"
- cargo build --target wasm32-unknown-unknown --features "v3 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v4 wasm-bindgen"
- cargo build --target wasm32-unknown-unknown --features "v5 wasm-bindgen"
- rust: 1.22.0
script:
- cargo test --features "serde std v4"
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -38,7 +38,7 @@ version = "0.5"

[dependencies.rand]
optional = true
version = "0.5"
version = "0.6"

[dependencies.serde]
default-features = false
Expand Down Expand Up @@ -77,6 +77,8 @@ v1 = []
v3 = ["md5", "rand"]
v4 = ["rand"]
v5 = ["sha1", "rand"]
stdweb = ["rand/stdweb"]
wasm-bindgen = ["rand/wasm-bindgen"]

# since rust 1.26.0
u128 = ["byteorder"]
Expand Down
43 changes: 40 additions & 3 deletions src/lib.rs
Expand Up @@ -43,6 +43,14 @@
//! * `serde` - adds the ability to serialize and deserialize a `Uuid` using the
//! `serde` crate.
//!
//! You need to enable one of the following Cargo features together with
//! `v3`, `v4` or `v5` feature if you're targeting `wasm32` architecture:
//!
//! * `stdweb` - enables support for `OsRng` on `wasm32-unknown-unknown` via
//! `stdweb` combined with `cargo-web`
//! * `wasm-bindgen` - `wasm-bindgen` enables support for `OsRng` on
//! `wasm32-unknown-unknown` via [`wasm-bindgen`]
//!
//! By default, `uuid` can be depended on with:
//!
//! ```toml
Expand Down Expand Up @@ -103,6 +111,8 @@
//!
//! * [Wikipedia: Universally Unique Identifier]( http://en.wikipedia.org/wiki/Universally_unique_identifier)
//! * [RFC4122: A Universally Unique IDentifier (UUID) URN Namespace]( http://tools.ietf.org/html/rfc4122)
//!
//! [`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "const_fn", feature(const_fn))]
Expand Down Expand Up @@ -160,11 +170,38 @@ mod std_support;
mod test_util;
#[cfg(feature = "u128")]
mod u128_support;
#[cfg(feature = "v3")]
#[cfg(all(
feature = "v3",
any(
not(target_arch = "wasm32"),
all(
target_arch = "wasm32",
any(feature = "stdweb", feature = "wasm-bindgen")
)
)
))]
mod v3;
#[cfg(feature = "v4")]
#[cfg(all(
feature = "v4",
any(
not(target_arch = "wasm32"),
all(
target_arch = "wasm32",
any(feature = "stdweb", feature = "wasm-bindgen")
)
)
))]
mod v4;
#[cfg(feature = "v5")]
#[cfg(all(
feature = "v5",
any(
not(target_arch = "wasm32"),
all(
target_arch = "wasm32",
any(feature = "stdweb", feature = "wasm-bindgen")
)
)
))]
mod v5;
#[cfg(all(windows, feature = "winapi"))]
mod winapi_support;
Expand Down

0 comments on commit f8c83f9

Please sign in to comment.