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 #374

Merged
merged 23 commits into from
Mar 8, 2025
Merged

Add no_std Support #374

merged 23 commits into from
Mar 8, 2025

Conversation

bushrat011899
Copy link
Contributor

Objective

Add no_std support to codespan-reporting, allowing its use in initiatives such as wasm32v1-none support in wgpu.

Solution

  • Feature-gated termcolor, as it does not have no_std support
  • Added #![no_std] to all 3 crates. Noting that codespan-lsp does not have no_std support as lsp-types does not haveno_std support.
  • Added std and termcolor features to codespan-reporting and codespan, which are both enabled by default.
  • Various minor changes as suggested by clippy

Notes

This is my first time contributing to this project. I have attempted to split my changes into meaningful commits with descriptions as described in the CONTRIBUTING.md document. Please let me know if there's anything I can do to assist with reviewing and merging this PR.

This is effectively required for `no_std` support, as without it, dev-dependencies will enable features on regular dependencies, making it impossible to have `std` tests in `no_std` crates.
These clippy lints highlight cases where an import is referencing `std` or `alloc` when it instead could import from `alloc` or `core`. This helps expose the "true" extent of `std` usage in a crate.
`termcolor` is incompatible with `no_std`, but `codespan` and `codespan-reporting` have valid use even without this crate. Making it optional allows `no_std` to be entirely decided by the contents of the codespan crates themselves.

Serde is also `no_std` compatible, as long as we disable default features.
During testing, `unicode-width` caused `cargo msrv find` to report `codespan-reporting`'s MSRV was 1.66, violating the current MSRV of 1.46. Since this is already the case, and there are features in 1.67, such as ilog10, which are desired within this crate, I have increased the MSRV to 1.67.
Before making substantive changes, `clippy` reported that these implementations for default can be derived. I have done so to silence the lint.
`Span::from_str` shadows `FromStr::from_str`. Instead of breaking the public API, I added an explicit `allow`
Caught by clippy, and actioned to reduce warnings
`Renderer::render_snippet_source` has too many arguments. Instead of breaking the public API, I'm suppressing this lint.
These double-references are automatically dereferenced anyway.
Increasing MSRV allows using the new `ilog10` method, solving a long-standing TODO
`ToString::to_string()` is preferred by `clippy` for converting `str` to `String`
`assert_eq` on a boolean is linted against by `clippy`, suggesting to use `assert!` instead.
The `Styles` type is only needed for controlling colour, which is only needed with `termcolor`. Since we only deal with UTF8 strings, `std::fmt::Write` is equivalent to `WriteColor` when colour isn't required.
Switch imports to relying on `core` and `alloc`. `std::io::Write` can be replaced by `core::fmt::Write` since we deal exclusively with UTF8 strings.
This checks for `no_std` compatibility on 3 key targets:
- `x86_64-unknown-none`: represents desktop `no_std`, such as in kernels
- `wasm32v1-none`: represents Web builds using the most broadly support WASM target
-`thumbv6m-none-eabi`: represents typical embedded targets, such as the Raspberry Pi Pico

Testing on all three platforms ensures the widest possible compatibility.
@kaleidawave
Copy link
Collaborator

kaleidawave commented Mar 3, 2025

Thanks for the change, would be good if this crate can work in more places!

(I need to update the CI, ignore the other errors) but I think cargo fmt does need running on this PR.

Will review the code before merging later this week ~ Thursday/Friday.

- `cargo fmt`
- Fix typo in `ci.yml` which misused the matrix
@bushrat011899
Copy link
Contributor Author

Ran cargo fmt and also fixed a typo I had in the new check-no-std CI task I wrote (matrix wasn't enumerating properly).

@kaleidawave
Copy link
Collaborator

Ran cargo fmt and also fixed a typo I had in the new check-no-std CI task I wrote (matrix wasn't enumerating properly).

Thanks!

Will double check and merge later. I don't know whether the CI issue is something from this change or something generally related to CI of which I should handle in #373

Copy link
Collaborator

@kaleidawave kaleidawave left a comment

Choose a reason for hiding this comment

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

Looks good!

I think the WASM CI needs some changes as it is currently showing error: component 'rust-std' for target 'wasm32v1-none' is unavailable for download for channel '1.82.0'.

Once that is resolved and the new job is green I will merge.

I have mentioned a few other things that you can (but don't necessarily need to) amend.

@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

The minimum supported rustc version is now `1.46.0` (was `1.40.0`).
The minimum supported rustc version is now `1.67.0` (was `1.40.0`).
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is a reasonable demand in 2025 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think it's been long enough haha!

@@ -26,5 +27,13 @@ rustyline = "6"
unindent = "0.1"

[features]
default = ["std", "termcolor"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, adding default should not cause breaking changes (unless a dependent specified no-default-features which would be pointless)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it's still technically a breaking change semantically, so you'll want to bump the version number for a release, but it's the nice kind of breaking change where migrating should be just enabling these features.

// Use a saturating_add because in that edge case the number of digits
// will not be changed.
(n.saturating_add(1) as f64).log10().ceil() as usize
n.ilog10() as usize + 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that rust-lang/rust#70887 was resolved. I think this is okay to be sneaked into this pr 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah without this change the MSRV is 1.66, and with it's 1.67, so I figured why not, I'm already bumping the MSRV, and it actually solves a problem too, since no_std removes some of the floating point operations like log10!

bushrat011899 and others added 4 commits March 8, 2025 20:42
`wasm32v1-none` was only added in 1.84, so I've set that as the earliest Rust version used for testing `no_std` compatibility. Note that it's typical for embedded development to be done on nightly anyway, so `no_std` will usually happen on the very latest version of Rust.
@bushrat011899
Copy link
Contributor Author

Conflicts resolved and review items addressed. I believe the CI task should pass now too!

Not currently used and is incompatible with `thumbv6m-none-eabi`
@bushrat011899
Copy link
Contributor Author

Fixed a minor issue with thumbv6m-none-eabi and formatting from the upstream merge

@kaleidawave

This comment was marked as outdated.

@kaleidawave
Copy link
Collaborator

kaleidawave commented Mar 8, 2025

Stable check, test, format all work as well on these new targets!

Thanks for the quick updates. Anything else before merge?

Once merged, I will hopefully have a release within the next 7 days. Just waiting for some publishing keys from the owner of the repository :)

@bushrat011899
Copy link
Contributor Author

Nope, I think this should be good to go! Thanks for being so receptive to the changes. Looking forward to the new release!

@kaleidawave kaleidawave merged commit 0f2450a into brendanzab:master Mar 8, 2025
21 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants