-
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
Add ability to compile for core #108
Conversation
I fixed the 1.0.0 build in the latest commit. I don't think the nightly failure is due to this patch. |
The feature checks seem backwards; "std" should be an enabled-by-default feature, as opposed to "no_std" being a disabled-by-default feature. It would be a good idea to get rid of the dependencies on alloc, collections, and core_io; they're basically unnecessary, and it would be nice to allow using |
Features are opt-in over the whole dependency tree. If your project is no_std, you want to be able to specify transitive dependencies to also be no_std. You can't do this with Also, I think the no_std ecosystem basically settled on having a no_std feature. For example bitflags had this before the feature was removed.
I'll look into this. They are used in some places. |
Added the |
I'm not sure what you're getting at with this... The reason you want a default-on feature is that it reduces the complexity of writing and using a no_std crate. Suppose I write a no_std crate cool_stuff that depends on rand... if rand has an "std" feature, I just write this:
Then if someone uses my crate, they just write:
And everything just works whether or not the final program is no_std. (no_std was carefully designed to make this work.) Now, suppose rand has a no_std feature instead. In this scenario, cool_stuff is forced to expose a completely useless no_std feature:
(Alternatively, if it doesn't expose a no_std feature, you'll get strange compile errors if another crate in the program uses the std variant of rand.) Then if you want to use mycrate in a no_std scenario, you have to set this completely useless feature:
This seems worse overall. |
I think you're making a good point. I suppose it's important to consider that when building a no_std final crate, all dependencies need to be no_std-aware.
This is the most convincing point. |
* Add dependency on core_io * Change mentions of std to core * Cfg-out everything that doesn't work in core
Thanks for the PR! Also cc @BurntSushi This seems invasive enough to me that we probably don't want to merge without rethinking the design of the |
I'm not sure what you mean by "difficult to use". I mean, without the std feature, the rand crate doesn't include thread_rng (so you have to choose an RNG and seed it yourself), but almost everything else works as-is. |
I'm specifically worried about seemingly arbitrary APIs disappearing, such as the distributions, |
This should be a separate crate which |
I suppose this sounds like a vote for @nagisa's proposal below, but I think it's acceptable. People working in no_std are used to not having the full functionality available to them.
An important part of the Rust ecosystem is the existence of several core (pun intended) traits that people can depend on in their interfaces,
This is an option. However, applying this paradigm everywhere would result in twice the number of crates. I'm not convinced that that is desirable. |
I'm curious where you are with this. It looks like the last point in the discussion was deciding whether to use @jethrogb's changes, or split FWIW, I agree with @jethrogb about
Allowing dependence on If it would help, I would be happy to implement the alternative to this PR, the splitting into two crates, so it might be easier to compare the two solutions, but I think that this is the better one. |
Cargo's crate feature system is not compatible with a setup like in the PR's current state. Crate features must be additive, otherwise there can be conflicts when you compile two different rdeps of rand in the same project. Scenario: Mother crate depends on A and B.
Cargo compiles the rand crate in the mother project with feature unioning. Features "default", "std", "core_io" and "libc" are thus selected. Crate Edit: is this a concern that can be disregarded in some way? I was thinking of it as a fundamental incompatibility, but I guess opinions can differ on how the crate features system should be used. |
@bluss The idea is that if you're in the no_std world you're only using crates that are compatible with it. If you're not, thing will break. With the current feature set, it is not intended for library crates to set the core_io feature. Library crates using rand just need to set default-features = false, and have a default-on std feature that also pulls in rand/std. This will keep everything working in the std case. In the no_std case, the final crate also needs to depend on rand and set features = [core_io]. |
I thought it should be possible to write crates that are compatible with both worlds simultaneously? Otherwise we need to rewrite the whole crate base to have special no-std compatible versions. What I am worried about is things like |
Not if you have a crate that depends on another crate only when using no_std. Cargo's feature unioning prevents any kind of sane handling here. The compatibility only goes one way: crates that use no_std generally can be used in the std world.
You need that anyway, because any crate that is not no_std will pull in std and now your final link artifact will be broken because you didn't want to link to std.
Are you saying that there should be a separate |
We can't accept this situation, in the big picture I mean. We need a composable ecosystem where more and more basic parts are being made available to both core- and std-using projects. We need it to not be two worlds.
Well something like it — I'll say what I wish for here, without deeper knowledge: A consumer should not need to support both. If the core-only API is good enough for them, then they use it in both core- and std-compatible mode. |
Agreed. The current situation is mostly a limitation of the way Cargo handles things. I think the situation could be improved if Cargo would let the final crate set cfgs throughout the dependency chain and cfg-dependent dependency/feature selection actually worked. |
Any progress on this? |
Looking into this,
BTW I've already reimplemented this. Just posting here to see if I get any answers to the above. |
@dhardy When coming back to a year-and-half old PR, I'd expect a little more research from your part into the history and rationale behind the design choices. Just because you don't immediately see why something is the way it is or whether it's useful, doesn't mean that there's no merit to it. In particular, the first two questions are fully understood and easily answered by anyone who has ever dealt with the
This is a recent change.
From the |
@jethrogb I think there is a bit of trouble with the communication. As part of the rand crate redesign there was an almost endless discussion about how to add error handling, with a focus on working also in the https://github.com/dhardy/rand includes efforts to support But you seem to know better than us what is needed for the
|
People ask about using the |
Okay, that makes sense, I suppose. I'm less sure whether With It is a shame that The weird thing is those distributions not being usable due to missing FP operations; this almost seems like an oversight in the (Yes, I'd like to get some |
Reimplemented as #208 |
This patch makes it possible to compile this crate with
no_std
enabled. This mostly just makes available theRng
trait and related types. Of course, in core, there is no default RNG.Basically the patch just chanes all mentions of
std
tocore
and configs-out everything that doesn't work in core. It adds a dependency on my core_io crate which was made basically the same way.To use the crate in core-mode, you have to do
{use-default-features=false,features=["no_std"]}
.I'm happy to carry this patch as a separate
core_rand
crate but I think it'd be nicer if it lived upstream (getting closer to my ultimate goal of making most things available in no_std mode).