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

Put stdweb dependency behind a target feature #336

Merged
merged 2 commits into from
Mar 26, 2018

Conversation

pitdicker
Copy link
Contributor

Cargo.toml Outdated
@@ -23,6 +23,7 @@ alloc = ["rand_core/alloc"] # enables Vec and Box support without std
i128_support = [] # enables i128 and u128 support

serde-1 = ["serde", "serde_derive"]
wasm-stdweb = ["stdweb"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just call it stdweb, or rather just use cfg(feature="stdweb") since that is already an optional dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my choice, but cargo does not support features that have the same name as dependencies. We have the same problem with serde, which has serde-1 as feature flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know using the name of an optional dependency was possible, much better!

.travis.yml Outdated
@@ -47,7 +47,7 @@ matrix:
# Cargo has errors with sub-commands so ignore updating for now:
- cargo --list | egrep "^\s*web$" -q || cargo install cargo-web
script:
- cargo web test --target wasm32-unknown-unknown --nodejs
- cargo web test --target wasm32-unknown-unknown --nodejs --features=wasm-stdweb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget the feature name here

@tomaka
Copy link

tomaka commented Mar 26, 2018

This is also theoretically the wrong fix unfortunately. What the stdlib does when the feature flag is enabled is assume that an FFI function exists, and calls it.

In other words, something like:

#[cfg(feature = "foo")]
extern "C" {
    fn rand() -> usize;
}

@pitdicker
Copy link
Contributor Author

I don't completely follow what you mean, @tomaka. Can you say a bit more about what is exactly wrong?

@dhardy
Copy link
Member

dhardy commented Mar 26, 2018

He means that js code shouldn't be there. I'm not really sure why that's beneficial though. @quininer can you also comment?

@dhardy dhardy added P-medium E-help-wanted Participation: help wanted X-experimental Type: experimental change labels Mar 26, 2018
@quininer
Copy link
Contributor

I don't understand.

@tomaka
Copy link

tomaka commented Mar 26, 2018

The general problem is assuming that wasm32-unknown-unknown is related to the web. Surely it was invented for the web, but in general it has a lot of traction for non-web usages.

With this PR, if you use a library A that depends on rand, then this library A will have propagate the stdweb feature to the user. This is really cumbersome.

@pitdicker
Copy link
Contributor Author

But then are we not ok now? With this PR wasm32-unknown-unknown will again have stubs, and only have an implementation using stdweb when you set the feature flag.

@dhardy
Copy link
Member

dhardy commented Mar 26, 2018

So do we want optional stdweb support and a feature calling unimplemented extern(C) function prototypes? I guess that's what you're asking for.

Then I'll answer that: no. We are planning on letting users replace OsRng anyway (for embedded platforms with no OS), but will probably do so in a different method. So I think this PR is all we need here (the custom WASM non-web entropy source can wait).

@tomaka
Copy link

tomaka commented Mar 26, 2018

Here is the whole background of the discussion if you are motivated for some lecture: rust-lang/rust#47102
Let's not reopen the same debate here!

@dhardy
Copy link
Member

dhardy commented Mar 26, 2018

With this PR, if you use a library A that depends on rand, then this library A will have propagate the stdweb feature to the user. This is really cumbersome.

If and only if library A depends on the stdweb feature of Rand. This is exactly the same as the case with std and other features. I don't see a problem here.

The status (after merging this PR) is thus that any attempt to use Rand when targetting wasm32-unknown-unknown will panic (if the stdweb feature is not used), which sounds similar to std without wasm-syscall (although I did not read the whole PR linked). As I said we plan to add a more general way to fix this in the future, but not as part of this PR.

@tomaka
Copy link

tomaka commented Mar 26, 2018

If and only if library A depends on the stdweb feature of Rand. This is exactly the same as the case with std and other features. I don't see a problem here.

The problem is that it's not A's job to decide whether to enable the stdweb feature of rand. It's only the final binary that should do so.

@pitdicker
Copy link
Contributor Author

The problem is that it's not A's job to decide whether to enable the stdweb feature of rand. It's only the final binary that should do so.

That seems like a good, although theoretical concern. If somewhere in the dependency chain A decides stdweb is available, I see no reason not to use it for OsRng.

But when things are worked out a bit more around WebAssembly, and we have a solution for #313, I think we can revisit this and move more in the direction you have in mind. But for now this simply solves a practical problem.

@dhardy
Copy link
Member

dhardy commented Mar 26, 2018

There are two solutions to that:

  1. A does not depend on the stdweb feature at all; the final binary may have to depend on Rand just to add the stdweb feature.
  2. Lib A adds an optional stdweb feature itself, forwarding the dependency.

Oh, I see @pitdicker got here before I did 👍

@dhardy
Copy link
Member

dhardy commented Mar 26, 2018

BTW rebase and merge?

@pitdicker pitdicker merged commit 43277d6 into rust-random:master Mar 26, 2018
@pitdicker pitdicker deleted the stdweb_feature branch March 26, 2018 15:27
pitdicker added a commit that referenced this pull request Apr 4, 2018
Put stdweb dependency behind a target feature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-help-wanted Participation: help wanted X-experimental Type: experimental change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants