Skip to content

Commit

Permalink
Add support for tuple paths
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Jan 22, 2023
1 parent cb4e8c8 commit 3efa01f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/bevy_reflect/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 list doesn't have a value at the index {list_index}")]
InvalidListIndex { index: usize, list_index: usize },
#[error("encountered an unexpected token `{token}`")]
Expand Down Expand Up @@ -282,6 +284,15 @@ fn read_field<'r, 'p>(
},
)?)
}
ReflectRef::Tuple(reflect_tuple) => {
let tuple_index = field.parse::<usize>()?;
Ok(reflect_tuple
.field(tuple_index)
.ok_or(ReflectPathError::InvalidTupleIndex {
index: current_index,
tuple_index,
})?)
}
ReflectRef::Enum(reflect_enum) => match reflect_enum.variant_type() {
VariantType::Struct => {
Ok(reflect_enum
Expand Down Expand Up @@ -334,6 +345,15 @@ fn read_field_mut<'r, 'p>(
},
)?)
}
ReflectMut::Tuple(reflect_tuple) => {
let tuple_index = field.parse::<usize>()?;
Ok(reflect_tuple
.field_mut(tuple_index)
.ok_or(ReflectPathError::InvalidTupleIndex {
index: current_index,
tuple_index,
})?)
}
ReflectMut::Enum(reflect_enum) => match reflect_enum.variant_type() {
VariantType::Struct => {
Ok(reflect_enum
Expand Down Expand Up @@ -467,6 +487,7 @@ mod tests {
unit_variant: F,
tuple_variant: F,
struct_variant: F,
tuple: (bool, f32),
}

#[derive(Reflect)]
Expand Down Expand Up @@ -504,6 +525,7 @@ mod tests {
unit_variant: F::Unit,
tuple_variant: F::Tuple(123, 321),
struct_variant: F::Struct { value: 'm' },
tuple: (true, 1.23),
};

assert_eq!(*a.get_path::<usize>("w").unwrap(), 1);
Expand All @@ -516,6 +538,10 @@ mod tests {
assert_eq!(*a.get_path::<u32>("tuple_variant.1").unwrap(), 321);
assert_eq!(*a.get_path::<char>("struct_variant.value").unwrap(), 'm');

assert_eq!(*a.get_path::<f32>("tuple.1").unwrap(), 1.23);
*a.get_path_mut::<f32>("tuple.1").unwrap() = 3.21;
assert_eq!(*a.get_path::<f32>("tuple.1").unwrap(), 3.21);

*a.get_path_mut::<f32>("y[1].baz").unwrap() = 3.0;
assert_eq!(a.y[1].baz, 3.0);

Expand Down

0 comments on commit 3efa01f

Please sign in to comment.