Skip to content

Update FilteredEntityRef/Mut getters to return Result instead of Option #21403

@cBournhonesque

Description

@cBournhonesque

What problem does this solve or what need does it fill?

Currently FilteredEntityRef look like this:

  /// Gets access to the component of type `T` for the current entity.
  /// Returns `None` if the entity does not have a component of type `T`.
  #[inline]
  pub fn get<T: Component>(&self) -> Option<&'_ T>;

The docstring is incorrect, you get a None if the entity does not have a component of type T OR if the FilteredEntityRef does not have the access to read T.

The return value of Option makes it impossible to distinguish between the two.

I have multiple times been stuck debugging while my entity does not have a given component T only to realize an hour later that my FilteredEntityRef does not have the correct access!!

What solution would you like?

Change the methods to return a Result, where the Errors could be an enum with:

  • InsufficientAccess
  • ComponentNotFound

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-Code-QualityA section of code that is hard to understand or changeC-UsabilityA targeted quality-of-life change that makes Bevy easier to useD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!X-UncontroversialThis work is generally agreed upon

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions