Skip to content

Conversation

@loadingalias
Copy link
Contributor

@loadingalias loadingalias commented Nov 4, 2025

The Problem

Issue #24 reports that crc-fast cannot compile for wasm32-unknown-unknown targets because rand and regex are runtime dependencies, even though they're only used in CLI binaries and tests.

This prevents:

  • WebAssembly compilation
  • Cleaner dependency trees for library consumers
  • Use in environments where these dependencies aren't available

The Solution

Move rand and regex to [dev-dependencies] and gate all CLI binaries behind a cli feature flag. This separates the library from the tools, allowing the library to compile for wasm32 and preparing for future no_std support.

Changes

1. Cargo.toml

  • Moved rand and regex from [dependencies] to [dev-dependencies]
  • Added feature flags:
    • default = ["std"] - Maintains backward compatibility
    • std = [] - Gates std-dependent functionality
    • cli = ["std"] - Gates all binary tools
    • alloc = [] - Existing feature, preserved
  • Made binaries explicit with [[bin]] sections and required-features = ["cli"]
  • Gated integration tests with required-features = ["cli"]

2. src/bin/checksum.rs

  • Removed rand dependency completely
  • Implemented minimal xorshift64* PRNG for benchmark data (13 lines, zero dependencies)
  • Maintains benchmark functionality with deterministic, reproducible results

3. src/lib.rs

  • Added #![cfg_attr(not(feature = "std"), no_std)] header for future compatibility
  • Gated std-specific imports: std::fs::File, std::io::{Read, Write}
  • Gated Write trait impl with #[cfg(feature = "std")]
  • Gated file I/O functions (checksum_file, checksum_file_with_params) with #[cfg(feature = "std")]

4. tests/checksum_integration_tests.rs

  • Added #![cfg(feature = "cli")] to gate entire test file
  • Updated all test commands to include --features cli flag

5. .github/workflows/tests.yml

  • Updated cargo checkcargo check --all-features
  • Updated cargo clippycargo clippy --all-features
  • Updated binary runs to use --features cli --bin <name>
  • Updated tests to use cargo test --features cli
  • Ensures CI tests the new feature flags correctly

Planned version bump

  • Which: [ MINOR ]
  • Why: [ Non-breaking new functionality (feature flags), internal changes (PRNG replacement), enables new use cases (wasm compilation), but maintains full backward compatibility ]

Links

Notes

Validation Performed

All changes tested successfully:

✅ cargo build --lib                                  # Default build works
✅ cargo build --target wasm32-unknown-unknown --lib  # wasm compilation works! 🎉
✅ cargo build --bin checksum                         # Correctly fails without feature
✅ cargo run --features cli --bin checksum -- --help  # CLI works with feature
✅ cargo test --features cli                          # All tests pass
✅ cargo tree --edges normal | grep -E "rand|regex"   # Clean dependency tree
✅ cargo clippy --all-features                        # No warnings

CI Status: All test jobs pass except macos-13 (deprecated runner)

Impact

For library users: Zero breaking changes. Default behavior identical to v1.6.0.

For CLI users: Must now use --features cli:
cargo install crc-fast --features cli
cargo run --features cli --bin checksum

For wasm/no_std users: Library now compiles for wasm32-unknown-unknown and is ready for future no_std support... which I'll gladly work on in my free time this week.

Future Work

This PR lays the groundwork for full no_std support (would require replacing HashMap (hashbrown?), RwLock, and other std-only types), but doesn't implement it to keep changes focused and reviewable.

@loadingalias
Copy link
Contributor Author

BUMP - I'm dying to work on the no-std impl. I need it, too!

@onethumb onethumb requested a review from Copilot November 7, 2025 17:57
@onethumb
Copy link
Contributor

onethumb commented Nov 7, 2025

This looks good. Thanks!

Looks like it'll need an update to the Release GH workflow, too, but I can add that. 👍

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR restructures the crate to support optional no_std environments by introducing feature gates and removing unnecessary dependencies from the main build. The changes enable the library to be used in embedded systems and other no_std contexts while maintaining CLI functionality behind a feature flag.

  • Introduced std and cli feature flags to make std-dependent functionality optional
  • Moved rand and regex from main dependencies to dev-dependencies, implementing a lightweight xorshift64* PRNG for CLI benchmarking
  • Added conditional compilation attributes to gate std-dependent code (file I/O, Write trait implementations)

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Cargo.toml Restructured dependencies and features; moved rand and regex to dev-dependencies, added feature flags (std, cli), and configured bins/tests with required-features
src/lib.rs Added no_std support with conditional compilation for std-dependent features (file I/O, Write trait)
src/bin/checksum.rs Replaced rand dependency with custom xorshift64* PRNG implementation for benchmark data generation
tests/checksum_integration_tests.rs Added feature gate and updated all test commands to include --features cli
.github/workflows/tests.yml Updated CI commands to use --all-features for checks and --features cli for tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@onethumb onethumb merged commit c879913 into awesomized:main Nov 7, 2025
82 checks passed
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.

no_std and WASM blocked by rand/regex deps

2 participants