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

Don't extern alloc unless needed to avoid requiring global allocators in #[no_std] environments #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

udoprog
Copy link

@udoprog udoprog commented Apr 26, 2024

Disabling the std feature causes extern crate alloc to be used, which in turn requires any downstream #[no_std] users to define a global allocator with a message like this:

error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait

This change ensures that we only use extern crate alloc when it's actually needed, which is when the write feature is enabled.

Also note that std::string::String and std::vec::Vec are aliases for the corresponding alloc types, so importing it through std when it's available is not necessary.

It would arguably be cleaner for #![no_std] to always be enabled and add the following where std types and items are used, but changing this is up to you:

#[cfg(feature = "std")]
extern crate std;

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.

None yet

1 participant