Repository navigation
Making the crate no_std and alloc compatible - #232
ngildenhuys wants to merge 2 commits into
Conversation
1bd2850 to
bb44333
Compare
|
We're interested in using |
| - name: "Build" | ||
| run: cargo build --verbose --no-default-features | ||
| - name: "Build" | ||
| run: cargo build --verbose --no-default-features --features=alloc | ||
| - name: "Run tests" | ||
| run: cargo test --verbose --no-default-features | ||
| - name: "Run tests as no_std with alloc" | ||
| run: cargo test --verbose --no-default-features --features=alloc |
There was a problem hiding this comment.
| - name: "Build" | |
| run: cargo build --verbose --no-default-features | |
| - name: "Build" | |
| run: cargo build --verbose --no-default-features --features=alloc | |
| - name: "Run tests" | |
| run: cargo test --verbose --no-default-features | |
| - name: "Run tests as no_std with alloc" | |
| run: cargo test --verbose --no-default-features --features=alloc | |
| - run: cargo build --verbose --no-default-features | |
| - run: cargo test --verbose --no-default-features | |
| - run: cargo build --verbose --no-default-features --features=alloc | |
| - run: cargo test --verbose --no-default-features --features=alloc |
this ordering will likely reduce overall time mildly (and names are not only superfluous but also fail to differentiate the steps properly.)
| # Turn this feature on to enable support for `#[derive(Arbitrary)]`. | ||
| default = ["std"] | ||
| derive = ["derive_arbitrary"] | ||
| std = ["alloc"] | ||
| alloc = [] |
There was a problem hiding this comment.
| # Turn this feature on to enable support for `#[derive(Arbitrary)]`. | |
| default = ["std"] | |
| derive = ["derive_arbitrary"] | |
| std = ["alloc"] | |
| alloc = [] | |
| default = ["std"] | |
| alloc = [] | |
| # Turn this feature on to enable support for `#[derive(Arbitrary)]`. | |
| derive = ["derive_arbitrary"] | |
| std = ["alloc"] |
There was a problem hiding this comment.
Besides the comment being adjusted to be above the corresponding feature, is there a reason for the specific ordering of the features? just curious.
| super::{Arbitrary, Result, Unstructured}, | ||
| std::{collections::HashSet, fmt::Debug, hash::Hash, rc::Rc, sync::Arc}, | ||
| core::{fmt::Debug, hash::Hash}, | ||
| std::collections::HashSet, |
There was a problem hiding this comment.
you're gating a chunk of these tests on the alloc feature, but then import hashset unconditionally. How does that work?
That said, I'd probably personally just specify the required features for tests in Cargo.toml and drop the cfgs. There is unlikely a future where there's any functional difference from enabling/disabling the features in this crate that would be worth testing.
There was a problem hiding this comment.
I'll gate it properly. The compiler is being smart here since HashSet is only being use in a function that is itself only used in tests that are gated behind the alloc feature, in a no_std compile time I imagine it is just dropping the import.
There was a problem hiding this comment.
actually doing some digging, you can import std without problem in test code because the unit test code is the only consumer of it and is compiled with std, the library code is compiled without std and linked in with the test code.
There was a problem hiding this comment.
I figured you gate the alloc feature like any other feature (like the derive feature), the tests that are testing alloc structures should be gated under the alloc feature, any code that doesn't need alloc should be tested without alloc to ensure that "version" of the library does not regress in functionality
There was a problem hiding this comment.
I would also prefer to just gate the tests on required features in cargo.toml
There was a problem hiding this comment.
Doing this would mean that you wouldn't be testing the library without the alloc feature enabled. This may cause an issue if someone adds another impl of Arbitrary for struct that requires alloc but they don't guard it (in code or in tests) properly tests won't catch that which could break users that have the alloc feature disabled.
There was a problem hiding this comment.
Right, but tests don't help with that, just compiling the code, right?
There was a problem hiding this comment.
ah yes you are correct, so then I'll just remove the tests with the --no-default-features flag
1f680b6 to
cca2693
Compare
|
Bumping this, as the checks failed due github's runners being down. |
b8daf7f to
cf3a562
Compare
|
bump...since it's been over a month now with no action |
cf3a562 to
3f50543
Compare
|
can we get this merged? @Manishearth |
|
@fitzgen ping. |
|
Ping |
|
I am pretty busy and this is a large change. I left one comment. |
3f50543 to
8c282fa
Compare
|
|
||
| ### Notes on no_std compatability: | ||
|
|
||
| - When compiling for versions older than **1.81.0** the `std` feature must be enabled. |
There was a problem hiding this comment.
either that, or we can cfg out the impl of std::error::Error for older versions
There was a problem hiding this comment.
cfgs based on version require detecting said version which means a build.rs or a dependency that uses a build.rs… Quite heavy for arbitrary. 1.81.0 is now approx 2 years old, so it seems fine to me to bump MSRV here.
There was a problem hiding this comment.
OTOH if we're fine with pulling rustversion here (procmacro) then fine I guess.
There was a problem hiding this comment.
On the other OTOH, it's backwards compatible to not support <1.81 no_std builds, since it is a new feature.
Change all the references to std structs requiring alloc to be guarded by an `alloc` feature and reference the alloc variants of the structs instead of the std one. Also adding a dependency to rustversion since older versions of rust have slightly different sets of what is implemented in core.
… library We only test with the alloc feature enabled to keep the configuration complexity of the test code down.
8c282fa to
fefd523
Compare
Adding
stdandallocfeatures to the crate, enabling std as default. Allowingallocfeature to gateallocstruct implementations andstdfeature to gatestdstruct implementations.