Skip to content

Commit

Permalink
Small improvement of code quality of Assets::set* methods (#1649)
Browse files Browse the repository at this point in the history
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  https://github.com/bevyengine/bevy/blob/68606934e32ab45828c628e1cefd3873273f8708/crates/bevy_asset/src/asset_server.rs#L468
  • Loading branch information
notsimon committed Mar 14, 2021
1 parent 2e72755 commit aa81aaf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 2 additions & 10 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit aa81aaf

Please sign in to comment.