Skip to content

bevy_reflect: Split up the std module #18939

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
May 26, 2025

Conversation

MrGVSV
Copy link
Member

@MrGVSV MrGVSV commented Apr 26, 2025

Important

To maintainers: we should wait to merge this one in until after #18944 lands so that cherry-picking the latter for 0.16.1 is simpler.

Objective

The std module is where we implement the reflection traits for types that are exported from std (including core and alloc). Over time, this file has grown increasingly large, making it difficult to navigate and a pain point for merge conflicts.

The goal of this PR is to break up the module into smaller chunks.

Solution

The std module has been split into many submodules:

  • alloc
  • bevy_platform
  • core
  • std

Each of these new modules is comprised of submodules that closely resemble the actual module. For example, the impls for ::alloc::vec::Vec have been moved to bevy_reflect::impls::alloc::vec::Vec.

Some liberties were taken. For example, Cow<'static, Path> was kept in bevy_reflect::impls::std::path rather than bevy_reflect::impls::alloc::borrow.

You may ask: Isn't this a little overkill? Why does the one-line impl for TypeId need its own file?

And yes, it is partly overkill. But the benefit with this approach is that where an std-related type should live is mostly unambiguous. If we wanted to reflect ::core::net::Ipv4Addr, it's very clear that it should be done in bevy_reflect::impls::core::net.

We can discuss better ways of breaking this up if people have other ideas or opinions, but I think this is a pretty straightforward way of doing it.

Note to Reviewers

The code is pretty much copy-paste from the mega module to the new submodules. It's probably best to focus efforts on reviewing the general module structure, as well as maybe which impls are included where.

You can review the code contained within each impl, but I promise you the only thing I touched were the paths in the macros so they could be more hygienic :)

Testing

You can just check that everything compiles still:

cargo check -p bevy_reflect --tests

@MrGVSV MrGVSV added C-Code-Quality A section of code that is hard to understand or change A-Reflection Runtime information about types D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 26, 2025
Copy link
Contributor

@hukasu hukasu left a comment

Choose a reason for hiding this comment

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

The files that have only one item could be moved to the modules root, though

@MrGVSV
Copy link
Member Author

MrGVSV commented Apr 26, 2025

The files that have only one item could be moved to the modules root, though

Possibly. However that risks growing in size too unless we remember to break things up once it gets too large. It also struggles with discoverability (i.e. it's slightly harder to find an impl based on the folder structure alone).

I’m also just of the mindset that mod.rs files should simply act as a directory and a place for module-related tests. So maybe I’m a little biased there haha

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/split-std branch from 9e8590b to 0f7ecf3 Compare April 26, 2025 04:36
@bushrat011899
Copy link
Contributor

I like this, but I have one suggestion: split into core, alloc, and std. This has a secondary benefit in that the std feature gates could all be combined together into a single:

#[cfg(feature ="std")]
mod std;

Otherwise, totally agree. I hate large files like that, and git diffs generally do too. Even single-line files are nice IMO, as long as the structure of the modules makes sense (which it does, since it mirrors a particular existing structure).

@MrGVSV
Copy link
Member Author

MrGVSV commented Apr 26, 2025

I like this, but I have one suggestion: split into core, alloc, and std. This has a secondary benefit in that the std feature gates could all be combined together into a single:

#[cfg(feature ="std")]
mod std;

Otherwise, totally agree. I hate large files like that, and git diffs generally do too. Even single-line files are nice IMO, as long as the structure of the modules makes sense (which it does, since it mirrors a particular existing structure).

You know what, that's a really good idea. I'll try doing that!

@MrGVSV MrGVSV added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 26, 2025
@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/split-std branch from c16aa9c to c3ac18c Compare April 26, 2025 18:58
@MrGVSV MrGVSV added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 26, 2025
@MrGVSV
Copy link
Member Author

MrGVSV commented Apr 26, 2025

Alright, I split it up even further into alloc, core, and bevy_platform!

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

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

Just some minor comments, nothing blocking. LGTM!

@@ -0,0 +1,18 @@
#[cfg(any(unix, windows))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Maybe move these gates up a level in the module tree:

#[cfg(any(unix, windows))]
mod ffi;

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh actually we can't do this since the other gate is the inverse (i.e. cfg(not(any(unix, windows)))]).

::core::sync::atomic::AtomicUsize,
::core::sync::atomic::Ordering::SeqCst
);
#[cfg(target_has_atomic = "64")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Future PR: It'd be nice to move these to the bevy_platform implementation module, since we can remove these target_has_atomic feature gates.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh? How so?

Copy link
Contributor

Choose a reason for hiding this comment

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

bevy_platform has a sync::atomic module which includes all those target_has_atomic flags already, and for unsupported types, it uses portable-atomic as the fallback.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh okay gotcha. Yeah that might be better served as a followup. I'll try to remember to make an issue or PR once this one lands

@MrGVSV MrGVSV added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 27, 2025
@bushrat011899 bushrat011899 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 7, 2025
@alice-i-cecile
Copy link
Member

@MrGVSV once merge conflicts are fixed I'll get this in so it doesn't accumulate more.

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/split-std branch from c3ac18c to b7a1c79 Compare May 26, 2025 18:36
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 26, 2025
Merged via the queue into bevyengine:main with commit 1674938 May 26, 2025
32 checks passed
@MrGVSV MrGVSV deleted the mrgvsv/reflect/split-std branch May 26, 2025 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Reflection Runtime information about types C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants