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

Introduce support for environments where the standard library is not available (#![no_std]) but a global allocator is #59

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

IntegralPilot
Copy link

This PR introduces support for environments where the standard library is not available but a global allocator is (i.e. embedded usecase) to the "subcrates" in this repository - wain-ast, wain-syntax-text, wain-syntax-binary, wain-validate and wain-exec.

wain-ast:

  • Support added without needing a feature to be enabled.
  • Incremented semver "minor" version so package version is now 0.3.0.

wain-exec:

  • Support added and locked behind no_std non-default feature.
  • This is because to add support a dependency is required on libm - this is only included with the no_std feature to make the claim that "no other dependencies are used" true for the default featureset.
  • Incremented semver "minor" version so package version is now 0.4.0.
  • execute function and DefaultImporter are not available when the no_std feature is enabled - developers who wish to use this feature need to create their own Importer and use runtime APIs to create and run a Runtime that uses it.

wain-syntax-binary:

  • Support added without needing a feature to be enabled.
  • Incremented semver "minor" version so package version is now 0.2.0.

wain-syntax-text:

  • Support added and locked behind no_std non-default feature.
  • This is because to add support a dependency is required on libm and hashbrown - this is only included with the no_std feature to make the claim that "no other dependencies are used" true for the default featureset.
  • Incremented semver "minor" version so package version is now 0.3.0.

wain-validate:

  • Support added and locked behind no_std non-default feature.
  • This is because to add support a dependency is required on hashbrown - this is only included with the no_std feature to make the claim that "no other dependencies are used" true for the default featureset.
  • Incremented semver "minor" version so package version is now 0.2.0.

Additionally, post-commit hooks were failing due to Clippy identified "dead code" in spec-test. I did not alter the code in spec-test to introduce #![no_std] support however have commented out the code that turned out to be actually dead and added Clippy ignore directives to the code that was being used in the tests but not detected by Clippy, in order to get these checks to pass and resolve all Clippy warnings.

If you decide to approve this PR, can you please deploy the new versions of the crates to crates.io so that I can use them more easily in my project?

Thank you for your time!

Copy link
Owner

@rhysd rhysd left a comment

Choose a reason for hiding this comment

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

Thank you. Supporting no_std would be useful. Before reviewing details in this branch, let me request some points:

  • Would you apply the following patch and revert the code commented out due to warnings?
    diff --git a/spec-test/src/main.rs b/spec-test/src/main.rs
    index ddbd226..d502206 100644
    --- a/spec-test/src/main.rs
    +++ b/spec-test/src/main.rs
    @@ -1,4 +1,5 @@
     #![forbid(unsafe_code)]
    +#![allow(dead_code)]
    
     mod crash;
     mod error;
    
  • Would you revert the package version bumps in this PR? I will do them in my side after understanding the changes. Since the major versions are zero, patch versions should be bumped if there are no breaking changes.
  • Could you run the spec test on this branch with enabling no_std and compare the results with main branch? If the numbers of passed test cases don't match, it would mean that there are some mistakes in the implementation added in this branch.
    • On my local machine, the results of the speci tests are:
      total: 19757, passed: 19460, failed: 179, skipped: 118
      

@IntegralPilot
Copy link
Author

Thank you for your reply!

I've reverted the version changes and commenting-out of "dead" code, as well as added #![allow(dead_code)] to the spec-tests.

With regards to the failing tests numbers, I've added a no_std test mode to the spec test (run with cargo run --features=no_std) and when running with this mode the test output and numbers of passed/failing/skipped tests are identical.

PS C:\Users\<username>\wain\spec-test> diff nostdtests.txt stdtests.txt

InputObject    SideIndicator
-----------    -------------
stdtests.txt   =>
nostdtests.txt <=

Is there anything else you would like?

Thanks again!

@IntegralPilot IntegralPilot requested a review from rhysd July 22, 2024 09:16
@rhysd
Copy link
Owner

rhysd commented Jul 23, 2024

Wow, passing the same spec-tests is great. I'll review the details. Since this PR is a bit larger, let me take a look at it on the weekend.

pub struct DefaultImporter<R: Read, W: Write> {
/// With `no_std` feature enabled, there is no `DefaultImporter`, it is up to you to create one suitable for your system.
#[cfg(not(feature = "no_std"))]
pub struct DefaultImporter<R, W>
Copy link
Owner

Choose a reason for hiding this comment

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

Question: Why DefaultImporter cannot be implemented when no_std?

use alloc::fmt;
use alloc::{borrow::Cow, vec::Vec};
#[cfg(feature = "hashbrown")]
use hashbrown::HashMap;
Copy link
Owner

Choose a reason for hiding this comment

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

alloc::collections::BTreeMap is available. Can we use BTreeMap instead of introducing a new dependency hashbrown on no_std? Or should we still use hashbrown?

https://github.com/rust-lang/rust/blob/80d8270d8488957f62fbf0df7a19dfe596be92ac/library/alloc/src/collections/mod.rs#L39

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.

2 participants