-
Notifications
You must be signed in to change notification settings - Fork 432
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
Make thread_rng()
available without std
#313
Comments
Good goal. Even without thread-local memory we have static memory, and any platform with threading support must support at least mutexes, so we could try using them; I don't know how to write portable code doing that however. @pitdicker already suggested two options for user-selected fallbacks:
It would be good to have help from people experienced in no-std work on this one. |
This seems like it might be a good candidate for the "global resource" treatment being discussed as part of the portability WG rust-lang-nursery/portability-wg#3 |
Thank you for linking that issue, that seems like just the thing to keep an eye on! |
|
That's half the problem. The other half is getting entropy to seed a PRNG.
|
I thought |
Given a suitable high-resolution timer, yes. But not by default, because the default timer comes from the |
There seem to be three issues here:
Custom static code injection can be done via a feature flag which enables (and uses) Alternatively we could add a dummy crate with an empty implementation, and expect users to patch their dependency tree with a real implementation. Or we could use run-time code injection, which should allow more Rustic options, perhaps passing a pointer to some type implementing |
There is one other advantage to using a dummy crate: local storage. The current design of If But I'm not sure this is the right solution. Run-time injection as in the So we could make |
@dhardy I think it's worth pointing out that https://github.com/rust-random/getrandom/blob/master/src/lib.rs#L127 so the "second half" of the problem seems solved? |
The second half of this problem is rust-random/getrandom#4 . As for the "first" half of this problem, I'm not sure how much genuine interest there is, and for what type of solution (thread-local, shared-with-mutex, single-thread reentrant-safe, simple single-thread). I think it may not be useful to pursue a general solution in this crate, however I guess this issue can stay open for discussion. |
In fact, lets close this until there is demand; otherwise this appears to be a solution in search of a problem. |
Currently we don't have
thread_rng()
available without thestd
feature.The primary reason is that we do not have thread-local storage available to keep the state of
ThreadRng
in. And a second problem is that we don't haveOsRng
orJitterRng
available, so we have no way to seed it.The reason to make
ThreadRng
available, even when the performance is sub-par, is that it makes it easier to port libraries to theno_std
environment. API's do not always allow for the state of an RNG to be passed around.A few possible solutions:
ThreadRng
in.OsRng
).Two problems to keep in mind:
ThreadRng
to something else.One solution could be to let
ThreadRng
fall back toEntropyRng
when we have no thread-local storage, meaning nostd
feature. Or maybe even put the fallback behind a feature flag itself.Next
EntropyRng
could be changed to not only useOsRng
, andJitterRng
as a fallback, but to have a secondary fallback if both are not available. The secondary fallback should then somehow be pluggable, so some external generator like RDRAND can be used.The text was updated successfully, but these errors were encountered: