Skip to content

Making the crate no_std and alloc compatible - #232

Open
ngildenhuys wants to merge 2 commits into
rust-fuzz:mainfrom
ngildenhuys:no_std_support
Open

ngildenhuys wants to merge 2 commits into
rust-fuzz:mainfrom
ngildenhuys:no_std_support

Conversation

@ngildenhuys

Copy link
Copy Markdown
Contributor

Adding std and alloc features to the crate, enabling std as default. Allowing alloc feature to gate alloc struct implementations and std feature to gate std struct implementations.

@ngildenhuys

Copy link
Copy Markdown
Contributor Author

#38

@epilys

epilys commented Feb 2, 2026

Copy link
Copy Markdown

We're interested in using arbitrary in a no_std environment. Is there something blocking this PR? could it be merged?

Comment thread .github/workflows/rust.yml Outdated
Comment on lines +70 to +77
- 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.)

Comment thread Cargo.toml Outdated
Comment on lines +26 to +30
# Turn this feature on to enable support for `#[derive(Arbitrary)]`.
default = ["std"]
derive = ["derive_arbitrary"]
std = ["alloc"]
alloc = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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"]

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/tests.rs
super::{Arbitrary, Result, Unstructured},
std::{collections::HashSet, fmt::Debug, hash::Hash, rc::Rc, sync::Arc},
core::{fmt::Debug, hash::Hash},
std::collections::HashSet,

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer to just gate the tests on required features in cargo.toml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but tests don't help with that, just compiling the code, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes you are correct, so then I'll just remove the tests with the --no-default-features flag

@ngildenhuys
ngildenhuys force-pushed the no_std_support branch 2 times, most recently from 1f680b6 to cca2693 Compare February 2, 2026 20:10
@ngildenhuys

Copy link
Copy Markdown
Contributor Author

Bumping this, as the checks failed due github's runners being down.

@fitzgen fitzgen closed this Mar 4, 2026
@fitzgen fitzgen reopened this Mar 4, 2026
@ngildenhuys
ngildenhuys requested a review from nagisa March 14, 2026 16:31
@ngildenhuys

Copy link
Copy Markdown
Contributor Author

bump...since it's been over a month now with no action

@ngildenhuys

Copy link
Copy Markdown
Contributor Author

can we get this merged? @Manishearth

@epilys

epilys commented Oct 2, 2026

Copy link
Copy Markdown

@fitzgen ping.

@epilys

epilys commented Nov 10, 2026

Copy link
Copy Markdown

Ping

@Manishearth

Copy link
Copy Markdown
Member

I am pretty busy and this is a large change. I left one comment.

Comment thread README.md Outdated

### Notes on no_std compatability:

- When compiling for versions older than **1.81.0** the `std` feature must be enabled.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fitzgen @nagisa are we fine with this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either that, or we can cfg out the impl of std::error::Error for older versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that instead

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH if we're fine with pulling rustversion here (procmacro) then fine I guess.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants