Skip to content

Modify derive_label to support no_std environments #15465

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

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions crates/bevy_macro_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,26 @@ pub fn derive_label(
.unwrap(),
);
quote! {
impl #impl_generics #trait_path for #ident #ty_generics #where_clause {
fn dyn_clone(&self) -> ::std::boxed::Box<dyn #trait_path> {
::std::boxed::Box::new(::core::clone::Clone::clone(self))
}
// To ensure alloc is available, but also prevent its name from clashing, we place the implementation inside an anonymous constant
const _: () = {
Copy link
Member

Choose a reason for hiding this comment

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

Why this is needed? 🤔

Copy link
Contributor Author

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
};

Copy link
Member

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!

extern crate alloc;

fn as_dyn_eq(&self) -> &dyn #dyn_eq_path {
self
}
impl #impl_generics #trait_path for #ident #ty_generics #where_clause {
fn dyn_clone(&self) -> alloc::boxed::Box<dyn #trait_path> {
alloc::boxed::Box::new(::core::clone::Clone::clone(self))
}

fn dyn_hash(&self, mut state: &mut dyn ::core::hash::Hasher) {
let ty_id = ::core::any::TypeId::of::<Self>();
::core::hash::Hash::hash(&ty_id, &mut state);
::core::hash::Hash::hash(self, &mut state);
fn as_dyn_eq(&self) -> &dyn #dyn_eq_path {
self
}

fn dyn_hash(&self, mut state: &mut dyn ::core::hash::Hasher) {
let ty_id = ::core::any::TypeId::of::<Self>();
::core::hash::Hash::hash(&ty_id, &mut state);
::core::hash::Hash::hash(self, &mut state);
}
}
}
};
}
.into()
}