bevy/crates/bevy_asset/src
Tristan Guichaoua a30da0001e
impl From<&AssetPath> for HandleId (#9132)
# Objective

In
[`AssetLoader::load()`](https://docs.rs/bevy/0.11.0/bevy/asset/trait.AssetLoader.html#tymethod.load),
I have an
[`AssetPath`](https://docs.rs/bevy/0.11.0/bevy/asset/struct.AssetPath.html)
to a dependency asset.
I get a handle to this dependency asset using
[`LoadContext::get_handle()`](https://docs.rs/bevy/0.11.0/bevy/asset/struct.LoadContext.html#method.get_handle)
passing the `AssetPath`. But I also need to pass this `AssetPath` to
[`LoadedAsset::with_dependency()`](https://docs.rs/bevy/0.11.0/bevy/asset/struct.LoadedAsset.html#method.with_dependency)
later.


The current solution for this problem is either use `clone()`, but
`AssetPath` may contains owned data.
```rust
let dependency_path: AssetPath = _;
let dependency = load_context.get_handle(dependency_path.clone());
// ...
load_context.set_default_asset(LoadedAsset::new(my_asset).with_dependency(dependency_path));
```

Or to use `AssetPathId::from(&path)` which is a bit verbose.
```rust
let dependency_path: AssetPath = _;
let dependency = load_context.get_handle(AssetPathId::from(&dependency_path));
// ...
load_context.set_default_asset(LoadedAsset::new(my_asset).with_dependency(dependency_path));
```

Ideal solution (introduced by this PR) is to pass a reference to
`get_handle()`.
```rust
let dependency_path: AssetPath = _;
let dependency = load_context.get_handle(&dependency_path);
// ...
load_context.set_default_asset(LoadedAsset::new(my_asset).with_dependency(dependency_path));
```

## Solution

Implement `From<&AssetPath>` for `HandleId`

---

## Changelog

- Added: `HandleId` can be build from a reference to `AssetPath`.
2023-07-15 21:32:17 +00:00
..
diagnostic Allow systems using Diagnostics to run in parallel (#8677) 2023-06-05 20:51:22 +00:00
io Bump hashbrown to 0.14 (#8904) 2023-06-21 13:04:44 +00:00
asset_server.rs Allow tuples and single plugins in add_plugins, deprecate add_plugin (#8097) 2023-06-21 20:51:03 +00:00
assets.rs fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
debug_asset_server.rs improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
filesystem_watcher.rs Delay asset hot reloading (#8503) 2023-05-16 01:26:11 +00:00
handle.rs impl From<&AssetPath> for HandleId (#9132) 2023-07-15 21:32:17 +00:00
info.rs docs: Full documentation for bevy_asset (#3536) 2022-07-12 15:44:09 +00:00
lib.rs Delay asset hot reloading (#8503) 2023-05-16 01:26:11 +00:00
loader.rs doc(asset): fix asset trait example (#9105) 2023-07-11 21:24:43 +00:00
path.rs bevy_reflect: FromReflect Ergonomics Implementation (#6056) 2023-06-29 01:31:34 +00:00
reflect.rs bevy_reflect: FromReflect Ergonomics Implementation (#6056) 2023-06-29 01:31:34 +00:00