-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Modify derive_label
to support no_std
environments
#15465
Modify derive_label
to support no_std
environments
#15465
Conversation
We can't generate code that uses `::std::boxed::Box` in `no_std` environments, but we also can't rely on `::alloc::boxed::Box`, since the user might not have declared `extern crate alloc`. To resolve this, the generated code is wrapped in an anonymous constant which contains the `extern crate alloc` invocation. This does mean the macro is no longer hygienic against cases where the user provides an alternate `alloc` crate, however I believe this is an acceptable compromise.
fn dyn_clone(&self) -> ::std::boxed::Box<dyn #trait_path> { | ||
::std::boxed::Box::new(::core::clone::Clone::clone(self)) | ||
} | ||
const _: () = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is needed? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you check the description of the PR the goal is to allow invoking this macro in no_std
crates, which is required for Bevy to become no_std
.
We would normally use the fully qualified name:
::std::boxed::Box
However, in no_std
we can't use the std
namespace, so instead we need to use the original definition of Box
in the alloc
crate:
::alloc::boxed::Box
But that also doesn't work, because access to the alloc
namespace requires explicitly linking it in the end-user's crate:
extern crate alloc;
So we need to include that extern
statement. However, we can't place it in the global namespace, because it would then clash with other macro invocations.
To ensure alloc
is available, but also prevent its name from clashing, we place the implementation inside an anonymous constant:
const _: () = {
// private namespace so no clashes
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just not understand why code put in const _: () = { ... }
. Thanks for detailed explanation!
) # Objective - Contributes to bevyengine#15460 ## Solution - Wrap `derive_label` `quote!` in an anonymous constant which contains an `extern crate alloc` statement, allowing use of the `alloc` namespace even when a user has not brought in the crate themselves. ## Testing - CI passed locally. ## Notes We can't generate code that uses `::std::boxed::Box` in `no_std` environments, but we also can't rely on `::alloc::boxed::Box` either, since the user might not have declared `extern crate alloc`. To resolve this, the generated code is wrapped in an anonymous constant which contains the `extern crate alloc` invocation. This does mean the macro is no longer hygienic against cases where the user provides an alternate `alloc` crate, however I believe this is an acceptable compromise. Additionally, this crate itself doesn't need to be `no_std`, it just needs to _generate_ `no_std` compatible code. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
) # Objective - Contributes to bevyengine#15460 ## Solution - Wrap `derive_label` `quote!` in an anonymous constant which contains an `extern crate alloc` statement, allowing use of the `alloc` namespace even when a user has not brought in the crate themselves. ## Testing - CI passed locally. ## Notes We can't generate code that uses `::std::boxed::Box` in `no_std` environments, but we also can't rely on `::alloc::boxed::Box` either, since the user might not have declared `extern crate alloc`. To resolve this, the generated code is wrapped in an anonymous constant which contains the `extern crate alloc` invocation. This does mean the macro is no longer hygienic against cases where the user provides an alternate `alloc` crate, however I believe this is an acceptable compromise. Additionally, this crate itself doesn't need to be `no_std`, it just needs to _generate_ `no_std` compatible code. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Objective
no_std
Bevy #15460Solution
derive_label
quote!
in an anonymous constant which contains anextern crate alloc
statement, allowing use of thealloc
namespace even when a user has not brought in the crate themselves.Testing
Notes
We can't generate code that uses
::std::boxed::Box
inno_std
environments, but we also can't rely on::alloc::boxed::Box
either, since the user might not have declaredextern crate alloc
. To resolve this, the generated code is wrapped in an anonymous constant which contains theextern crate alloc
invocation.This does mean the macro is no longer hygienic against cases where the user provides an alternate
alloc
crate, however I believe this is an acceptable compromise.Additionally, this crate itself doesn't need to be
no_std
, it just needs to generateno_std
compatible code.