Small improvement of code quality of Assets::set* methods (#1649)

As mentioned in #1609.

I'm not sure if this is desirable, but on top of factoring the `set` and `set_untracked` methods I added a warning when the return value of `set` isn't used to mitigate similar issues.

I silenced it for the only occurence where it's currently done  68606934e3/crates/bevy_asset/src/asset_server.rs (L468)
This commit is contained in:
Simon Guillot 2021-03-14 00:19:44 +00:00
parent 2e72755b8a
commit aa81aaf3fa
2 changed files with 3 additions and 11 deletions

View File

@ -465,7 +465,7 @@ impl AssetServer {
}
}
assets.set(result.id, result.asset);
let _ = assets.set(result.id, result.asset);
}
Ok(AssetLifecycleEvent::Free(handle_id)) => {
if let HandleId::AssetPathId(id) = handle_id {

View File

@ -72,18 +72,10 @@ impl<T: Asset> Assets<T> {
self.get_handle(id)
}
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn set<H: Into<HandleId>>(&mut self, handle: H, asset: T) -> Handle<T> {
let id: HandleId = handle.into();
if self.assets.insert(id, asset).is_some() {
self.events.send(AssetEvent::Modified {
handle: Handle::weak(id),
});
} else {
self.events.send(AssetEvent::Created {
handle: Handle::weak(id),
});
}
self.set_untracked(id, asset);
self.get_handle(id)
}