bevy/crates/bevy_render/src/texture
Jakob Hellermann f867319336 add ReflectAsset and ReflectHandle (#5923)
# Objective
![image](https://user-images.githubusercontent.com/22177966/189350194-639a0211-e984-4f73-ae62-0ede44891eb9.png)

^ enable this

Concretely, I need to
- list all handle ids for an asset type
- fetch the asset as `dyn Reflect`, given a `HandleUntyped`
- when encountering a `Handle<T>`, find out what asset type that handle refers to (`T`'s type id) and turn the handle into a `HandleUntyped`

## Solution

- add `ReflectAsset` type containing function pointers for working with assets
```rust
pub struct ReflectAsset {
    type_uuid: Uuid,
    assets_resource_type_id: TypeId, // TypeId of the `Assets<T>` resource

    get: fn(&World, HandleUntyped) -> Option<&dyn Reflect>,
    get_mut: fn(&mut World, HandleUntyped) -> Option<&mut dyn Reflect>,
    get_unchecked_mut: unsafe fn(&World, HandleUntyped) -> Option<&mut dyn Reflect>,
    add: fn(&mut World, &dyn Reflect) -> HandleUntyped,
    set: fn(&mut World, HandleUntyped, &dyn Reflect) -> HandleUntyped,
    len: fn(&World) -> usize,
    ids: for<'w> fn(&'w World) -> Box<dyn Iterator<Item = HandleId> + 'w>,
    remove: fn(&mut World, HandleUntyped) -> Option<Box<dyn Reflect>>,
}
```
- add `ReflectHandle` type relating the handle back to the asset type and providing a way to create a `HandleUntyped`
```rust
pub struct ReflectHandle {
    type_uuid: Uuid,
    asset_type_id: TypeId,
    downcast_handle_untyped: fn(&dyn Any) -> Option<HandleUntyped>,
}
```
- add the corresponding `FromType` impls
- add a function `app.register_asset_reflect` which is supposed to be called after `.add_asset` and registers `ReflectAsset` and `ReflectHandle` in the type registry
---

## Changelog

- add `ReflectAsset` and `ReflectHandle` types, which allow code to use reflection to manipulate arbitrary assets without knowing their types at compile time
2022-10-28 20:42:33 +00:00
..
basis.rs update wgpu to 0.13 (#5168) 2022-07-14 21:17:16 +00:00
dds.rs dds: Ensure the Extent3d for compressed textures represents the physical size (#5406) 2022-07-20 22:43:36 +00:00
fallback_image.rs Make Resource trait opt-in, requiring #[derive(Resource)] V2 (#5577) 2022-08-08 21:36:35 +00:00
hdr_texture_loader.rs update image to 0.24 (#4121) 2022-05-28 02:00:55 +00:00
image.rs add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
image_texture_conversion.rs Expose Image conversion functions (fixes #5452) (#5527) 2022-09-03 17:47:38 +00:00
image_texture_loader.rs Do not crash if RenderDevice doesn't exist (#4427) 2022-04-05 19:37:23 +00:00
ktx2.rs Support array / cubemap / cubemap array textures in KTX2 (#5325) 2022-07-30 07:02:58 +00:00
mod.rs add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
texture_cache.rs Make Resource trait opt-in, requiring #[derive(Resource)] V2 (#5577) 2022-08-08 21:36:35 +00:00