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

[Merged by Bors] - bevy_reflect: Fix apply method for Option<T> #5780

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 69 additions & 18 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,7 @@ impl<T: FromReflect> Reflect for Option<T> {
if self.variant_name() == value.variant_name() {
// Same variant -> just update fields
for (index, field) in value.iter_fields().enumerate() {
let name = value.name_at(index).unwrap();
if let Some(v) = self.field_mut(name) {
if let Some(v) = self.field_at_mut(index) {
v.apply(field.value());
}
}
Expand All @@ -707,14 +706,23 @@ impl<T: FromReflect> Reflect for Option<T> {
"Some" => {
let field = value
.field_at(0)
.expect("Field at index 0 should exist")
.clone_value();
let field = field.take::<T>().unwrap_or_else(|_| {
panic!(
"Field at index 0 should be of type {}",
std::any::type_name::<T>()
)
});
.unwrap_or_else(|| {
panic!(
"Field in `Some` variant of {} should exist",
std::any::type_name::<Option<T>>()
)
})
.clone_value()
.take::<T>()
.unwrap_or_else(|value| {
Copy link
Member

Choose a reason for hiding this comment

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

Seems reasonable for the current impl. Stuff like this makes me want to revisit mixing "dynamic type" reflection and "actual type" reflection. But thats a conversation for another day :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds like a good time to revisit bevyengine/rfcs#56 😉

T::from_reflect(&*value).unwrap_or_else(|| {
panic!(
"Field in `Some` variant of {} should be of type {}",
std::any::type_name::<Option<T>>(),
std::any::type_name::<T>()
)
})
});
*self = Some(field);
}
"None" => {
Expand Down Expand Up @@ -761,14 +769,23 @@ impl<T: FromReflect> FromReflect for Option<T> {
"Some" => {
let field = dyn_enum
.field_at(0)
.expect("Field at index 0 should exist")
.clone_value();
let field = T::from_reflect(field.as_ref()).unwrap_or_else(|| {
panic!(
"Field at index 0 should be of type {}",
std::any::type_name::<T>()
)
});
.unwrap_or_else(|| {
panic!(
"Field in `Some` variant of {} should exist",
std::any::type_name::<Option<T>>()
)
})
.clone_value()
.take::<T>()
.unwrap_or_else(|value| {
T::from_reflect(&*value).unwrap_or_else(|| {
panic!(
"Field in `Some` variant of {} should be of type {}",
std::any::type_name::<Option<T>>(),
std::any::type_name::<T>()
)
})
});
Some(Some(field))
}
"None" => Some(None),
Expand Down Expand Up @@ -953,6 +970,40 @@ mod tests {
assert_eq!(expected, output);
}

#[test]
fn option_should_apply() {
#[derive(Reflect, FromReflect, PartialEq, Debug)]
struct Foo(usize);

// === None on None === //
let patch = None::<Foo>;
let mut value = None;
Reflect::apply(&mut value, &patch);

assert_eq!(patch, value, "None apply onto None");

// === Some on None === //
let patch = Some(Foo(123));
let mut value = None;
Reflect::apply(&mut value, &patch);

assert_eq!(patch, value, "Some apply onto None");

// === None on Some === //
let patch = None::<Foo>;
let mut value = Some(Foo(321));
Reflect::apply(&mut value, &patch);

assert_eq!(patch, value, "None apply onto Some");

// === Some on Some === //
let patch = Some(Foo(123));
let mut value = Some(Foo(321));
Reflect::apply(&mut value, &patch);

assert_eq!(patch, value, "Some apply onto Some");
}

#[test]
fn option_should_impl_typed() {
type MyOption = Option<i32>;
Expand Down