[Merged by Bors] - Enforce type safe usage of Handle::get#4794
[Merged by Bors] - Enforce type safe usage of Handle::get#4794DJMcNab wants to merge 5 commits intobevyengine:mainfrom
Conversation
|
Note to reviewers: ordinarily, I would flag this flavor of major API change as |
| fn get_asset<'world>( | ||
| id: &Handle<PngAsset>, | ||
| world: &'world World, | ||
| ) -> Option<&'world PngAsset> { |
There was a problem hiding this comment.
Note to reviewers: this is part of a test suite. Hence the overly broad name for a specific asset type.
I would probably rename the method here, but I don't feel strongly about it.
crates/bevy_ui/src/ui_node.rs
Outdated
|
|
||
| /// The image of the node | ||
| #[derive(Component, Clone, Debug, Reflect)] | ||
| #[derive(Component, Clone, Debug, Reflect, Deref)] |
There was a problem hiding this comment.
We should have DerefMut to match IMO.
There was a problem hiding this comment.
| #[derive(Component, Clone, Debug, Reflect, Deref)] | |
| #[derive(Component, Clone, Debug, Reflect, Deref, DerefMut)] |
There was a problem hiding this comment.
We should have a full review of which things internally should use the Deref derives, I guess.
alice-i-cecile
left a comment
There was a problem hiding this comment.
I'm really impressed at how painless this change was. I'm strongly in favor of more type safety here.
# Objective - Sometimes, people might load an asset as one type, then use it with an `Asset`s for a different type. - See e.g. #4784. - This is especially likely with the Gltf types, since users may not have a clear conceptual model of what types the assets will be. - We had an instance of this ourselves, in the `scene_viewer` example ## Solution - Make `Assets::get` require a type safe handle. --- ## Changelog ### Changed - `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. ### Added - `HandleUntyped::typed_weak`, a helper function for creating a weak typed version of an exisitng `HandleUntyped`. ## Migration Guide `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. If you were previously passing in: - a `HandleId`, use `&Handle::weak(id)` instead, to create a weak handle. You may have been able to store a type safe `Handle` instead. - a `HandleUntyped`, use `&handle_untyped.typed_weak()` to create a weak handle of the specified type. This is most likely to be the useful when using [load_folder](https://docs.rs/bevy_asset/latest/bevy_asset/struct.AssetServer.html#method.load_folder) - a `Handle<U>` of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) use `Handle::weak(handle.id)` to cast to a different type.
# Objective - Sometimes, people might load an asset as one type, then use it with an `Asset`s for a different type. - See e.g. bevyengine#4784. - This is especially likely with the Gltf types, since users may not have a clear conceptual model of what types the assets will be. - We had an instance of this ourselves, in the `scene_viewer` example ## Solution - Make `Assets::get` require a type safe handle. --- ## Changelog ### Changed - `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. ### Added - `HandleUntyped::typed_weak`, a helper function for creating a weak typed version of an exisitng `HandleUntyped`. ## Migration Guide `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. If you were previously passing in: - a `HandleId`, use `&Handle::weak(id)` instead, to create a weak handle. You may have been able to store a type safe `Handle` instead. - a `HandleUntyped`, use `&handle_untyped.typed_weak()` to create a weak handle of the specified type. This is most likely to be the useful when using [load_folder](https://docs.rs/bevy_asset/latest/bevy_asset/struct.AssetServer.html#method.load_folder) - a `Handle<U>` of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) use `Handle::weak(handle.id)` to cast to a different type.
# Objective - Sometimes, people might load an asset as one type, then use it with an `Asset`s for a different type. - See e.g. bevyengine#4784. - This is especially likely with the Gltf types, since users may not have a clear conceptual model of what types the assets will be. - We had an instance of this ourselves, in the `scene_viewer` example ## Solution - Make `Assets::get` require a type safe handle. --- ## Changelog ### Changed - `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. ### Added - `HandleUntyped::typed_weak`, a helper function for creating a weak typed version of an exisitng `HandleUntyped`. ## Migration Guide `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. If you were previously passing in: - a `HandleId`, use `&Handle::weak(id)` instead, to create a weak handle. You may have been able to store a type safe `Handle` instead. - a `HandleUntyped`, use `&handle_untyped.typed_weak()` to create a weak handle of the specified type. This is most likely to be the useful when using [load_folder](https://docs.rs/bevy_asset/latest/bevy_asset/struct.AssetServer.html#method.load_folder) - a `Handle<U>` of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) use `Handle::weak(handle.id)` to cast to a different type.
Objective
Assets for a different type.scene_viewerexampleSolution
Assets::getrequire a type safe handle.Changelog
Changed
Assets::<T>::getandAssets::<T>::get_mutnow require that the passed handles areHandle<T>, improving the type safety of handles.Added
HandleUntyped::typed_weak, a helper function for creating a weak typed version of an exisitngHandleUntyped.Migration Guide
Assets::<T>::getandAssets::<T>::get_mutnow require that the passed handles areHandle<T>, improving the type safety of handles. If you were previously passing in:HandleId, use&Handle::weak(id)instead, to create a weak handle. You may have been able to store a type safeHandleinstead.HandleUntyped, use&handle_untyped.typed_weak()to create a weak handle of the specified type. This is most likely to be the useful when using load_folderHandle<U>of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) useHandle::weak(handle.id)to cast to a different type.