Skip to content
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

Add no_std support #39

Merged
merged 1 commit into from
Nov 29, 2024
Merged

Add no_std support #39

merged 1 commit into from
Nov 29, 2024

Conversation

daxpedda
Copy link
Owner

@daxpedda daxpedda commented Nov 27, 2024

  • Through no_std support, we now also support wasm32v1-none.

  • Introduced a new std crate feature that is enabled by default. Without it #[no_std] is enabled, but only on Web! This allows Web target to build without std, which is now supported by wasm-bindgen as well.

    Additionally, various changes had to be done to support no_std:

    • no_std does not support thread_local!, we use once_cell to polyfill this gap. once_cell is not a new dependency, it is already a dependency of wasm-bindgen.

      • With feature = "std", we use thread_local! as before.
      • Without std and without target_feature = "atomics", we use a static mut with once_cell::unsync::Lazy.
      • Without std and with atomics, we use #[thread_local] with once_cell::unsync::Lazy.
    • Some f64 instructions are not available on no_std and had to be polyfilled. For this code from libm was copied. Which is used by std as well.

    • SystemTimeError now only implements Error with feature = "std".

    • no_std testing requires to refrain from using the default test harness. The problem was that native tests still needed to use the default test harness. To solve this, integration tests were removed from root crate and two workspace members added, that manually define all integration tests as test targets. The tests-web crate has harness = false on all tests, while tests-native functions regularly. This allow us to use the default test harness for native tests while disabling it for Web.

      Additionally, every test target requires the run crate feature, which are enabled by default depending on the target by the root crate. This way regular testing can function correctly for each target as long as --all-features is not used. E.g. cargo test --workspace and cargo test --workspace --target wasm32-unknown-unknown will work correctly.

      The tests-web library is used to implement the panic_handler, global_allocator and critical_section. It is always imported to reduce code duplication in all tests.

    • Used serde-json-core to cover tests with no_std as well.

    • All links to std documentation had to be supplemented with manual link when std is not present.

  • Improvements to CI:

    • Refactored all matrices for simplification and covering --no-default-features.
    • Split regular and minimal versions building off tests.
    • Split doctests from regular tests.
    • Update Rust toolchain when testing cargo publish.
    • Test coverage improvements:
      • The new wasm-bindgen update allows us to refrain from having to pass cfg flags to the wasm-bindgen proc-macros.
      • Use Rust llvm-tools instead of the official LLVM package.
      • Remove invalid script tag from coverage report.
      • Retain coverage artifact instead of limiting it to one day.
      • Refactor large environment variables into global level ones.
  • Small fixes that were stumbled upon:

    • Expose web_time::web with cfg(docsrs) on native as well.
    • Serialize and Deserialize implementation are now marked with doc(cfg(feature = "serde")).
    • Std docs.rs link unnecessarily including stable.
    • Recommendation for -Ctarget-feature=+nontrapping-fptoint was moved from the top-level to the "Usage" section.
    • The minimal version of Serde is now fully specified as v1.0.0.
    • Fix some typos in internal documentation.

no_std support for serde_test: serde-deprecated/test#36
no_std support for getrandom: rust-random/getrandom#541.
no_std Wasm f64 instructions: rust-lang/stdarch#1677

@daxpedda daxpedda force-pushed the no-std branch 30 times, most recently from 824f6f2 to ace24fe Compare November 29, 2024 13:58
@daxpedda daxpedda force-pushed the no-std branch 5 times, most recently from dad963c to a8fcea2 Compare November 29, 2024 17:01
@daxpedda daxpedda changed the title Implement no_std support Add no_std support Nov 29, 2024
@daxpedda daxpedda marked this pull request as ready for review November 29, 2024 20:48
@daxpedda daxpedda force-pushed the no-std branch 7 times, most recently from 37c4069 to 923672f Compare November 29, 2024 22:29
- Through `no_std` support, we now also support `wasm32v1-none`.
- Introduced a new `std` crate feature that is enabled by default. Without it `#[no_std]` is enabled, but only on Web! This allows Web target to build without std, which is now supported by `wasm-bindgen` as well.

  Additionally, various changes had to be done to support `no_std`:
  - `no_std` does not support `thread_local!`, we use `once_cell` to polyfill this gap. `once_cell` is not a new dependency, it is already a dependency of `wasm-bindgen`.
    - With `feature = "std"`, we use `thread_local!` as before.
    - Without std and without `target_feature = "atomics"`, we use a `static mut` with `once_cell::unsync::Lazy`.
    - Without std and with atomics, we use [`#[thread_local]`](https://doc.rust-lang.org/1.83.0/unstable-book/language-features/thread-local.html?highlight=thread_l#thread_local) with `once_cell::unsync::Lazy`.
  - Some `f64` instructions are not available on `no_std` and had to be polyfilled. For this code from [`libm`](https://crates.io/crates/libm) was copied. Which is used by std as well.
  - `SystemTimeError` now only implements `Error` with `feature = "std"`.
  - `no_std` testing requires to refrain from using the default test harness. The problem was that native tests still needed to use the default test harness. To solve this, integration tests were removed from root crate and two workspace members added, that manually define all integration tests as test targets. The `tests-web` crate has `harness = false` on all tests, while `tests-native` functions regularly. This allow us to use the default test harness for native tests while disabling it for Web.

    Additionally, every test target requires the `run` crate feature, which are enabled by default depending on the target by the root crate. This way regular testing can function correctly for each target as long as `--all-features` is not used. E.g. `cargo test --workspace` and `cargo test --workspace --target wasm32-unknown-unknown` will work correctly.

    The `tests-web` library is used to implement the `panic_handler`, `global_allocator` and `critical_section`. It is always imported to reduce code duplication in all tests.
  - Used [`serde-json-core`](https://crates.io/crates/serde-json-core) to cover tests with `no_std` as well.
  - All links to std documentation had to be supplemented with manual link when `std` is not present.
- Improvements to CI:
  - Refactored all matrices for simplification and covering `--no-default-features`.
  - Split regular and minimal versions building off tests.
  - Split doctests from regular tests.
  - Update Rust toolchain when testing `cargo publish`.
  - Test coverage improvements:
    - The new `wasm-bindgen` update allows us to refrain from having to pass `cfg` flags to the `wasm-bindgen` proc-macros.
    - Use Rust `llvm-tools` instead of the official LLVM package.
    - Remove invalid `script` tag from coverage report.
    - Retain coverage artifact instead of limiting it to one day.
    - Refactor large environment variables into global level ones.
- Small fixes that were stumbled upon:
  - Expose `web_time::web` with `cfg(docsrs)` on native as well.
  - `Serialize` and `Deserialize` implementation are now marked with `doc(cfg(feature = "serde"))`.
  - Std docs.rs link unnecessarily including `stable`.
  - Recommendation for `-Ctarget-feature=+nontrapping-fptoint` was moved from the top-level to the "Usage" section.
  - The minimal version of Serde is now fully specified as v1.0.0.
  - Fix some typos in internal documentation.
@daxpedda

This comment has been minimized.

@github-actions github-actions bot merged commit 227cd06 into main Nov 29, 2024
130 checks passed
@daxpedda daxpedda deleted the no-std branch December 9, 2024 10:52
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.

1 participant