1
- use std:: fmt;
1
+ use std:: { borrow :: Cow , fmt} ;
2
2
3
3
use super :: { AccessError , ReflectPathError } ;
4
4
use crate :: { Reflect , ReflectMut , ReflectRef , VariantType } ;
@@ -13,10 +13,7 @@ pub(super) enum Error<'a> {
13
13
access. kind( ) ,
14
14
access. display_value( ) ,
15
15
) ]
16
- Access {
17
- ty : TypeShape ,
18
- access : AccessRef < ' a > ,
19
- } ,
16
+ Access { ty : TypeShape , access : Access < ' a > } ,
20
17
21
18
#[ error( "invalid type shape: expected {expected} but found a reflect {actual}" ) ]
22
19
Type {
@@ -36,6 +33,10 @@ impl<'a> Error<'a> {
36
33
let error = AccessError ( self ) ;
37
34
ReflectPathError :: InvalidAccess { offset, error }
38
35
}
36
+
37
+ fn access ( ty : TypeShape , access : Access < ' a > ) -> Self {
38
+ Self :: Access { ty, access }
39
+ }
39
40
}
40
41
impl Error < ' static > {
41
42
fn bad_enum_variant ( expected : TypeShape , actual : impl Into < TypeShape > ) -> Self {
@@ -101,20 +102,18 @@ impl From<VariantType> for TypeShape {
101
102
}
102
103
}
103
104
104
- /// A singular owned element access within a path.
105
+ /// A singular element access within a path.
105
106
///
106
107
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
107
- ///
108
- /// A path is composed of multiple accesses in sequence.
109
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
110
- pub ( super ) enum Access {
111
- Field ( Box < str > ) ,
108
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
109
+ pub ( super ) enum Access < ' a > {
110
+ Field ( Cow < ' a , str > ) ,
112
111
FieldIndex ( usize ) ,
113
112
TupleIndex ( usize ) ,
114
113
ListIndex ( usize ) ,
115
114
}
116
115
117
- impl fmt:: Display for Access {
116
+ impl fmt:: Display for Access < ' _ > {
118
117
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
119
118
match self {
120
119
Access :: Field ( field) => write ! ( f, ".{field}" ) ,
@@ -125,35 +124,10 @@ impl fmt::Display for Access {
125
124
}
126
125
}
127
126
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
-
139
- /// A singular borrowed element access within a path.
140
- ///
141
- /// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
142
- ///
143
- /// Does not own the backing store it's sourced from.
144
- /// For an owned version, you can convert one to an [`Access`] with [`AccessRef::to_owned`].
145
- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
146
- pub ( super ) enum AccessRef < ' a > {
147
- Field ( & ' a str ) ,
148
- FieldIndex ( usize ) ,
149
- TupleIndex ( usize ) ,
150
- ListIndex ( usize ) ,
151
- }
152
-
153
- impl < ' a > AccessRef < ' a > {
154
- pub ( super ) fn to_owned ( self ) -> Access {
127
+ impl < ' a > Access < ' a > {
128
+ pub ( super ) fn into_owned ( self ) -> Access < ' static > {
155
129
match self {
156
- Self :: Field ( value) => Access :: Field ( value. to_string ( ) . into_boxed_str ( ) ) ,
130
+ Self :: Field ( value) => Access :: Field ( value. to_string ( ) . into ( ) ) ,
157
131
Self :: FieldIndex ( value) => Access :: FieldIndex ( value) ,
158
132
Self :: TupleIndex ( value) => Access :: TupleIndex ( value) ,
159
133
Self :: ListIndex ( value) => Access :: ListIndex ( value) ,
@@ -174,78 +148,78 @@ impl<'a> AccessRef<'a> {
174
148
}
175
149
}
176
150
177
- pub ( super ) fn element (
178
- self ,
179
- base : & dyn Reflect ,
151
+ pub ( super ) fn element < ' r > (
152
+ & self ,
153
+ base : & ' r dyn Reflect ,
180
154
offset : usize ,
181
- ) -> Result < & dyn Reflect , ReflectPathError < ' a > > {
155
+ ) -> Result < & ' r dyn Reflect , ReflectPathError < ' a > > {
182
156
let ty = base. reflect_ref ( ) . into ( ) ;
183
157
self . element_inner ( base)
184
- . and_then ( |maybe| maybe. ok_or ( Error :: Access { ty, access : self } ) )
158
+ . and_then ( |maybe| maybe. ok_or ( Error :: access ( ty, self . clone ( ) ) ) )
185
159
. map_err ( |err| err. with_offset ( offset) )
186
160
}
187
161
188
- fn element_inner ( self , base : & dyn Reflect ) -> InnerResult < & dyn Reflect > {
162
+ fn element_inner < ' r > ( & self , base : & ' r dyn Reflect ) -> InnerResult < & ' r dyn Reflect > {
189
163
use ReflectRef :: * ;
190
164
match ( self , base. reflect_ref ( ) ) {
191
- ( Self :: Field ( field) , Struct ( struct_ref) ) => Ok ( struct_ref. field ( field) ) ,
165
+ ( Self :: Field ( field) , Struct ( struct_ref) ) => Ok ( struct_ref. field ( field. as_ref ( ) ) ) ,
192
166
( Self :: Field ( field) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
193
- VariantType :: Struct => Ok ( enum_ref. field ( field) ) ,
167
+ VariantType :: Struct => Ok ( enum_ref. field ( field. as_ref ( ) ) ) ,
194
168
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
195
169
} ,
196
- ( Self :: FieldIndex ( index) , Struct ( struct_ref) ) => Ok ( struct_ref. field_at ( index) ) ,
197
- ( Self :: FieldIndex ( index) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
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 ( ) {
198
172
VariantType :: Struct => Ok ( enum_ref. field_at ( index) ) ,
199
173
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
200
174
} ,
201
- ( Self :: TupleIndex ( index) , TupleStruct ( tuple) ) => Ok ( tuple. field ( index) ) ,
202
- ( Self :: TupleIndex ( index) , Tuple ( tuple) ) => Ok ( tuple. field ( index) ) ,
203
- ( Self :: TupleIndex ( index) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
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 ( ) {
204
178
VariantType :: Tuple => Ok ( enum_ref. field_at ( index) ) ,
205
179
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Tuple , actual) ) ,
206
180
} ,
207
- ( Self :: ListIndex ( index) , List ( list) ) => Ok ( list. get ( index) ) ,
208
- ( Self :: ListIndex ( index) , Array ( list) ) => Ok ( list. get ( index) ) ,
209
- ( Self :: ListIndex ( _) , actual) => Err ( Error :: bad_type ( TypeShape :: List , actual) ) ,
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) ) ,
210
184
( _, actual) => Err ( Error :: bad_type ( TypeShape :: Struct , actual) ) ,
211
185
}
212
186
}
213
187
214
- pub ( super ) fn element_mut (
215
- self ,
216
- base : & mut dyn Reflect ,
188
+ pub ( super ) fn element_mut < ' r > (
189
+ & self ,
190
+ base : & ' r mut dyn Reflect ,
217
191
offset : usize ,
218
- ) -> Result < & mut dyn Reflect , ReflectPathError < ' a > > {
192
+ ) -> Result < & ' r mut dyn Reflect , ReflectPathError < ' a > > {
219
193
let ty = base. reflect_ref ( ) . into ( ) ;
220
194
self . element_inner_mut ( base)
221
- . and_then ( |maybe| maybe. ok_or ( Error :: Access { ty, access : self } ) )
195
+ . and_then ( |maybe| maybe. ok_or ( Error :: access ( ty, self . clone ( ) ) ) )
222
196
. map_err ( |err| err. with_offset ( offset) )
223
197
}
224
198
225
- fn element_inner_mut ( self , base : & mut dyn Reflect ) -> InnerResult < & mut dyn Reflect > {
199
+ fn element_inner_mut < ' r > ( & self , base : & ' r mut dyn Reflect ) -> InnerResult < & ' r mut dyn Reflect > {
226
200
use ReflectMut :: * ;
227
- let base_kind : TypeShape = base. reflect_ref ( ) . into ( ) ;
201
+ let base_shape : TypeShape = base. reflect_ref ( ) . into ( ) ;
228
202
match ( self , base. reflect_mut ( ) ) {
229
- ( Self :: Field ( field) , Struct ( struct_mut) ) => Ok ( struct_mut. field_mut ( field) ) ,
203
+ ( Self :: Field ( field) , Struct ( struct_mut) ) => Ok ( struct_mut. field_mut ( field. as_ref ( ) ) ) ,
230
204
( Self :: Field ( field) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
231
- VariantType :: Struct => Ok ( enum_mut. field_mut ( field) ) ,
205
+ VariantType :: Struct => Ok ( enum_mut. field_mut ( field. as_ref ( ) ) ) ,
232
206
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
233
207
} ,
234
- ( Self :: FieldIndex ( index) , Struct ( struct_mut) ) => Ok ( struct_mut. field_at_mut ( index) ) ,
235
- ( Self :: FieldIndex ( index) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
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 ( ) {
236
210
VariantType :: Struct => Ok ( enum_mut. field_at_mut ( index) ) ,
237
211
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
238
212
} ,
239
- ( Self :: TupleIndex ( index) , TupleStruct ( tuple) ) => Ok ( tuple. field_mut ( index) ) ,
240
- ( Self :: TupleIndex ( index) , Tuple ( tuple) ) => Ok ( tuple. field_mut ( index) ) ,
241
- ( Self :: TupleIndex ( index) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
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 ( ) {
242
216
VariantType :: Tuple => Ok ( enum_mut. field_at_mut ( index) ) ,
243
217
actual => Err ( Error :: bad_enum_variant ( TypeShape :: Tuple , actual) ) ,
244
218
} ,
245
- ( Self :: ListIndex ( index) , List ( list) ) => Ok ( list. get_mut ( index) ) ,
246
- ( Self :: ListIndex ( index) , Array ( list) ) => Ok ( list. get_mut ( index) ) ,
247
- ( Self :: ListIndex ( _) , _) => Err ( Error :: bad_type ( TypeShape :: List , base_kind ) ) ,
248
- ( _, _) => Err ( Error :: bad_type ( TypeShape :: Struct , base_kind ) ) ,
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 ) ) ,
249
223
}
250
224
}
251
225
}
0 commit comments