-
-
Notifications
You must be signed in to change notification settings - Fork 487
Disable std_rng, sys_rng and thread_rng by default
#1733
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am on the fence about this change. I think it makes sense to have the "default" RNG available by default, but it also makes sense to guard users against unintentionally pulling chacha20 and getrandom. So I would like to hear opinion of others.
|
|
||
| #[cfg(feature = "std")] | ||
| impl std::error::Error for Error {} | ||
| impl core::error::Error for Error {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to do this change in a separate PR.
It also may be worth to review all uses of #[cfg(feature = "std")]. I think some of them can be changed to alloc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did review these uses; the ones in seq require f64::ln and the other case requires std::io.
Sure, I can move it to a new PR.
As am I, hence asking for many reviewers to comment.
I am less sure that a thread-local rng should be enabled by default. It's why I opened #1545, but to quote @hpenne from that issue:
So, maybe just disabling it by default is the best option? |
| [dev-dependencies] | ||
| rand = { path = ".", features = ["chacha", "thread_rng"] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be slightly problematic; e.g. we have CI tests like this which will now use additional features:
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features
But, is there a better alternative? It's nice if examples just work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(BTW "chacha" is not strictly required.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually I just gate tests on features instead of forcibly enabling them like that.
With chacha tests you also could add chacha20 as a dev dependency.
|
One radical option is to not expose any RNGs in |
No thanks. "Batteries included" makes life easier for users; we already opted to include I have proposed moving |
Well, according to this stance shouldn't you keep the
Personally, I do support it. :) |
a8d3fbc to
3c9a37a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
While a minor inconvenience to users, explicit opt-in to these features may help users evaluate which features they wish to use
In general, I think people who want to consider which precise features to use (working on libraries, or relatively mature programs where unused features are expensive) would already look over the feature set and/or set no-default-features in Cargo.toml.
-
I generally would prefer to not making breaking changes if there is no large benefit. This change could require reading the documentation and updating Cargo.toml for any light users of the library that want to update. The error message when building without the
thread_rngfeature is somewhat tricky:
error[E0423]: expected function, found module `rand::rng`
--> examples/monty-hall.rs:80:19
|
80 | let mut rng = rand::rng();
| ^^^^^^^^^ not a function
error[E0603]: module `rng` is private
--> examples/print-next.rs:4:25
|
4 | let mut rng = rand::rng();
| ^^^ private module
|
note: the module `rng` is defined here
--> <path>/rand/src/lib.rs:80:1
|
80 | mod rng;
| ^^^^^^^
- I think the default feature set should be sufficient for the basic uses of the library. For example, with the default feature set it should be possible to write a simple program that randomly prints "heads" or "tails", or does a simple simulation like
examples/monte_carlo.rs. This requiresstd_rngat minimum, but the currentexamples/rely onthread_rng.
CHANGELOG.mdentrySummary
Disable several features by default:
std_rng,sys_rngandthread_rngMotivation
This enables
randto use a smaller dependency tree by default:getrandomandchacha20are no longer default-dependencies.While a minor inconvenience to users, explicit opt-in to these features may help users evaluate which features they wish to use
Details
Related: #1545.
Additional rules enable these features in the playground and (as required) for examples.
Also,
core::error::Errorimplementations no longer require featurestd.