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

FilteredResource returns a Result instead of a simple Option #18265

Merged

Conversation

andristarr
Copy link
Contributor

@andristarr andristarr commented Mar 11, 2025

Objective

FilteredResource::get should return a Result instead of Option

Fixes #17480


Migration Guide

Users will need to handle the different return type on FilteredResource::get, FilteredResource::get_id, FilteredResource::get_mut as it is now a Result not an Option.

/// The resource does not exist in the world.
#[error("The resource does not exist in the world.")]
MissingResource,
/// No access to the resource with the given [`ComponentId`] in the world.
Copy link
Member

Choose a reason for hiding this comment

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

These docs / error message should be clearer. What sort of access are we talking about here?

Copy link
Contributor Author

@andristarr andristarr Mar 11, 2025

Choose a reason for hiding this comment

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

Hmmm, how about something like "Cannot get access to resource as it conflicts with an on going operation"?

Something along this line, I am not a native speaker so feel free to suggest something that is better!

Copy link
Member

Choose a reason for hiding this comment

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

Something like that is better, but I think the key thing to mention is that the conflicting access is with respect to the world :)

Copy link
Contributor

Choose a reason for hiding this comment

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

What sort of access are we talking about here?

The main way to use FilteredResources is with a SystemParamBuilder, so the answer is usually that you needed to request it in the FilteredResourcesParamBuilder.

(The other way to get one is to convert from &World or &mut World, but those will have access to all resources and shouldn't ever return this variant.)

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Reflection Runtime information about types M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Mar 11, 2025
Copy link
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-18265

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

Comment on lines 156 to 177
pub fn get<R: Resource>(&self) -> Result<Ref<'w, R>, ResourceFetchError> {
let component_id = self
.world
.components()
.resource_id::<R>()
.ok_or(ResourceFetchError::MissingResource)?;
if !self.access.has_resource_read(component_id) {
return None;
return Err(ResourceFetchError::NoResourceAccess(component_id));
}

// SAFETY: We have read access to this resource
unsafe { self.world.get_resource_with_ticks(component_id) }.map(|(value, ticks, caller)| {
Ref {
// SAFETY: `component_id` was obtained from the type ID of `R`.
value: unsafe { value.deref() },
// SAFETY: We have read access to the resource, so no mutable reference can exist.
ticks: unsafe { Ticks::from_tick_cells(ticks, self.last_run, self.this_run) },
// SAFETY: We have read access to the resource, so no mutable reference can exist.
changed_by: unsafe { caller.map(|caller| caller.deref()) },
}
let (value, ticks, caller) = unsafe { self.world.get_resource_with_ticks(component_id) }
.ok_or(ResourceFetchError::MissingResource)?;

Ok(Ref {
// SAFETY: `component_id` was obtained from the type ID of `R`.
value: unsafe { value.deref() },
// SAFETY: We have read access to the resource, so no mutable reference can exist.
ticks: unsafe { Ticks::from_tick_cells(ticks, self.last_run, self.this_run) },
// SAFETY: We have read access to the resource, so no mutable reference can exist.
changed_by: unsafe { caller.map(|caller| caller.deref()) },
})
Copy link
Contributor

Choose a reason for hiding this comment

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

very idiomatic, I like it.

Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

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

Oh, yay, FilteredResources is getting used!

Should we update FilteredResources::get_by_id and the various FilteredResourcesMut::get_mut methods, as well? The extra information seems just as valuable for those, and it would be odd for them to be different.

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-ECS Entities, components, systems, and events and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Mar 15, 2025
@andristarr
Copy link
Contributor Author

I think this is ready for a final review to be merged

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 17, 2025
@alice-i-cecile
Copy link
Member

Technically needed a second approval. But I can do that for you :)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 17, 2025
Merged via the queue into bevyengine:main with commit 2b21b6c Mar 17, 2025
34 checks passed
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 A-Reflection Runtime information about types C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return Result instead of Option from FilteredResources::get and similar
4 participants