Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor process_handle_drop_internal() in bevy_asset #10920

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions crates/bevy_asset/src/server/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,44 +562,49 @@ impl AssetInfos {
watching_for_changes: bool,
id: UntypedAssetId,
) -> bool {
match infos.entry(id) {
Entry::Occupied(mut entry) => {
if entry.get_mut().handle_drops_to_skip > 0 {
entry.get_mut().handle_drops_to_skip -= 1;
false
} else {
let info = entry.remove();
if let Some(path) = info.path {
if watching_for_changes {
for loader_dependency in info.loader_dependencies.keys() {
if let Some(dependants) =
loader_dependants.get_mut(loader_dependency)
{
dependants.remove(&path);
}
}
if let Some(label) = path.label() {
let mut without_label = path.to_owned();
without_label.remove_label();
if let Entry::Occupied(mut entry) =
living_labeled_assets.entry(without_label)
{
entry.get_mut().remove(label);
if entry.get().is_empty() {
entry.remove();
}
};
}
}
path_to_id.remove(&path);
}
true
}
}
let Entry::Occupied(mut entry) = infos.entry(id) else {
// Either the asset was already dropped, it doesn't exist, or it isn't managed by the asset server
// None of these cases should result in a removal from the Assets collection
Entry::Vacant(_) => false,
return false;
};

if entry.get_mut().handle_drops_to_skip > 0 {
entry.get_mut().handle_drops_to_skip -= 1;
return false;
}

let info = entry.remove();
let Some(path) = info.path else {
return true;
};

if !watching_for_changes {
path_to_id.remove(&path);
return true;
}

for loader_dependency in info.loader_dependencies.keys() {
if let Some(dependants) = loader_dependants.get_mut(loader_dependency) {
dependants.remove(&path);
}
}

let Some(label) = path.label() else {
path_to_id.remove(&path);
return true;
};

let mut without_label = path.to_owned();
without_label.remove_label();

if let Entry::Occupied(mut entry) = living_labeled_assets.entry(without_label) {
entry.get_mut().remove(label);
if entry.get().is_empty() {
entry.remove();
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This passage is too difficult to understand and duplicates the path_to_id.remove(&path) line, making it more obscure. It is more appropriate to extract it instead of inverting it. It is essentially a sort of subroutine anyway.

Basically you should revert the old if watching_for_changes block, and extract all the code inside it to a new function. Then you can invert the body of the new function as you usually do. If you use rust-analyzer there is a specific action to extract selected code into a function.

Once that's done, I think it's probably OK to go.

path_to_id.remove(&path);
true
}

/// Consumes all current handle drop events. This will update information in [`AssetInfos`], but it
Expand Down