Skip to content

Conversation

roberto-bayardo
Copy link
Collaborator

@roberto-bayardo roberto-bayardo commented Aug 26, 2025

Makes codec crate buildable under no_std. A "std" feature flag is introduced for conditionally compiling codec trait implementations for std:: types such as HashMap and HashSet.

Confirmed the package now builds in a no_std architecture:

]: cargo check -p commonware-codec --no-default-features --target thumbv7em-none-eabihf

    Blocking waiting for file lock on build directory
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.45s

@roberto-bayardo roberto-bayardo marked this pull request as ready for review August 26, 2025 18:14
Copy link
Contributor

@Copilot 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 adds support for building the codec crate in no_std environments by introducing conditional compilation features and separating std-dependent functionality from core functionality.

  • Introduces std feature flag (enabled by default) to control std vs no_std compilation
  • Replaces std:: imports with core:: equivalents where possible
  • Separates std-dependent collections (HashMap, HashSet) from no_std-compatible ones (BTreeMap, BTreeSet)

Reviewed Changes

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

Show a summary per file
File Description
codec/Cargo.toml Adds feature flags for std/no_std support
codec/src/lib.rs Adds no_std attribute and alloc extern declaration
codec/src/varint.rs Replaces std:: with core:: imports for basic functionality
codec/src/types/primitives.rs Updates memory size calculations to use core::mem
codec/src/types/vec.rs Adds conditional import for alloc::vec::Vec
codec/src/types/mod.rs Conditionally includes std-dependent modules
codec/src/types/set.rs Removes BTreeSet implementation (moved to set_nostd)
codec/src/types/set_nostd.rs New file with BTreeSet implementation using alloc
codec/src/types/map.rs Removes BTreeMap implementation (moved to map_nostd)
codec/src/types/map_nostd.rs New file with BTreeMap implementation using alloc
codec/src/error.rs Updates error trait bounds to use core::error
codec/src/config.rs Replaces std::ops with core::ops imports

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@patrick-ogrady
Copy link
Contributor

It would be great to add a no_std CI compilation check for crates/modes that support it:

WASM:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run setup
uses: ./.github/actions/setup
- name: Add WASM target
run: rustup target add wasm32-unknown-unknown
- name: Build cryptography
run: cargo build --target wasm32-unknown-unknown --release --manifest-path cryptography/Cargo.toml && du -h target/wasm32-unknown-unknown/release/commonware_cryptography.wasm
- name: Build macros
run: cargo build --target wasm32-unknown-unknown --release --manifest-path macros/Cargo.toml # can't check size because it is a proc-macro
- name: Build utils
run: cargo build --target wasm32-unknown-unknown --release --manifest-path utils/Cargo.toml && du -h target/wasm32-unknown-unknown/release/commonware_utils.wasm
- name: Build runtime
run: cargo build --target wasm32-unknown-unknown --release --manifest-path runtime/Cargo.toml && du -h target/wasm32-unknown-unknown/release/commonware_runtime.wasm
- name: Build consensus
run: cargo build --target wasm32-unknown-unknown --release --manifest-path consensus/Cargo.toml && du -h target/wasm32-unknown-unknown/release/commonware_consensus.wasm
- name: Build storage
run: cargo build --target wasm32-unknown-unknown --release --manifest-path storage/Cargo.toml && du -h target/wasm32-unknown-unknown/release/commonware_storage.wasm

//! [f32], [f64], [u8; N],
//! and [usize] (must fit within a [u32] for cross-platform compatibility).
//! - Collections: [`Vec<T>`], [`Option<T>`]
//! - Collections: [`Vec`], [`Option`], `BTreeMap`, `BTreeSet`
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: wrap in []

Copy link
Collaborator Author

@roberto-bayardo roberto-bayardo Aug 26, 2025

Choose a reason for hiding this comment

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

Unfortunately this doesn't work, since it's not a default visible type. If you fully specify alloc::collections::BTreeMap instead, then this fails because cargo doc runs in std mode and doesn't have alloc visibility.

error::Error,
RangeCfg,
};
use alloc::collections::BTreeMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

til: BTreeMap works for this 👀

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah the only reason Hash* collections don't work is because of the use of a non-deterministic hash function to avoid DoS.

@patrick-ogrady patrick-ogrady added this to the v0.0.60 milestone Aug 26, 2025
@roberto-bayardo
Copy link
Collaborator Author

It would be great to add a no_std CI compilation check for crates/modes that support it:

Added!

BrendanChou
BrendanChou previously approved these changes Aug 26, 2025
const _COMMIT_OP_ASSERT: () =
assert!(std::mem::size_of::<Self>() == std::mem::size_of::<Self::UnsignedEquivalent>());
const _COMMIT_OP_ASSERT: () = assert!(
core::mem::size_of::<Self>() == core::mem::size_of::<Self::UnsignedEquivalent>()
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: my fault, but might be nice to just use core::mem::size_of instead of qualifying it everywhere

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fine to not do this if just want to merge when things pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

BrendanChou
BrendanChou previously approved these changes Aug 26, 2025
- name: Run setup
uses: ./.github/actions/setup
- name: Add minimal no_std target
run: rustup target add thumbv7em-none-eabihf
Copy link
Contributor

Choose a reason for hiding this comment

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

confirmed that this target is commonly used for this 👍

@patrick-ogrady
Copy link
Contributor

Put up a small PR to add testing for no_std builds as well: #1478

@patrick-ogrady patrick-ogrady merged commit e3fffd3 into main Aug 26, 2025
37 checks passed
@patrick-ogrady patrick-ogrady deleted the nostd-digest branch August 26, 2025 21:40
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.69%. Comparing base (ce5341d) to head (31cb85b).
⚠️ Report is 1 commits behind head on main.

@@            Coverage Diff             @@
##             main    #1477      +/-   ##
==========================================
- Coverage   91.71%   91.69%   -0.02%     
==========================================
  Files         278      280       +2     
  Lines       70501    70361     -140     
==========================================
- Hits        64659    64519     -140     
  Misses       5842     5842              
Files with missing lines Coverage Δ
codec/src/config.rs 100.00% <ø> (ø)
codec/src/types/btree_map.rs 100.00% <100.00%> (ø)
codec/src/types/btree_set.rs 100.00% <100.00%> (ø)
codec/src/types/bytes.rs 100.00% <ø> (ø)
codec/src/types/hash_map.rs 100.00% <100.00%> (ø)
codec/src/types/hash_set.rs 100.00% <ø> (ø)
codec/src/types/primitives.rs 99.52% <100.00%> (ø)
codec/src/types/vec.rs 100.00% <100.00%> (ø)
codec/src/varint.rs 96.59% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce5341d...31cb85b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants