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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 as it is now a Result not an Option now.

/// 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 :)

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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