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

fix world.is_resource_added() panicing #2829

Closed
wants to merge 2 commits into from

Conversation

RustyStriker
Copy link

Objective

Fixes #2828

Solution

replaced unwrap with match(and added a test to make sure everything works without crashing)

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Sep 15, 2021
@MinerSebas
Copy link
Contributor

Could you also remove the unwraps inside is_resource_changed.

@RustyStriker
Copy link
Author

Could you also remove the unwraps inside is_resource_changed.

if a resource doesn't exist, would you say it is never changed? or indeed should we crash the program because the user might try to access an unregistered resource?

@MinerSebas
Copy link
Contributor

I can see that argument, but in that case is_resource_changed should return Option<bool> and not Panic.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

I'd implement the function differently, but in a semantically equivalent way. The test looks good though

I'm not sure about is_resource_changed. I'd prefer returning false when the resource doesn't exist to panicing, but I'm less sure about changing the return type.

Comment on lines +630 to 640
let component_id = match self.components.get_resource_id(TypeId::of::<T>()) {
Some(id) => id,
None => return false,
};
let column = match self.get_populated_resource_column(component_id) {
Some(col) => col,
None => return false,
};
// SAFE: resources table always have row 0
let ticks = unsafe { column.get_ticks_unchecked(0) };
ticks.is_added(self.last_change_tick(), self.read_change_tick())
Copy link
Member

Choose a reason for hiding this comment

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

This could be combined with the line using Option::and_then, avoiding one match.

I would also change it to be an if let Some(...) with an else {false} at the end
I.e. my final version would be:

Suggested change
let component_id = match self.components.get_resource_id(TypeId::of::<T>()) {
Some(id) => id,
None => return false,
};
let column = match self.get_populated_resource_column(component_id) {
Some(col) => col,
None => return false,
};
// SAFE: resources table always have row 0
let ticks = unsafe { column.get_ticks_unchecked(0) };
ticks.is_added(self.last_change_tick(), self.read_change_tick())
if let Some(column) = self.components.get_resource_id(TypeId::of::<T>())
.and_then(|component_id| self.get_populated_resource_column(component_id)) {
// SAFE: resources table always have row 0
let ticks = unsafe { column.get_ticks_unchecked(0) };
ticks.is_added(self.last_change_tick(), self.read_change_tick())
} else {
false
}

Copy link
Author

Choose a reason for hiding this comment

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

it seems a bit less readable imo, the if condition stretches over 2 lines(or occupies a really long line) and generally seems a bit crowded, the match cases(while seems a bit long) gives you a clear view of what is going on at a glance.
(and generally speaking i would get that if condition to be a variable and a short if let statement instead)

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior and removed S-Needs-Triage This issue needs to be labelled labels Sep 22, 2021
@RustyStriker RustyStriker deleted the first_cont branch September 25, 2021 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

app.world().is_resource_added::<T>() panics instead of returning false
4 participants