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

Fixed: Build in no_std environment #36

Closed
wants to merge 2 commits into from

Conversation

Sewer56
Copy link

@Sewer56 Sewer56 commented Feb 11, 2024

This simple PR makes it possible to build the library in a no_std environment once again.

Although the library itself is no_std, it's being tested under an std environment; as a result of this, implicit use(s) that are automatically imported by std apply while building tests, but not while building from an actual no_std environment.

In this case, we needed to specify the imports from alloc manually.

@athre0z
Copy link
Member

athre0z commented Feb 12, 2024

Ah yeah, good catch. I intended to initially make the encoder feature depend on the std feature, but it looks like I forgot to actually do that.

It's also good to know that our tests currently don't properly catch such cases. We should probably add a cargo test --no-default-features --features <all features that are supposed to work without std feature> run in the text matrix, but that doesn't have to happen in the scope of this PR (unless you feel like it).

IMO we shouldn't just require alloc to be present in no_std builds. We have two alternatives here:

  • Disable the encode function returning a Vec if the std feature isn't enabled. Users can still use encode_into with a buffer that they allocated themselves (either with alloc crate or stack array). Rework tests to use encode_into or also just disable them in no-libc mode until we can be bothered to rework them. extern crate alloc not required.
  • Add a separate alloc feature flag and make the encode(..) -> Vec<...> function + extern crate alloc depend on that.

Personally I don't have a strong opinion here. The second variant might be nicer in the long run, because it'd also allow using the formatter in no_std environments.

src/encoder.rs Outdated Show resolved Hide resolved
@Sewer56
Copy link
Author

Sewer56 commented Feb 13, 2024

Did you also see my comment above? 😅

Yup, I saw the comment. Seems I just tabbed out for work stuff and forgot to come back.


Anyway, I think the second option is probably more preferable.

I feel like allowing the user to 'opt-into' functionality is preferable, compared to effectively locking them out (by asking for full std to get alloc)

Although I myself am not an embedded programmer,

I cannot really imagine a world where you'd be programming without any sort of allocator, even if it allocates in some memory block at the end (top) of the stack.

Assuming there are a bunch of no_std users who do have some form of alloc, it'd feel bad to exclude functionality they can still technically access without full std.

@athre0z
Copy link
Member

athre0z commented Feb 13, 2024

Yup, I saw the comment. Seems I just tabbed out for work stuff and forgot to come back.

Cool, no pressure or hurry: it just seemed like you might have missed it since you immediately fixed the one thing and then went idle. :)

I cannot really imagine a world where you'd be programming without any sort of allocator, even if it allocates in some memory block at the end (top) of the stack.

So far I've managed to avoid need for a dynamic allocator in all my no-libc projects. But I agree with option 2 being preferable in general.

Assuming there are a bunch of no_std users who do have some form of alloc, it'd feel bad to exclude functionality they can still technically access without full std.

Yeah. The separate alloc feature is also what most other crates with no_std do, I think.

@athre0z
Copy link
Member

athre0z commented Mar 9, 2024

Superseded by #37

@athre0z athre0z closed this Mar 9, 2024
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