Skip to content

Commit b100069

Browse files
nicopapMrGVSV
andcommitted
Implement MrGVSV suggestions (round 1)
Co-authored-by: Gino Valente <[email protected]>
1 parent dba74e1 commit b100069

File tree

3 files changed

+74
-65
lines changed

3 files changed

+74
-65
lines changed

crates/bevy_reflect/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
//!
234234
//! The [`GetPath`] trait allows accessing arbitrary nested fields of a [`Reflect`] type.
235235
//!
236-
//! Using [`GetPath`], it is possible to use a path strings to access a specific field
236+
//! Using `GetPath`, it is possible to use a path string to access a specific field
237237
//! of a reflected type.
238238
//!
239239
//! ```
@@ -252,8 +252,6 @@
252252
//! );
253253
//! ```
254254
//!
255-
//! Check the [`GetPath`] documentation for more details.
256-
//!
257255
//! # Type Registration
258256
//!
259257
//! This crate also comes with a [`TypeRegistry`] that can be used to store and retrieve additional type metadata at runtime,

crates/bevy_reflect/src/path/access.rs

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use super::ReflectPathError;
3+
use super::{AccessError, ReflectPathError};
44
use crate::{Reflect, ReflectMut, ReflectRef, VariantType};
55
use thiserror::Error;
66

@@ -13,34 +13,43 @@ pub(super) enum Error<'a> {
1313
access.kind(),
1414
access.display_value(),
1515
)]
16-
Access { ty: Type, access: AccessRef<'a> },
16+
Access {
17+
ty: TypeShape,
18+
access: AccessRef<'a>,
19+
},
1720

1821
#[error("invalid type shape: expected {expected} but found a reflect {actual}")]
19-
Type { expected: Type, actual: Type },
22+
Type {
23+
expected: TypeShape,
24+
actual: TypeShape,
25+
},
2026

2127
#[error("invalid enum access: expected {expected} variant but found {actual} variant")]
22-
Enum { expected: Type, actual: Type },
28+
Enum {
29+
expected: TypeShape,
30+
actual: TypeShape,
31+
},
2332
}
2433

2534
impl<'a> Error<'a> {
2635
fn with_offset(self, offset: usize) -> ReflectPathError<'a> {
27-
let error = super::AccessError(self);
36+
let error = AccessError(self);
2837
ReflectPathError::InvalidAccess { offset, error }
2938
}
3039
}
3140
impl Error<'static> {
32-
fn bad_enum(expected: Type, actual: impl Into<Type>) -> Self {
41+
fn bad_enum_variant(expected: TypeShape, actual: impl Into<TypeShape>) -> Self {
3342
let actual = actual.into();
3443
Error::Enum { expected, actual }
3544
}
36-
fn bad_type(expected: Type, actual: impl Into<Type>) -> Self {
45+
fn bad_type(expected: TypeShape, actual: impl Into<TypeShape>) -> Self {
3746
let actual = actual.into();
3847
Error::Type { expected, actual }
3948
}
4049
}
4150

4251
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
43-
pub(super) enum Type {
52+
pub(super) enum TypeShape {
4453
Struct,
4554
TupleStruct,
4655
Tuple,
@@ -52,42 +61,42 @@ pub(super) enum Type {
5261
Unit,
5362
}
5463

55-
impl fmt::Display for Type {
64+
impl fmt::Display for TypeShape {
5665
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5766
let name = match self {
58-
Type::Struct => "struct",
59-
Type::TupleStruct => "tuple struct",
60-
Type::Tuple => "tuple",
61-
Type::List => "list",
62-
Type::Array => "array",
63-
Type::Map => "map",
64-
Type::Enum => "enum",
65-
Type::Value => "value",
66-
Type::Unit => "unit",
67+
TypeShape::Struct => "struct",
68+
TypeShape::TupleStruct => "tuple struct",
69+
TypeShape::Tuple => "tuple",
70+
TypeShape::List => "list",
71+
TypeShape::Array => "array",
72+
TypeShape::Map => "map",
73+
TypeShape::Enum => "enum",
74+
TypeShape::Value => "value",
75+
TypeShape::Unit => "unit",
6776
};
6877
write!(f, "{name}")
6978
}
7079
}
71-
impl<'a> From<ReflectRef<'a>> for Type {
80+
impl<'a> From<ReflectRef<'a>> for TypeShape {
7281
fn from(value: ReflectRef<'a>) -> Self {
7382
match value {
74-
ReflectRef::Struct(_) => Type::Struct,
75-
ReflectRef::TupleStruct(_) => Type::TupleStruct,
76-
ReflectRef::Tuple(_) => Type::Tuple,
77-
ReflectRef::List(_) => Type::List,
78-
ReflectRef::Array(_) => Type::Array,
79-
ReflectRef::Map(_) => Type::Map,
80-
ReflectRef::Enum(_) => Type::Enum,
81-
ReflectRef::Value(_) => Type::Value,
83+
ReflectRef::Struct(_) => TypeShape::Struct,
84+
ReflectRef::TupleStruct(_) => TypeShape::TupleStruct,
85+
ReflectRef::Tuple(_) => TypeShape::Tuple,
86+
ReflectRef::List(_) => TypeShape::List,
87+
ReflectRef::Array(_) => TypeShape::Array,
88+
ReflectRef::Map(_) => TypeShape::Map,
89+
ReflectRef::Enum(_) => TypeShape::Enum,
90+
ReflectRef::Value(_) => TypeShape::Value,
8291
}
8392
}
8493
}
85-
impl From<VariantType> for Type {
94+
impl From<VariantType> for TypeShape {
8695
fn from(value: VariantType) -> Self {
8796
match value {
88-
VariantType::Struct => Type::Struct,
89-
VariantType::Tuple => Type::Tuple,
90-
VariantType::Unit => Type::Unit,
97+
VariantType::Struct => TypeShape::Struct,
98+
VariantType::Tuple => TypeShape::Tuple,
99+
VariantType::Unit => TypeShape::Unit,
91100
}
92101
}
93102
}
@@ -116,6 +125,17 @@ impl fmt::Display for Access {
116125
}
117126
}
118127

128+
impl Access {
129+
pub(super) fn as_ref(&self) -> AccessRef<'_> {
130+
match self {
131+
Self::Field(value) => AccessRef::Field(value),
132+
Self::FieldIndex(value) => AccessRef::FieldIndex(*value),
133+
Self::TupleIndex(value) => AccessRef::TupleIndex(*value),
134+
Self::ListIndex(value) => AccessRef::ListIndex(*value),
135+
}
136+
}
137+
}
138+
119139
/// A singular borrowed element access within a path.
120140
///
121141
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
@@ -130,17 +150,6 @@ pub(super) enum AccessRef<'a> {
130150
ListIndex(usize),
131151
}
132152

133-
impl Access {
134-
pub(super) fn as_ref(&self) -> AccessRef<'_> {
135-
match self {
136-
Self::Field(value) => AccessRef::Field(value),
137-
Self::FieldIndex(value) => AccessRef::FieldIndex(*value),
138-
Self::TupleIndex(value) => AccessRef::TupleIndex(*value),
139-
Self::ListIndex(value) => AccessRef::ListIndex(*value),
140-
}
141-
}
142-
}
143-
144153
impl<'a> AccessRef<'a> {
145154
pub(super) fn to_owned(self) -> Access {
146155
match self {
@@ -175,29 +184,30 @@ impl<'a> AccessRef<'a> {
175184
.and_then(|maybe| maybe.ok_or(Error::Access { ty, access: self }))
176185
.map_err(|err| err.with_offset(offset))
177186
}
187+
178188
fn element_inner(self, base: &dyn Reflect) -> InnerResult<&dyn Reflect> {
179189
use ReflectRef::*;
180190
match (self, base.reflect_ref()) {
181191
(Self::Field(field), Struct(struct_ref)) => Ok(struct_ref.field(field)),
182192
(Self::Field(field), Enum(enum_ref)) => match enum_ref.variant_type() {
183193
VariantType::Struct => Ok(enum_ref.field(field)),
184-
actual => Err(Error::bad_enum(Type::Struct, actual)),
194+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
185195
},
186196
(Self::FieldIndex(index), Struct(struct_ref)) => Ok(struct_ref.field_at(index)),
187197
(Self::FieldIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() {
188198
VariantType::Struct => Ok(enum_ref.field_at(index)),
189-
actual => Err(Error::bad_enum(Type::Struct, actual)),
199+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
190200
},
191201
(Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field(index)),
192202
(Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field(index)),
193203
(Self::TupleIndex(index), Enum(enum_ref)) => match enum_ref.variant_type() {
194204
VariantType::Tuple => Ok(enum_ref.field_at(index)),
195-
actual => Err(Error::bad_enum(Type::Tuple, actual)),
205+
actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)),
196206
},
197207
(Self::ListIndex(index), List(list)) => Ok(list.get(index)),
198208
(Self::ListIndex(index), Array(list)) => Ok(list.get(index)),
199-
(Self::ListIndex(_), actual) => Err(Error::bad_type(Type::List, actual)),
200-
(_, actual) => Err(Error::bad_type(Type::Struct, actual)),
209+
(Self::ListIndex(_), actual) => Err(Error::bad_type(TypeShape::List, actual)),
210+
(_, actual) => Err(Error::bad_type(TypeShape::Struct, actual)),
201211
}
202212
}
203213

@@ -211,30 +221,31 @@ impl<'a> AccessRef<'a> {
211221
.and_then(|maybe| maybe.ok_or(Error::Access { ty, access: self }))
212222
.map_err(|err| err.with_offset(offset))
213223
}
224+
214225
fn element_inner_mut(self, base: &mut dyn Reflect) -> InnerResult<&mut dyn Reflect> {
215226
use ReflectMut::*;
216-
let base_kind: Type = base.reflect_ref().into();
227+
let base_kind: TypeShape = base.reflect_ref().into();
217228
match (self, base.reflect_mut()) {
218229
(Self::Field(field), Struct(struct_mut)) => Ok(struct_mut.field_mut(field)),
219230
(Self::Field(field), Enum(enum_mut)) => match enum_mut.variant_type() {
220231
VariantType::Struct => Ok(enum_mut.field_mut(field)),
221-
actual => Err(Error::bad_enum(Type::Struct, actual)),
232+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
222233
},
223234
(Self::FieldIndex(index), Struct(struct_mut)) => Ok(struct_mut.field_at_mut(index)),
224235
(Self::FieldIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() {
225236
VariantType::Struct => Ok(enum_mut.field_at_mut(index)),
226-
actual => Err(Error::bad_enum(Type::Struct, actual)),
237+
actual => Err(Error::bad_enum_variant(TypeShape::Struct, actual)),
227238
},
228239
(Self::TupleIndex(index), TupleStruct(tuple)) => Ok(tuple.field_mut(index)),
229240
(Self::TupleIndex(index), Tuple(tuple)) => Ok(tuple.field_mut(index)),
230241
(Self::TupleIndex(index), Enum(enum_mut)) => match enum_mut.variant_type() {
231242
VariantType::Tuple => Ok(enum_mut.field_at_mut(index)),
232-
actual => Err(Error::bad_enum(Type::Tuple, actual)),
243+
actual => Err(Error::bad_enum_variant(TypeShape::Tuple, actual)),
233244
},
234245
(Self::ListIndex(index), List(list)) => Ok(list.get_mut(index)),
235246
(Self::ListIndex(index), Array(list)) => Ok(list.get_mut(index)),
236-
(Self::ListIndex(_), _) => Err(Error::bad_type(Type::List, base_kind)),
237-
(_, _) => Err(Error::bad_type(Type::Struct, base_kind)),
247+
(Self::ListIndex(_), _) => Err(Error::bad_type(TypeShape::List, base_kind)),
248+
(_, _) => Err(Error::bad_type(TypeShape::Struct, base_kind)),
238249
}
239250
}
240251
}

crates/bevy_reflect/src/path/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type ParseResult<T> = Result<T, ReflectPathParseError>;
1414
#[error(transparent)]
1515
pub struct AccessError<'a>(access::Error<'a>);
1616

17-
/// A parse error for a path string query.
17+
/// A parse error for a path string.
1818
#[derive(Debug, PartialEq, Eq, Error)]
1919
pub enum ReflectPathParseError {
2020
#[error("expected an identifier at offset {offset}")]
@@ -558,7 +558,7 @@ mod tests {
558558
use super::*;
559559
use crate as bevy_reflect;
560560
use crate::*;
561-
use access::Type;
561+
use access::TypeShape;
562562

563563
#[derive(Reflect)]
564564
struct A {
@@ -777,7 +777,7 @@ mod tests {
777777
ReflectPathError::InvalidAccess {
778778
offset: 2,
779779
error: AccessError(access::Error::Access {
780-
ty: Type::Struct,
780+
ty: TypeShape::Struct,
781781
access: AccessRef::Field("notreal"),
782782
}),
783783
}
@@ -788,8 +788,8 @@ mod tests {
788788
ReflectPathError::InvalidAccess {
789789
offset: 13,
790790
error: AccessError(access::Error::Enum {
791-
actual: Type::Unit,
792-
expected: Type::Tuple
791+
actual: TypeShape::Unit,
792+
expected: TypeShape::Tuple
793793
}),
794794
}
795795
);
@@ -804,8 +804,8 @@ mod tests {
804804
ReflectPathError::InvalidAccess {
805805
offset: 2,
806806
error: AccessError(access::Error::Type {
807-
actual: Type::Struct,
808-
expected: Type::List
807+
actual: TypeShape::Struct,
808+
expected: TypeShape::List
809809
}),
810810
}
811811
);
@@ -815,8 +815,8 @@ mod tests {
815815
ReflectPathError::InvalidAccess {
816816
offset: 2,
817817
error: AccessError(access::Error::Type {
818-
actual: Type::List,
819-
expected: Type::Struct
818+
actual: TypeShape::List,
819+
expected: TypeShape::Struct
820820
}),
821821
}
822822
);

0 commit comments

Comments
 (0)