Skip to content

Conversation

VanjaRo
Copy link

@VanjaRo VanjaRo commented Oct 18, 2025

Objective

Fixes #21403

Solution

  • Add try_get_* APIs to filtered entity refs/muts to return Result, distinguishing missing components from insufficient access.
  • Introduce ComponentFetchError with InsufficientAccess and ComponentNotFound.
  • Clarify docs for existing get/get_ref that None can also mean no read access under filtering.

Testing

  • Added unit tests covering success paths and both error cases.
  • Run: cargo test -p bevy_ecs.

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 19, 2025
if !self.access.has_component_write(component_id) {
return None;
}
self.entity.get_mut_by_id(component_id).ok()
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this unsafe?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this is just a formatting change? the compiler allows calling get_mut_by_id without the unsafe block here because bevy_ecs doesn't use unsafe_op_in_unsafe_fn, but it's good style to annotate unsafe function calls even within unsafe functions, since not all of the safety bounds of the parent function may have to hold for the inner call.

pub fn try_get<T: Component>(&self) -> Result<&'w T, ComponentFetchError> {
let components = self.entity.world().components();
let id = components.get_valid_id(TypeId::of::<T>()).ok_or(
ComponentFetchError::ComponentNotFound(ComponentId::new(usize::MAX)),
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to differentiate between ComponentNotFound on the entity and ComponentNotRegistered

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, this would be a confusing error to get.

Copy link
Contributor

@janis-bhm janis-bhm left a comment

Choose a reason for hiding this comment

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

Looks like this PR includes a handful of unrelated formatting changes? I think this is a nice addition, but I agree that a something like a ComponentNotRegistered error makes!

/// Returns an error distinguishing between insufficient access and the component
/// not being present on the entity.
#[inline]
pub fn try_get_mut<T: Component<Mutability = Mutable>>(
Copy link
Contributor

Choose a reason for hiding this comment

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

not blocking, but: an unchecked version of this method as well as try_get_mut_by_id sounds useful to me as well.

pub fn try_get<T: Component>(&self) -> Result<&'w T, ComponentFetchError> {
let components = self.entity.world().components();
let id = components.get_valid_id(TypeId::of::<T>()).ok_or(
ComponentFetchError::ComponentNotFound(ComponentId::new(usize::MAX)),
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, this would be a confusing error to get.

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-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 S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update FilteredEntityRef/Mut getters to return Result instead of Option

4 participants