bevyengine / bevyengine/bevy

Update FilteredEntityRef/Mut getters to return Result instead of Option

Open
#21,403 2 comments 1 reaction 0 assignees View on GitHub
A-ECS C-Code-Quality C-Usability D-Modest S-Ready-For-Implementation X-Uncontroversial
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## 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(&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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.