bevy/crates/bevy_asset/src
Gino Valente 4d0961cc8a
bevy_reflect: Add ReflectRef/ReflectMut/ReflectOwned convenience casting methods (#15235)
# Objective

#13320 added convenience methods for casting a `TypeInfo` into its
respective variant:

```rust
let info: &TypeInfo = <Vec<i32> as Typed>::type_info();

// We know `info` contains a `ListInfo`, so we can simply cast it:
let list_info: &ListInfo = info.as_list().unwrap();
```

This is especially helpful when you have already verified a type is a
certain kind via `ReflectRef`, `ReflectMut`, `ReflectOwned`, or
`ReflectKind`.

As mentioned in that PR, though, it would be useful to add similar
convenience methods to those types as well.

## Solution

Added convenience casting methods to `ReflectRef`, `ReflectMut`, and
`ReflectOwned`.

With these methods, I was able to reduce our nesting in certain places
throughout the crate.

Additionally, I took this opportunity to move these types (and
`ReflectKind`) to their own module to help clean up the `reflect`
module.

## Testing

You can test locally by running:

```
cargo test --package bevy_reflect --all-features
```

---

## Showcase

Convenience methods for casting `ReflectRef`, `ReflectMut`, and
`ReflectOwned` into their respective variants has been added! This
allows you to write cleaner code if you already know the kind of your
reflected data:

```rust
// BEFORE
let ReflectRef::List(list) = list.reflect_ref() else {
    panic!("expected list");
};

// AFTER
let list = list.reflect_ref().as_list().unwrap();
```

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Pablo Reinhardt <126117294+pablo-lua@users.noreply.github.com>
2024-09-23 16:50:46 +00:00
..
io Fix doc link import style to avoid unused_imports (#15337) 2024-09-21 00:04:32 +00:00
processor Fix doc link import style to avoid unused_imports (#15337) 2024-09-21 00:04:32 +00:00
server Fix doc link import style to avoid unused_imports (#15337) 2024-09-21 00:04:32 +00:00
assets.rs Fix rust beta lints (#14537) 2024-07-31 01:27:26 +00:00
direct_access_ext.rs Add methods to directly load assets from World (#12023) 2024-02-27 00:28:26 +00:00
event.rs Added AssetLoadFailedEvent, UntypedAssetLoadFailedEvent (#11369) 2024-01-17 21:12:00 +00:00
folder.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
handle.rs move ShortName to bevy_reflect (#15340) 2024-09-21 20:52:46 +00:00
id.rs Allow phase items not associated with meshes to be binned. (#14029) 2024-06-27 16:13:03 +00:00
lib.rs Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
loader_builders.rs Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
loader.rs Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
meta.rs Optimize common usages of AssetReader (#14082) 2024-07-01 19:59:42 +00:00
path.rs Replace bevy_utils::CowArc with atomicow (#14977) 2024-08-30 00:43:07 +00:00
reflect.rs bevy_reflect: Add ReflectRef/ReflectMut/ReflectOwned convenience casting methods (#15235) 2024-09-23 16:50:46 +00:00
saver.rs Add module and supporting documentation to bevy_assets (#15056) 2024-09-17 22:07:37 +00:00
transformer.rs Add basic docs explaining what asset processing is and where to look (#15058) 2024-09-17 22:59:12 +00:00