Skip to content

Commit 6af5b69

Browse files
committed
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
1 parent 4fd092f commit 6af5b69

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

crates/bevy_reflect/src/path.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum ReflectPathError<'a> {
1818
index: usize,
1919
tuple_struct_index: usize,
2020
},
21+
#[error("the current tuple doesn't have a field with the index {tuple_index}")]
22+
InvalidTupleIndex { index: usize, tuple_index: usize },
2123
#[error("the current struct variant doesn't have a field with the name `{field}`")]
2224
InvalidStructVariantField { index: usize, field: &'a str },
2325
#[error("the current tuple variant doesn't have a field with the index {tuple_variant_index}")]
@@ -515,6 +517,12 @@ impl<'a> AccessRef<'a> {
515517
},
516518
)
517519
}
520+
(Self::TupleIndex(tuple_index), ReflectRef::Tuple(reflect_tuple)) => reflect_tuple
521+
.field(*tuple_index)
522+
.ok_or(ReflectPathError::InvalidTupleIndex {
523+
index: current_index,
524+
tuple_index: *tuple_index,
525+
}),
518526
(Self::ListIndex(list_index), ReflectRef::List(reflect_list)) => reflect_list
519527
.get(*list_index)
520528
.ok_or(ReflectPathError::InvalidListIndex {
@@ -603,6 +611,12 @@ impl<'a> AccessRef<'a> {
603611
},
604612
)
605613
}
614+
(Self::TupleIndex(tuple_index), ReflectMut::Tuple(reflect_tuple)) => reflect_tuple
615+
.field_mut(*tuple_index)
616+
.ok_or(ReflectPathError::InvalidTupleIndex {
617+
index: current_index,
618+
tuple_index: *tuple_index,
619+
}),
606620
(Self::ListIndex(list_index), ReflectMut::List(reflect_list)) => reflect_list
607621
.get_mut(*list_index)
608622
.ok_or(ReflectPathError::InvalidListIndex {
@@ -815,6 +829,7 @@ mod tests {
815829
tuple_variant: F,
816830
struct_variant: F,
817831
array: [i32; 3],
832+
tuple: (bool, f32),
818833
}
819834

820835
#[derive(Reflect)]
@@ -909,6 +924,7 @@ mod tests {
909924
tuple_variant: F::Tuple(123, 321),
910925
struct_variant: F::Struct { value: 'm' },
911926
array: [86, 75, 309],
927+
tuple: (true, 1.23),
912928
};
913929

914930
let b = ParsedPath::parse("w").unwrap();
@@ -923,6 +939,7 @@ mod tests {
923939
let k = ParsedPath::parse("struct_variant.value").unwrap();
924940
let l = ParsedPath::parse("struct_variant#0").unwrap();
925941
let m = ParsedPath::parse("array[2]").unwrap();
942+
let n = ParsedPath::parse("tuple.1").unwrap();
926943

927944
for _ in 0..30 {
928945
assert_eq!(*b.element::<usize>(&a).unwrap(), 1);
@@ -937,6 +954,7 @@ mod tests {
937954
assert_eq!(*k.element::<char>(&a).unwrap(), 'm');
938955
assert_eq!(*l.element::<char>(&a).unwrap(), 'm');
939956
assert_eq!(*m.element::<i32>(&a).unwrap(), 309);
957+
assert_eq!(*n.element::<f32>(&a).unwrap(), 1.23);
940958
}
941959
}
942960

@@ -996,6 +1014,7 @@ mod tests {
9961014
tuple_variant: F::Tuple(123, 321),
9971015
struct_variant: F::Struct { value: 'm' },
9981016
array: [86, 75, 309],
1017+
tuple: (true, 1.23),
9991018
};
10001019

10011020
assert_eq!(*a.path::<usize>("w").unwrap(), 1);
@@ -1013,6 +1032,10 @@ mod tests {
10131032

10141033
assert_eq!(*a.path::<i32>("array[2]").unwrap(), 309);
10151034

1035+
assert_eq!(*a.path::<f32>("tuple.1").unwrap(), 1.23);
1036+
*a.path_mut::<f32>("tuple.1").unwrap() = 3.21;
1037+
assert_eq!(*a.path::<f32>("tuple.1").unwrap(), 3.21);
1038+
10161039
*a.path_mut::<f32>("y[1].baz").unwrap() = 3.0;
10171040
assert_eq!(a.y[1].baz, 3.0);
10181041

0 commit comments

Comments
 (0)