Skip to content

Commit 08ea1d1

Browse files
nicopapalice-i-cecileMrGVSV
authored
Refactor path module of bevy_reflect (#8887)
# Objective - The `path` module was getting fairly large. - The code in `AccessRef::read_element` and mut equivalent was very complex and difficult to understand. - The `ReflectPathError` had a lot of variants, and was difficult to read. ## Solution - Split the file in two, `access` now has its own module - Rewrite the `read_element` methods, they were ~200 lines long, they are now ~70 lines long — I didn't change any of the logic. It's really just the same code, but error handling is separated. - Split the `ReflectPathError` error - Merge `AccessRef` and `Access` - A few other changes that aim to reduce code complexity ### Fully detailed change list - `Display` impl of `ParsedPath` now includes prefix dots — this allows simplifying its implementation, and IMO `.path.to.field` is a better way to express a "path" than `path.to.field` which could suggest we are reading the `to` field of a variable named `path` - Add a test to check that dot prefixes and other are correctly parsed — Until now, no test contained a prefixing dot - Merge `Access` and `AccessRef`, using a `Cow<'a, str>`. Generated code seems to agree with this decision (`ParsedPath::parse` sheds 5% of instructions) - Remove `Access::as_ref` since there is no such thing as an `AccessRef` anymore. - Rename `AccessRef::to_owned` into `AccessRef::into_owned()` since it takes ownership of `self` now. - Add a `parse_static` that doesn't allocate new strings for named fields! - Add a section about path reflection in the `bevy_reflect` crate root doc — I saw a few people that weren't aware of path reflection, so I thought it was pertinent to add it to the root doc - a lot of nits - rename `index` to `offset` when it refers to offset in the path string — There is no more confusion with the other kind of indices in this context, also it's a common naming convention for parsing. - Make a dedicated enum for parsing errors - rename the `read_element` methods to `element` — shorter, but also `read_element_mut` was a fairly poor name - The error values now not only contain the expected type but also the actual type. - Remove lifetimes that could be inferred from the `GetPath` trait methods. --- ## Change log - Added the `ParsedPath::parse_static` method, avoids allocating when parsing `&'static str`. ## Migration Guide If you were matching on the `Err(ReflectPathError)` value returned by `GetPath` and `ParsedPath` methods, now only the parse-related errors and the offset are publicly accessible. You can always use the `fmt::Display` to get a clear error message, but if you need programmatic access to the error types, please open an issue. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Gino Valente <[email protected]>
1 parent 04ca832 commit 08ea1d1

File tree

3 files changed

+423
-404
lines changed

3 files changed

+423
-404
lines changed

crates/bevy_reflect/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,29 @@
229229
//!
230230
//! All primitives and simple types implement `FromReflect` by relying on their [`Default`] implementation.
231231
//!
232+
//! # Path navigation
233+
//!
234+
//! The [`GetPath`] trait allows accessing arbitrary nested fields of a [`Reflect`] type.
235+
//!
236+
//! Using `GetPath`, it is possible to use a path string to access a specific field
237+
//! of a reflected type.
238+
//!
239+
//! ```
240+
//! # use bevy_reflect::{Reflect, GetPath};
241+
//! #[derive(Reflect)]
242+
//! struct MyStruct {
243+
//! value: Vec<Option<u32>>
244+
//! }
245+
//!
246+
//! let my_struct = MyStruct {
247+
//! value: vec![None, None, Some(123)],
248+
//! };
249+
//! assert_eq!(
250+
//! my_struct.path::<u32>(".value[2].0").unwrap(),
251+
//! &123,
252+
//! );
253+
//! ```
254+
//!
232255
//! # Type Registration
233256
//!
234257
//! This crate also comes with a [`TypeRegistry`] that can be used to store and retrieve additional type metadata at runtime,
+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
use std::{borrow::Cow, fmt};
2+
3+
use super::{AccessError, ReflectPathError};
4+
use crate::{Reflect, ReflectMut, ReflectRef, VariantType};
5+
use thiserror::Error;
6+
7+
type InnerResult<T> = Result<Option<T>, Error<'static>>;
8+
9+
#[derive(Debug, PartialEq, Eq, Error)]
10+
pub(super) enum Error<'a> {
11+
#[error(
12+
"the current {ty} doesn't have the {} {}",
13+
access.kind(),
14+
access.display_value(),
15+
)]
16+
Access { ty: TypeShape, access: Access<'a> },
17+
18+
#[error("invalid type shape: expected {expected} but found a reflect {actual}")]
19+
Type {
20+
expected: TypeShape,
21+
actual: TypeShape,
22+
},
23+
24+
#[error("invalid enum access: expected {expected} variant but found {actual} variant")]
25+
Enum {
26+
expected: TypeShape,
27+
actual: TypeShape,
28+
},
29+
}
30+
31+
impl<'a> Error<'a> {
32+
fn with_offset(self, offset: usize) -> ReflectPathError<'a> {
33+
let error = AccessError(self);
34+
ReflectPathError::InvalidAccess { offset, error }
35+
}
36+
37+
fn access(ty: TypeShape, access: Access<'a>) -> Self {
38+
Self::Access { ty, access }
39+
}
40+
}
41+
impl Error<'static> {
42+
fn bad_enum_variant(expected: TypeShape, actual: impl Into<TypeShape>) -> Self {
43+
let actual = actual.into();
44+
Error::Enum { expected, actual }
45+
}
46+
fn bad_type(expected: TypeShape, actual: impl Into<TypeShape>) -> Self {
47+
let actual = actual.into();
48+
Error::Type { expected, actual }
49+
}
50+
}
51+
52+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
53+
pub(super) enum TypeShape {
54+
Struct,
55+
TupleStruct,
56+
Tuple,
57+
List,
58+
Array,
59+
Map,
60+
Enum,
61+
Value,
62+
Unit,
63+
}
64+
65+
impl fmt::Display for TypeShape {
66+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67+
let name = match self {
68+
TypeShape::Struct => "struct",
69+
TypeShape::TupleStruct => "tuple struct",
70+
TypeShape::Tuple => "tuple",
71+
TypeShape::List => "list",
72+
TypeShape::Array => "array",
73+
TypeShape::Map => "map",
74+
TypeShape::Enum => "enum",
75+
TypeShape::Value => "value",
76+
TypeShape::Unit => "unit",
77+
};
78+
write!(f, "{name}")
79+
}
80+
}
81+
impl<'a> From<ReflectRef<'a>> for TypeShape {
82+
fn from(value: ReflectRef<'a>) -> Self {
83+
match value {
84+
ReflectRef::Struct(_) => TypeShape::Struct,
85+
ReflectRef::TupleStruct(_) => TypeShape::TupleStruct,
86+
ReflectRef::Tuple(_) => TypeShape::Tuple,
87+
ReflectRef::List(_) => TypeShape::List,
88+
ReflectRef::Array(_) => TypeShape::Array,
89+
ReflectRef::Map(_) => TypeShape::Map,
90+
ReflectRef::Enum(_) => TypeShape::Enum,
91+
ReflectRef::Value(_) => TypeShape::Value,
92+
}
93+
}
94+
}
95+
impl From<VariantType> for TypeShape {
96+
fn from(value: VariantType) -> Self {
97+
match value {
98+
VariantType::Struct => TypeShape::Struct,
99+
VariantType::Tuple => TypeShape::Tuple,
100+
VariantType::Unit => TypeShape::Unit,
101+
}
102+
}
103+
}
104+
105+
/// A singular element access within a path.
106+
///
107+
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
108+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
109+
pub(super) enum Access<'a> {
110+
Field(Cow<'a, str>),
111+
FieldIndex(usize),
112+
TupleIndex(usize),
113+
ListIndex(usize),
114+
}
115+
116+
impl fmt::Display for Access<'_> {
117+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118+
match self {
119+
Access::Field(field) => write!(f, ".{field}"),
120+
Access::FieldIndex(index) => write!(f, "#{index}"),
121+
Access::TupleIndex(index) => write!(f, ".{index}"),
122+
Access::ListIndex(index) => write!(f, "[{index}]"),
123+
}
124+
}
125+
}
126+
127+
impl<'a> Access<'a> {
128+
pub(super) fn into_owned(self) -> Access<'static> {
129+
match self {
130+
Self::Field(value) => Access::Field(value.to_string().into()),
131+
Self::FieldIndex(value) => Access::FieldIndex(value),
132+
Self::TupleIndex(value) => Access::TupleIndex(value),
133+
Self::ListIndex(value) => Access::ListIndex(value),
134+
}
135+
}
136+
137+
fn display_value(&self) -> &dyn fmt::Display {
138+
match self {
139+
Self::Field(value) => value,
140+
Self::FieldIndex(value) | Self::TupleIndex(value) | Self::ListIndex(value) => value,
141+
}
142+
}
143+
fn kind(&self) -> &'static str {
144+
match self {
145+
Self::Field(_) => "field",
146+
Self::FieldIndex(_) => "field index",
147+
Self::TupleIndex(_) | Self::ListIndex(_) => "index",
148+
}
149+
}
150+
151+
pub(super) fn element<'r>(
152+
&self,
153+
base: &'r dyn Reflect,
154+
offset: usize,
155+
) -> Result<&'r dyn Reflect, ReflectPathError<'a>> {
156+
let ty = base.reflect_ref().into();
157+
self.element_inner(base)
158+
.and_then(|maybe| maybe.ok_or(Error::access(ty, self.clone())))
159+
.map_err(|err| err.with_offset(offset))
160+
}
161+
162+
fn element_inner<'r>(&self, base: &'r dyn Reflect) -> InnerResult<&'r dyn Reflect> {
163+
use ReflectRef::*;
164+
match (self, base.reflect_ref()) {
165+
(Self::Field(field), Struct(struct_ref)) => Ok(struct_ref.field(field.as_ref())),
166+
(Self::Field(field), Enum(enum_ref)) => match enum_ref.variant_type() {
167+
VariantType::Struct => Ok(enum_ref.field(field.as_ref())),
168+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
169+
},
170+
(&Self::FieldIndex(index), Struct(struct_ref)) => Ok(struct_ref.field_at(index)),
171+
(&Self::FieldIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() {
172+
VariantType::Struct => Ok(enum_ref.field_at(index)),
173+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
174+
},
175+
(&Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field(index)),
176+
(&Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field(index)),
177+
(&Self::TupleIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() {
178+
VariantType::Tuple => Ok(enum_ref.field_at(index)),
179+
actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)),
180+
},
181+
(&Self::ListIndex(index), List(list)) => Ok(list.get(index)),
182+
(&Self::ListIndex(index), Array(list)) => Ok(list.get(index)),
183+
(&Self::ListIndex(_), actual) => Err(Error::bad_type(TypeShape::List, actual)),
184+
(_, actual) => Err(Error::bad_type(TypeShape::Struct, actual)),
185+
}
186+
}
187+
188+
pub(super) fn element_mut<'r>(
189+
&self,
190+
base: &'r mut dyn Reflect,
191+
offset: usize,
192+
) -> Result<&'r mut dyn Reflect, ReflectPathError<'a>> {
193+
let ty = base.reflect_ref().into();
194+
self.element_inner_mut(base)
195+
.and_then(|maybe| maybe.ok_or(Error::access(ty, self.clone())))
196+
.map_err(|err| err.with_offset(offset))
197+
}
198+
199+
fn element_inner_mut<'r>(&self, base: &'r mut dyn Reflect) -> InnerResult<&'r mut dyn Reflect> {
200+
use ReflectMut::*;
201+
let base_shape: TypeShape = base.reflect_ref().into();
202+
match (self, base.reflect_mut()) {
203+
(Self::Field(field), Struct(struct_mut)) => Ok(struct_mut.field_mut(field.as_ref())),
204+
(Self::Field(field), Enum(enum_mut)) => match enum_mut.variant_type() {
205+
VariantType::Struct => Ok(enum_mut.field_mut(field.as_ref())),
206+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
207+
},
208+
(&Self::FieldIndex(index), Struct(struct_mut)) => Ok(struct_mut.field_at_mut(index)),
209+
(&Self::FieldIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() {
210+
VariantType::Struct => Ok(enum_mut.field_at_mut(index)),
211+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
212+
},
213+
(&Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field_mut(index)),
214+
(&Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field_mut(index)),
215+
(&Self::TupleIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() {
216+
VariantType::Tuple => Ok(enum_mut.field_at_mut(index)),
217+
actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)),
218+
},
219+
(&Self::ListIndex(index), List(list)) => Ok(list.get_mut(index)),
220+
(&Self::ListIndex(index), Array(list)) => Ok(list.get_mut(index)),
221+
(&Self::ListIndex(_), _) => Err(Error::bad_type(TypeShape::List, base_shape)),
222+
(_, _) => Err(Error::bad_type(TypeShape::Struct, base_shape)),
223+
}
224+
}
225+
}

0 commit comments

Comments
 (0)