Skip to content

Conversation

@antimora
Copy link
Contributor

@antimora antimora commented Sep 5, 2025

This PR addresses issue #650 by following the convention of the feature configuration for no_std support.

Changes

  • Removed mutual exclusivity between std and alloc features: Previously, the crate required exactly one of std or alloc
    to be enabled, causing compilation errors when both or neither were specified
  • Automatic alloc fallback: When std is not enabled, the crate now automatically uses alloc + hashbrown::HashMap
  • Backward compatibility: The alloc feature is retained as a no-op for compatibility with existing downstream crates

How it works

  • Default build (cargo build): Uses std with std::collections::HashMap
  • No-std build (cargo build --no-default-features): Automatically uses alloc with hashbrown::HashMap
  • Explicit alloc (cargo build --no-default-features --features alloc): Still works for backward compatibility

Testing

All tests pass in both configurations:

cargo test                          # std mode ✓
cargo test --no-default-features    # no-std mode ✓
cargo test --no-default-features --features alloc  # backward compat ✓

This aligns with common Rust ecosystem patterns where default = ["std"] provides standard library support, and
--no-default-features automatically provides no_std support with alloc.

Fixes #650

Updated Cargo.toml to always enable alloc support and removed the alloc feature from dependencies. The alloc feature is now a no-op for backward compatibility. Adjusted lib.rs to use alloc when std is not enabled and removed compile-time errors enforcing feature exclusivity.
@antimora
Copy link
Contributor Author

antimora commented Sep 5, 2025

Hi @Narsil, this is to support both std/no_std. The feature flag patten follows "std" flag convention. This is needed for Burn project to support no_std when "std" is disabled by no-default-features. I tested locally and it works.

Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM. I'm not sure why we wrote it being exclusive features in the first place.

Thanks.

Removes the explicit 'alloc' feature from the no-std test command in the GitHub Actions workflow, running tests with only --no-default-features. This may be to ensure tests run in a stricter no-std environment or to match updated feature requirements.
@antimora
Copy link
Contributor Author

antimora commented Sep 5, 2025

LGTM. I'm not sure why we wrote it being exclusive features in the first place.

Thanks.

yeah, it's rare that mutually exclusive feature flags are needed. Probably at the type the pattern wasn't known.

BTW, I updated no-std test command in CI workflow to remove alloc flag. Just case if someone accidentally re-introduces the alloc flag usage.

@Narsil Narsil merged commit 0816a1a into huggingface:main Sep 8, 2025
27 of 29 checks passed
@antimora
Copy link
Contributor Author

@Narsil , thank you for the review and merge. I checked feature configurations and no-std works for Burn.

Do you know if there is a chance to release the patch in near future? The Burn team would like to make a release soon. I am currently linking github repo in Cargo.toml

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.

Cannot automatically select between std and alloc based on default features

2 participants