-
Notifications
You must be signed in to change notification settings - Fork 92
Making the crate no_std and alloc compatible #232
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
base: main
Are you sure you want to change the base?
Conversation
1bd2850 to
bb44333
Compare
|
We're interested in using |
.github/workflows/rust.yml
Outdated
| - 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - 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.)
Cargo.toml
Outdated
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
f095a0a to
1f680b6
Compare
1f680b6 to
cca2693
Compare
Adding
stdandallocfeatures to the crate, enabling std as default. Allowingallocfeature to gateallocstruct implementations andstdfeature to gatestdstruct implementations.