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

Add weak_handle! convenience macro #17384

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

Conversation

SludgePhD
Copy link
Contributor

@SludgePhD SludgePhD commented Jan 15, 2025

Objective

  • A common bevy pattern is to pre-allocate a weak Handle with a static, random ID and fill it during Plugin::build via load_internal_asset!
  • This requires generating a random 128-bit number that is interpreted as a UUID. This is much less convenient than generating a UUID directly, and also, strictly speaking, error prone, since it often results in an invalid UUIDv4 – they have to follow the pattern xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx, where x is a random nibble (in practice this doesn't matter, since the UUID is just interpreted as a bag of bytes).

Solution

  • Add a weak_handle! macro that internally calls uuid::uuid! to parse a UUID from a string literal.
  • Now any random UUID generation tool can be used to generate an asset ID, such as uuidgen or entering "uuid" in DuckDuckGo.

Previously:

const SHADER: Handle<Shader> = Handle::weak_from_u128(314685653797097581405914117016993910609);

After this PR:

const SHADER: Handle<Shader> = weak_handle!("1347c9b7-c46a-48e7-b7b8-023a354b7cac");

Note that I did not yet migrate any of the existing uses. I can do that if desired, but want to have some feedback first to avoid wasted effort.

Testing

Tested via the included doctest.

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 15, 2025
@BenjaminBrienen BenjaminBrienen added the D-Straightforward Simple bug fixes and API improvements, docs, test and examples label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants