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

FilteredEntityRef conversions #11838

Merged
merged 8 commits into from
Feb 14, 2024

Conversation

coreh
Copy link
Contributor

@coreh coreh commented Feb 13, 2024

Objective

Right now, it's a bit cumbersome to write code that simultaneously deals with both FilteredEntityRef/EntityRef or with FilteredEntityMut/EntityMut. This PR aims to make it easier by allowing conversions (both infallible and fallible) between them.

Solution

  • Added infallible conversions from unfiltered into filtered entity refs
  • Added fallible conversions from filtered into unfiltered entity refs

Changelog

  • Added the following infallible conversions: (From)
    • EntityRef<'a>FilteredEntityRef<'a>
    • &'a EntityRefFilteredEntityRef<'a>
    • EntityMut<'a>FilteredEntityRef<'a>
    • &'a EntityMutFilteredEntityRef<'a>
    • EntityWorldMut<'a>FilteredEntityRef<'a>
    • &'a EntityWorldMutFilteredEntityRef<'a>
    • EntityMut<'a>FilteredEntityMut<'a>
    • &'a mut EntityMutFilteredEntityMut<'a>
    • EntityWorldMut<'a>FilteredEntityMut<'a>
    • &'a mut EntityWorldMutFilteredEntityMut<'a>
  • Added the following fallible conversions: (TryFrom)
    • FilteredEntityRef<'a>EntityRef<'a>
    • &'a FilteredEntityRefEntityRef<'a>
    • FilteredEntityMut<'a>EntityRef<'a>
    • &'a FilteredEntityMutEntityRef<'a>
    • FilteredEntityMut<'a>EntityMut<'a>
    • &'a mut FilteredEntityMutEntityMut<'a>

@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 labels Feb 13, 2024
@@ -189,6 +189,58 @@ impl<'a> From<&'a EntityMut<'_>> for EntityRef<'a> {
}
}

impl<'a> TryFrom<FilteredEntityRef<'a>> for EntityRef<'a> {
type Error = ();
Copy link
Contributor

Choose a reason for hiding this comment

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

Does worth it adding an error for this conversion, so users may know why it failed, something like:

pub enum EntityRefAccessError {
    NoReadAll,
    NoWriteAll,
}

impl fmt::Display for EntityRefAccessError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoReadAll=> write!(f, "The given FilteredEntityRef has no read access on all entities."),
            Self::NoWriteAll=> write!(f, "The given FilteredEntityRef has no write access on all entities."),
        }
    }
}

impl std::error::Error for IdentifierError {}

Copy link
Contributor Author

@coreh coreh Feb 14, 2024

Choose a reason for hiding this comment

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

Added the error enum, went with slightly different names to match the naming conventions for existing TryFrom errors, and also for variants of this existing error type

type Error = ();

fn try_from(value: FilteredEntityMut<'a>) -> Result<Self, Self::Error> {
if value.access.has_read_all() && value.access.has_write_all() {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: AFAIK you don't have to check for read all access, since it's impossible to have write_all access without read_all

Suggested change
if value.access.has_read_all() && value.access.has_write_all() {
if value.access.has_write_all() {

Copy link
Member

Choose a reason for hiding this comment

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

I'd like to keep this in as a failsafe.

Copy link
Contributor Author

@coreh coreh Feb 14, 2024

Choose a reason for hiding this comment

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

Is this guarantee something that we want to match the semantics of & and &mut in Rust, to keep it straightforward, or is it possible that we might want to have write-only access in the future?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's possible that we have write only access to certain data in the future. And more importantly, users can currently do this on their own.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 14, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 14, 2024
Merged via the queue into bevyengine:main with commit 0354ce4 Feb 14, 2024
24 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 C-Usability A targeted quality-of-life change that makes Bevy easier to use 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.

3 participants