-
Notifications
You must be signed in to change notification settings - Fork 111
[codec] support no_std codec builds #1477
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
Conversation
537353e
to
12b2db8
Compare
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.
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 withcore::
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.
12b2db8
to
020929c
Compare
020929c
to
8874071
Compare
c66cd67
to
acd64c5
Compare
7da86c9
to
2b0778c
Compare
It would be great to add a monorepo/.github/workflows/fast.yml Lines 118 to 139 in ce5341d
|
//! [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` |
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.
nit: wrap in []
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.
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; |
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.
til: BTreeMap
works for this 👀
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.
Yeah the only reason Hash* collections don't work is because of the use of a non-deterministic hash function to avoid DoS.
Added! |
eed226a
to
4c3709a
Compare
codec/src/varint.rs
Outdated
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>() |
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.
nit: my fault, but might be nice to just use core::mem::size_of
instead of qualifying it everywhere
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.
Fine to not do this if just want to merge when things pass
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.
fixed
- name: Run setup | ||
uses: ./.github/actions/setup | ||
- name: Add minimal no_std target | ||
run: rustup target add thumbv7em-none-eabihf |
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.
confirmed that this target is commonly used for this 👍
Put up a small PR to add testing for |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ 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
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
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: