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

bevy_reflect: Add ReflectRef/ReflectMut/ReflectOwned convenience casting methods #15235

Merged

Conversation

MrGVSV
Copy link
Member

@MrGVSV MrGVSV commented Sep 15, 2024

Objective

#13320 added convenience methods for casting a TypeInfo into its respective variant:

let info: &TypeInfo = <Vec<i32> as Typed>::type_info();

// We know `info` contains a `ListInfo`, so we can simply cast it:
let list_info: &ListInfo = info.as_list().unwrap();

This is especially helpful when you have already verified a type is a certain kind via ReflectRef, ReflectMut, ReflectOwned, or ReflectKind.

As mentioned in that PR, though, it would be useful to add similar convenience methods to those types as well.

Solution

Added convenience casting methods to ReflectRef, ReflectMut, and ReflectOwned.

With these methods, I was able to reduce our nesting in certain places throughout the crate.

Additionally, I took this opportunity to move these types (and ReflectKind) to their own module to help clean up the reflect module.

Testing

You can test locally by running:

cargo test --package bevy_reflect --all-features

Showcase

Convenience methods for casting ReflectRef, ReflectMut, and ReflectOwned into their respective variants has been added! This allows you to write cleaner code if you already know the kind of your reflected data:

// BEFORE
let ReflectRef::List(list) = list.reflect_ref() else {
    panic!("expected list");
};

// AFTER
let list = list.reflect_ref().as_list().unwrap();

@MrGVSV MrGVSV added C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Reflection Runtime information about types D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 15, 2024
@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/kind-conversions branch from 7303200 to ba60c4c Compare September 15, 2024 19:25
@pablo-lua pablo-lua added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 22, 2024
@alice-i-cecile
Copy link
Member

@MrGVSV if this is merge-conflict-free tomorrow for the merge train I'll get this in :)

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/kind-conversions branch from b834b97 to f6683d7 Compare September 23, 2024 03:57
@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/kind-conversions branch from f6683d7 to 66ae70f Compare September 23, 2024 04:27
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 23, 2024
Merged via the queue into bevyengine:main with commit 4d0961c Sep 23, 2024
26 checks passed
@MrGVSV MrGVSV deleted the mrgvsv/reflect/kind-conversions branch September 23, 2024 17:35
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-Code-Quality A section of code that is hard to understand or change 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-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants