From 6af5b691e468a1ec4eb39f56f8b3749a89a75078 Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Mon, 6 Feb 2023 17:10:38 +0000 Subject: [PATCH] bevy_reflect: Support tuple reflection paths (#7324) # Objective Currently the `GetPath` documentation suggests it can be used with `Tuple` types (reflected tuples). However, this is not currently the case. ## Solution Add reflection path support for `Tuple` types. --- ## Changelog - Add reflection path support for `Tuple` types --- crates/bevy_reflect/src/path.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/bevy_reflect/src/path.rs b/crates/bevy_reflect/src/path.rs index e43a45fea31eeb..cb01b6ad7b404a 100644 --- a/crates/bevy_reflect/src/path.rs +++ b/crates/bevy_reflect/src/path.rs @@ -18,6 +18,8 @@ pub enum ReflectPathError<'a> { index: usize, tuple_struct_index: usize, }, + #[error("the current tuple doesn't have a field with the index {tuple_index}")] + InvalidTupleIndex { index: usize, tuple_index: usize }, #[error("the current struct variant doesn't have a field with the name `{field}`")] InvalidStructVariantField { index: usize, field: &'a str }, #[error("the current tuple variant doesn't have a field with the index {tuple_variant_index}")] @@ -515,6 +517,12 @@ impl<'a> AccessRef<'a> { }, ) } + (Self::TupleIndex(tuple_index), ReflectRef::Tuple(reflect_tuple)) => reflect_tuple + .field(*tuple_index) + .ok_or(ReflectPathError::InvalidTupleIndex { + index: current_index, + tuple_index: *tuple_index, + }), (Self::ListIndex(list_index), ReflectRef::List(reflect_list)) => reflect_list .get(*list_index) .ok_or(ReflectPathError::InvalidListIndex { @@ -603,6 +611,12 @@ impl<'a> AccessRef<'a> { }, ) } + (Self::TupleIndex(tuple_index), ReflectMut::Tuple(reflect_tuple)) => reflect_tuple + .field_mut(*tuple_index) + .ok_or(ReflectPathError::InvalidTupleIndex { + index: current_index, + tuple_index: *tuple_index, + }), (Self::ListIndex(list_index), ReflectMut::List(reflect_list)) => reflect_list .get_mut(*list_index) .ok_or(ReflectPathError::InvalidListIndex { @@ -815,6 +829,7 @@ mod tests { tuple_variant: F, struct_variant: F, array: [i32; 3], + tuple: (bool, f32), } #[derive(Reflect)] @@ -909,6 +924,7 @@ mod tests { tuple_variant: F::Tuple(123, 321), struct_variant: F::Struct { value: 'm' }, array: [86, 75, 309], + tuple: (true, 1.23), }; let b = ParsedPath::parse("w").unwrap(); @@ -923,6 +939,7 @@ mod tests { let k = ParsedPath::parse("struct_variant.value").unwrap(); let l = ParsedPath::parse("struct_variant#0").unwrap(); let m = ParsedPath::parse("array[2]").unwrap(); + let n = ParsedPath::parse("tuple.1").unwrap(); for _ in 0..30 { assert_eq!(*b.element::(&a).unwrap(), 1); @@ -937,6 +954,7 @@ mod tests { assert_eq!(*k.element::(&a).unwrap(), 'm'); assert_eq!(*l.element::(&a).unwrap(), 'm'); assert_eq!(*m.element::(&a).unwrap(), 309); + assert_eq!(*n.element::(&a).unwrap(), 1.23); } } @@ -996,6 +1014,7 @@ mod tests { tuple_variant: F::Tuple(123, 321), struct_variant: F::Struct { value: 'm' }, array: [86, 75, 309], + tuple: (true, 1.23), }; assert_eq!(*a.path::("w").unwrap(), 1); @@ -1013,6 +1032,10 @@ mod tests { assert_eq!(*a.path::("array[2]").unwrap(), 309); + assert_eq!(*a.path::("tuple.1").unwrap(), 1.23); + *a.path_mut::("tuple.1").unwrap() = 3.21; + assert_eq!(*a.path::("tuple.1").unwrap(), 3.21); + *a.path_mut::("y[1].baz").unwrap() = 3.0; assert_eq!(a.y[1].baz, 3.0);