bevy icon indicating copy to clipboard operation
bevy copied to clipboard

implement `TypeUuid` for primitives

Open dis-da-mor opened this issue 2 years ago • 4 comments

Objective

  • Fixes #5432

Solution

  • move code responsible for generating the impl TypeUuid from type_uuid_derive into a new function, gen_impl_type_uuid.
  • this allows the new proc macro, impl_type_uuid, to call the code for generation.
  • added struct TypeUuidDef and implemented syn::Parse to allow parsing of the input for the new macro.
  • finally, used the new macro impl_type_uuid to implement TypeUuid for the standard library (in crates/bevy_reflect/src/type_uuid_impl.rs).

dis-da-mor avatar Nov 15 '22 18:11 dis-da-mor

Generic types should not have a single static UUID.

jakobhellermann avatar Nov 15 '22 21:11 jakobhellermann

Generic types should not have a single static UUID.

I'm pretty sure they don't, this macro generates the same code as the derive, where a generic type's UUID will be a composite of their own uuid and their parameter's uuid.

dis-da-mor avatar Nov 16 '22 08:11 dis-da-mor

does anyone know why compilation sometimes fails due to the smallvec import?

dis-da-mor avatar Nov 16 '22 08:11 dis-da-mor

does anyone know why compilation sometimes fails due to the smallvec import?

It's because smallvec is gated behind a feature.

MrGVSV avatar Nov 16 '22 12:11 MrGVSV

Can you implement TypeUuid for arrays? Ideally [T; 1] and [T; 2] should have different TYPE_UUID.

harudagondi avatar Nov 17 '22 00:11 harudagondi

Can you implement TypeUuid for arrays? Ideally [T; 1] and [T; 2] should have different TYPE_UUID.

I think it can be implemented for [T; N], by converting N to a u128 and then to a Uuid from that, and then compositing T::TYPE_UUID, a static uuid for all arrays, and N::TYPE_UUID. Does that make sense?

dis-da-mor avatar Nov 17 '22 07:11 dis-da-mor

On the topic of handling arrays, do we handle tuples as well? If not, that might need to be added for completeness (up to twelve fields to match Reflect and other std impls).

MrGVSV avatar Nov 17 '22 17:11 MrGVSV

I've had to move the all_tuples macro outside of bevy_ecs in order to avoid a circular dependency. I've moved it into a new proc macro crate in bevy_utils called bevy_utils_macros. This seemed the most suitable place to me but lmk if there is a better place.

dis-da-mor avatar Dec 21 '22 13:12 dis-da-mor

the name bevy_utils_macros is very confusing when we already have a crate named bevy_macro_utils. i agree that bevy_ecs is not the best place for the macro but i'm not settled on the name and perhaps this should be part of a separate pr.

soqb avatar Dec 22 '22 09:12 soqb

i'm happy for you to revert the change to using all_tuples! for the tuple implementation. i had originally thought we already had access to it (in bevy_reflect) for implementing Reflect, but that uses a custom macro - my mistake.

soqb avatar Dec 22 '22 09:12 soqb

well the only reason it needs to be in a separate crate is that it's a proc macro. I could move it as a sub-crate of bevy_macro_utils called bevy_macro_utils_proc. I've already done all the refactoring work and if it's just the name that's the issue that could change.

dis-da-mor avatar Dec 22 '22 11:12 dis-da-mor

bevy_macro_utils is not the correct place for the macro imo. the macro utils crate is for crates like bevy_reflect_derive to use, not for a crate like bevy_reflect. i'm not really sure where the correct place for the macro is. bevy_utils seems like a sound fit but our current naming scheme of bevy_xxx_{macros, derive} doesn't fit very well here.

@alice-i-cecile - can you provide some guidance here?

soqb avatar Dec 22 '22 14:12 soqb

Hmm, I agree with your analysis @soqb.

bevy_macro_utils_proc

This seems fine for now. @mockersf may have better ideas: he's very good at this sort of organizational thinking.

alice-i-cecile avatar Dec 22 '22 19:12 alice-i-cecile

i don't think it's correct to put all_tuples in bevy_macro_utils so perhaps bevy_utils_proc_macros?

soqb avatar Dec 23 '22 08:12 soqb

LGTM!

considering the all_tuples! move this should have C-Breaking-Change.

Is all_tuples! public API?

harudagondi avatar Jan 08 '23 02:01 harudagondi

https://docs.rs/bevy/latest/bevy/ecs/macro.all_tuples.html

Apparently yes, although it has no docs.

alice-i-cecile avatar Jan 08 '23 05:01 alice-i-cecile

@dis-da-moe can you please rebase? This is worth including in the release.

alice-i-cecile avatar Feb 13 '23 18:02 alice-i-cecile

bors r+

alice-i-cecile avatar Feb 16 '23 16:02 alice-i-cecile