See also
https://peter-kehl.github.io/embedded_low_level_rust
for building and testing no_std/no_std_heap/std libraries on desktop. That
also shows any source code included below.
—
Rust macros for ergonomic conditional compilation based on no_std,
no_std_heap and std
features.
The feature names used/referred-to by these macros are not, and cannot be, configurable. The above three feature names had to be hard-coded. (Explanation of those features is later in this document.)
TODO include Cargo.toml
Basic conditional feature-based compilation is easy. It can be as simple as using:
TODO EXAMPLE
But if you depend on compound conditions, it gets repetitive. It clutters the code. Hence suggest using macros.
New to Rust? Any identifier following by an exclamation mark ! indicates a
macro invocation. (Another type of macros, called attribute macros, start with a
hash #.)
TODO EXAMPLE
no_std library will work for any purpose: no_std without
heap, with heap or stdno_std libraries may have both heap-less and heap parts. For
example: extra functions or enum variants that work with
alloc::boxed::Box, alloc::vec::Vec, alloc::string::String…enum from another crate.no_std_heap (or similar). Then conditionally compile such
functionality (see later in this document).
—no_std and std mutually exclusive?std, (like using
HashMap)
std)no_std, toostd or
no_stdstd but another using
no_std of the same crate) are reported clearlystd/no_std-agnostic. They import an
std/no_std-aware library, but the std/no_std choice is left for
the higher consumer. Such a consumer library would use the std/no_std
aware library through abstractions that are std/no_std-independent.
This differentiation is not possible under the cargo book’s general
recommendation.
See slicing_any_std_test later in this document.no_std functionality is available under std feature,
toono_std_heap extends functionality of no_std. You could make
no_std_heap imply no_std and be usable even without no_std, but then
your documentation and compile checks (no_std_heap vs std) would get
complicated. Instead, may I suggest to have no_std_heap require
no_std. Then the exclusion checks are between no_std and std only.If your no_std_heap or std code needs extra dependencies, use
Features >
Optional
dependencies.
TODO ranging -> slicing example