-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Remove incorrect equality comparisons for asset load error types #15890
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
Conversation
Could you either open your PR against @JoJoJet branch so that they can merge it, or copy the original title/description in this PR? Same for sections of the description, it's better for a PR to be self contained rather than have to refer to another. Some are also used by automated tools which don't know how to follow redirections |
Done! |
Please feel free to leave a reply on my review comments and then I'll feel comfortable refining the PR with that feedback. |
@alice-i-cecile feedback is addressed I also realized that the merge conflict involved 2 new error types, which should have the same process done to them. It is much better now, I think. I'll update the migration guide. |
@andriyDev I've gone through and double checked all the assertions. Sorry for the copy paste errors! |
Objective
The type
AssetLoadError
hasPartialEq
andEq
impls, which is problematic due to the fact that theAssetLoaderError
andAddAsyncError
variants lie in their impls: they will returntrue
for anyBox<dyn Error>
with the sameTypeId
, even if the actual value is different. This can lead to subtle bugs if a user relies on the equality comparison to ensure that two values are equal.The same is true for
DependencyLoadState
,RecursiveDependencyLoadState
.More generally, it is an anti-pattern for large error types involving dynamic dispatch, such as
AssetLoadError
, to have equality comparisons. Directly comparing two errors for equality is usually not desired -- if some logic needs to branch based on the value of an error, it is usually more correct to check for specific variants and inspect their fields.As far as I can tell, the only reason these errors have equality comparisons is because the
LoadState
enum wrapsAssetLoadError
for itsFailed
variant. This equality comparison is only used to check for== LoadState::Loaded
, which we can easily replace with anis_loaded
method.Solution
Remove the
{Partial}Eq
impls fromLoadState
, which also allows us to remove it from the error types.Adopted from #14428 by @JoJoJet.
Migration Guide
The types
bevy_asset::AssetLoadError
andbevy_asset::LoadState
no longer support equality comparisons. If you need to check for an asset's load state, consider checking for a specific variant usingLoadState::is_loaded
or thematches!
macro. Similarly, consider using thematches!
macro to check for specific variants of theAssetLoadError
type if you need to inspect the value of an asset load error in your code.DependencyLoadState
andRecursiveDependencyLoadState
are not released yet, so no migration needed,