-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Add weak_handle!
convenience macro
#17384
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BenjaminBrienen
approved these changes
Jan 22, 2025
Bleachfuel
approved these changes
Feb 3, 2025
alice-i-cecile
approved these changes
Feb 5, 2025
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.
Much nicer. I'd like to see the internal usages migrated in follow-up I think.
Merged
github-merge-queue bot
pushed a commit
that referenced
this pull request
Feb 5, 2025
# Objective - Make use of the new `weak_handle!` macro added in #17384 ## Solution - Migrate bevy from `Handle::weak_from_u128` to the new `weak_handle!` macro that takes a random UUID - Deprecate `Handle::weak_from_u128`, since there are no remaining use cases that can't also be addressed by constructing the type manually ## Testing - `cargo run -p ci -- test` --- ## Migration Guide Replace `Handle::weak_from_u128` with `weak_handle!` and a random UUID.
mrchantey
pushed a commit
to mrchantey/bevy
that referenced
this pull request
Feb 17, 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!`](https://docs.rs/uuid/1.12.0/uuid/macro.uuid.html) 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: ```rust const SHADER: Handle<Shader> = Handle::weak_from_u128(314685653797097581405914117016993910609); ``` After this PR: ```rust 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.
mrchantey
pushed a commit
to mrchantey/bevy
that referenced
this pull request
Feb 17, 2025
# Objective - Make use of the new `weak_handle!` macro added in bevyengine#17384 ## Solution - Migrate bevy from `Handle::weak_from_u128` to the new `weak_handle!` macro that takes a random UUID - Deprecate `Handle::weak_from_u128`, since there are no remaining use cases that can't also be addressed by constructing the type manually ## Testing - `cargo run -p ci -- test` --- ## Migration Guide Replace `Handle::weak_from_u128` with `weak_handle!` and a random UUID.
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-Ready-For-Final-Review
This PR has been approved by the community. It's ready for a maintainer to consider merging it
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Handle
with a static, random ID and fill it duringPlugin::build
viaload_internal_asset!
xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx
, wherex
is a random nibble (in practice this doesn't matter, since the UUID is just interpreted as a bag of bytes).Solution
weak_handle!
macro that internally callsuuid::uuid!
to parse a UUID from a string literal.uuidgen
or entering "uuid" in DuckDuckGo.Previously:
After this PR:
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.