-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Migrate Quat
reflection strategy from "value" to "struct"
#8954
Migrate Quat
reflection strategy from "value" to "struct"
#8954
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me: great PR description and investigation. Is this intentionally in draft mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
It would be nice if we added some tests like we do here:
bevy/crates/bevy_reflect/src/lib.rs
Lines 1851 to 1953 in bec299f
#[cfg(feature = "glam")] | |
mod glam { | |
use super::*; | |
#[test] | |
fn vec3_serialization() { | |
let v = vec3(12.0, 3.0, -6.9); | |
let mut registry = TypeRegistry::default(); | |
registry.register::<f32>(); | |
registry.register::<Vec3>(); | |
let ser = ReflectSerializer::new(&v, ®istry); | |
let config = PrettyConfig::default() | |
.new_line(String::from("\n")) | |
.indentor(String::from(" ")); | |
let output = to_string_pretty(&ser, config).unwrap(); | |
let expected = r#" | |
{ | |
"glam::f32::vec3::Vec3": ( | |
x: 12.0, | |
y: 3.0, | |
z: -6.9, | |
), | |
}"#; | |
assert_eq!(expected, format!("\n{output}")); | |
} | |
#[test] | |
fn vec3_deserialization() { | |
let data = r#" | |
{ | |
"glam::f32::vec3::Vec3": ( | |
x: 12.0, | |
y: 3.0, | |
z: -6.9, | |
), | |
}"#; | |
let mut registry = TypeRegistry::default(); | |
registry.add_registration(Vec3::get_type_registration()); | |
registry.add_registration(f32::get_type_registration()); | |
let de = UntypedReflectDeserializer::new(®istry); | |
let mut deserializer = | |
ron::de::Deserializer::from_str(data).expect("Failed to acquire deserializer"); | |
let dynamic_struct = de | |
.deserialize(&mut deserializer) | |
.expect("Failed to deserialize"); | |
let mut result = Vec3::default(); | |
result.apply(&*dynamic_struct); | |
assert_eq!(result, vec3(12.0, 3.0, -6.9)); | |
} | |
#[test] | |
fn vec3_field_access() { | |
let mut v = vec3(1.0, 2.0, 3.0); | |
assert_eq!(*v.get_field::<f32>("x").unwrap(), 1.0); | |
*v.get_field_mut::<f32>("y").unwrap() = 6.0; | |
assert_eq!(v.y, 6.0); | |
} | |
#[test] | |
fn vec3_path_access() { | |
let mut v = vec3(1.0, 2.0, 3.0); | |
assert_eq!( | |
*v.reflect_path("x").unwrap().downcast_ref::<f32>().unwrap(), | |
1.0 | |
); | |
*v.reflect_path_mut("y") | |
.unwrap() | |
.downcast_mut::<f32>() | |
.unwrap() = 6.0; | |
assert_eq!(v.y, 6.0); | |
} | |
#[test] | |
fn vec3_apply_dynamic() { | |
let mut v = vec3(3.0, 3.0, 3.0); | |
let mut d = DynamicStruct::default(); | |
d.insert("x", 4.0f32); | |
d.insert("y", 2.0f32); | |
d.insert("z", 1.0f32); | |
v.apply(&d); | |
assert_eq!(v, vec3(4.0, 2.0, 1.0)); | |
} | |
} |
But I don't think we need to block on that since all the other glam types don't have any tests.
@alice-i-cecile Wanted to find out more about the test situation before I bothered anyone too much.
@MrGVSV I've force-pushed some tests. They failed on Ubuntu since it selected SSE2 but I think they would have passed on MacOS if it were not cancelled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hmm, it appears that Bevy deps on See:
I guess this was always an issue and is only becoming apparent now that it's being tested. Not sure whether to:
|
This also broke WARN bevy_asset::asset_server: encountered an error while loading an asset: Expected identifier at scenes/load_scene_example.scn.ron:16:22 which is expected but wasn't picked up by the CI. I guess CI doesn't test examples? Should I be testing all examples locally before a PR like this? |
This was previously discussed in #5803. #7184 should offer a way to fix that
Can you try to see how it would work with a stable type path?
CI doesn't test all examples, and this is a warning so it wouldn't even break a test run...
Maybe not all, but examples relevant to reflection and scene can be interesting to run 👍 |
Seconding this: let's try this before merging :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking on the stable type path changes.
Technically we could upstream into I am personally working on top of soqb's PRs which seem A-OK; they were quick to resolve the only two issues I found so far. Let us back-burner this until then. In the meantime I've found several more instances this technique should be applied; namely |
@pyrotechnick, can you please rebase or otherwise resolve merge conflicts? This is no longer blocked. |
I've resolved the merge conflicts; can y'all verify that I did so correctly? |
Looks like it needs formatting following the manual merge conflict resolution. |
registry.add_registration(Quat::get_type_registration()); | ||
registry.add_registration(f32::get_type_registration()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Any reason to not use the register
method?
registry.add_registration(Quat::get_type_registration()); | |
registry.add_registration(f32::get_type_registration()); | |
registry.register::<Quat>(); | |
registry.register::<f32>(); |
Putting this up for adoption; there's just a bit more work needed to get over the finish line. |
I've adopted this PR for the final touches in #10068. |
Adopted from #8954, co-authored by @pyrotechnick # Objective The Bevy ecosystem currently reflects `Quat` via "value" rather than the more appropriate "struct" strategy. This behaviour is inconsistent to that of similar types, i.e. `Vec3`. Additionally, employing the "value" strategy causes instances of `Quat` to be serialised as a sequence `[x, y, z, w]` rather than structures of shape `{ x, y, z, w }`. The [comments surrounding the applicable code](https://github.com/bevyengine/bevy/blob/bec299fa6e727a59d973fc8ca8f468732d40cb14/crates/bevy_reflect/src/impls/glam.rs#L254) give context and historical reasons for this discrepancy: ``` // Quat fields are read-only (as of now), and reflection is currently missing // mechanisms for read-only fields. I doubt those mechanisms would be added, // so for now quaternions will remain as values. They are represented identically // to Vec4 and DVec4, so you may use those instead and convert between. ``` This limitation has [since been lifted by the upstream crate](bitshifter/glam-rs@3746251), glam. ## Solution Migrating the reflect strategy of Quat from "value" to "struct" via replacing `impl_reflect_value` with `impl_reflect_struct` resolves the issue. ## Changelog Migrated `Quat` reflection strategy to "struct" from "value" Migration Guide Changed Quat serialization/deserialization from sequences `[x, y, z, w]` to structures `{ x, y, z, w }`. --------- Co-authored-by: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
…ne#10068) Adopted from bevyengine#8954, co-authored by @pyrotechnick # Objective The Bevy ecosystem currently reflects `Quat` via "value" rather than the more appropriate "struct" strategy. This behaviour is inconsistent to that of similar types, i.e. `Vec3`. Additionally, employing the "value" strategy causes instances of `Quat` to be serialised as a sequence `[x, y, z, w]` rather than structures of shape `{ x, y, z, w }`. The [comments surrounding the applicable code](https://github.com/bevyengine/bevy/blob/bec299fa6e727a59d973fc8ca8f468732d40cb14/crates/bevy_reflect/src/impls/glam.rs#L254) give context and historical reasons for this discrepancy: ``` // Quat fields are read-only (as of now), and reflection is currently missing // mechanisms for read-only fields. I doubt those mechanisms would be added, // so for now quaternions will remain as values. They are represented identically // to Vec4 and DVec4, so you may use those instead and convert between. ``` This limitation has [since been lifted by the upstream crate](bitshifter/glam-rs@3746251), glam. ## Solution Migrating the reflect strategy of Quat from "value" to "struct" via replacing `impl_reflect_value` with `impl_reflect_struct` resolves the issue. ## Changelog Migrated `Quat` reflection strategy to "struct" from "value" Migration Guide Changed Quat serialization/deserialization from sequences `[x, y, z, w]` to structures `{ x, y, z, w }`. --------- Co-authored-by: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
…ne#10068) Adopted from bevyengine#8954, co-authored by @pyrotechnick # Objective The Bevy ecosystem currently reflects `Quat` via "value" rather than the more appropriate "struct" strategy. This behaviour is inconsistent to that of similar types, i.e. `Vec3`. Additionally, employing the "value" strategy causes instances of `Quat` to be serialised as a sequence `[x, y, z, w]` rather than structures of shape `{ x, y, z, w }`. The [comments surrounding the applicable code](https://github.com/bevyengine/bevy/blob/bec299fa6e727a59d973fc8ca8f468732d40cb14/crates/bevy_reflect/src/impls/glam.rs#L254) give context and historical reasons for this discrepancy: ``` // Quat fields are read-only (as of now), and reflection is currently missing // mechanisms for read-only fields. I doubt those mechanisms would be added, // so for now quaternions will remain as values. They are represented identically // to Vec4 and DVec4, so you may use those instead and convert between. ``` This limitation has [since been lifted by the upstream crate](bitshifter/glam-rs@3746251), glam. ## Solution Migrating the reflect strategy of Quat from "value" to "struct" via replacing `impl_reflect_value` with `impl_reflect_struct` resolves the issue. ## Changelog Migrated `Quat` reflection strategy to "struct" from "value" Migration Guide Changed Quat serialization/deserialization from sequences `[x, y, z, w]` to structures `{ x, y, z, w }`. --------- Co-authored-by: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
…ne#10068) Adopted from bevyengine#8954, co-authored by @pyrotechnick # Objective The Bevy ecosystem currently reflects `Quat` via "value" rather than the more appropriate "struct" strategy. This behaviour is inconsistent to that of similar types, i.e. `Vec3`. Additionally, employing the "value" strategy causes instances of `Quat` to be serialised as a sequence `[x, y, z, w]` rather than structures of shape `{ x, y, z, w }`. The [comments surrounding the applicable code](https://github.com/bevyengine/bevy/blob/bec299fa6e727a59d973fc8ca8f468732d40cb14/crates/bevy_reflect/src/impls/glam.rs#L254) give context and historical reasons for this discrepancy: ``` // Quat fields are read-only (as of now), and reflection is currently missing // mechanisms for read-only fields. I doubt those mechanisms would be added, // so for now quaternions will remain as values. They are represented identically // to Vec4 and DVec4, so you may use those instead and convert between. ``` This limitation has [since been lifted by the upstream crate](bitshifter/glam-rs@3746251), glam. ## Solution Migrating the reflect strategy of Quat from "value" to "struct" via replacing `impl_reflect_value` with `impl_reflect_struct` resolves the issue. ## Changelog Migrated `Quat` reflection strategy to "struct" from "value" Migration Guide Changed Quat serialization/deserialization from sequences `[x, y, z, w]` to structures `{ x, y, z, w }`. --------- Co-authored-by: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Objective
The Bevy ecosystem currently reflects
Quat
via "value" rather than the more appropriate "struct" strategy. This behaviour is inconsistent to that of similar types, i.e.Vec3
. Additionally, employing the "value" strategy causes instances ofQuat
to be serialised as a sequence[x, y, z, w]
rather than structures of shape{ x, y, z, w }
.The comments surrounding the applicable code give context and historical reasons for this discrepancy:
This limitation has since been lifted by the upstream crate,
glam
.Solution
Migrating the reflect strategy of Quat from "value" to "struct" via replacing
impl_reflect_value
withimpl_reflect_struct
resolves the issue.Changelog
Migrated
Quat
reflection strategy to "struct" from "value"Migration Guide
Changed
Quat
serialization/deserialization from sequences[x, y, z, w]
to structures{ x, y, z, w }
.