Skip to content

Commit 1b0d2f7

Browse files
committed
get it working with the type system
1 parent 8d5f341 commit 1b0d2f7

File tree

13 files changed

+61
-72
lines changed

13 files changed

+61
-72
lines changed

crates/bevy_reflect/src/serde/de/arrays.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::ReflectDeserializerProcessor;
1313
pub(super) struct ArrayVisitor<'a, P> {
1414
pub array_info: &'static ArrayInfo,
1515
pub registry: &'a TypeRegistry,
16-
pub processor: P,
16+
pub processor: Option<&'a mut P>,
1717
}
1818

1919
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ArrayVisitor<'_, P> {
@@ -32,7 +32,7 @@ impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ArrayVisitor<'_, P>
3232
while let Some(value) = seq.next_element_seed(TypedReflectDeserializer::new_internal(
3333
registration,
3434
self.registry,
35-
&mut self.processor,
35+
self.processor.as_deref_mut(),
3636
))? {
3737
vec.push(value);
3838
}

crates/bevy_reflect/src/serde/de/deserializer.rs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,6 @@ impl ReflectDeserializerProcessor for () {
135135
}
136136
}
137137

138-
impl<T: ReflectDeserializerProcessor + ?Sized> ReflectDeserializerProcessor for &mut T {
139-
fn try_deserialize<'de, D>(
140-
&mut self,
141-
registration: &TypeRegistration,
142-
deserializer: D,
143-
) -> Result<Result<Box<dyn PartialReflect>, D>, D::Error>
144-
where
145-
D: serde::Deserializer<'de>,
146-
{
147-
(*self).try_deserialize(registration, deserializer)
148-
}
149-
}
150-
151138
/// A general purpose deserializer for reflected types.
152139
///
153140
/// This is the deserializer counterpart to [`ReflectSerializer`].
@@ -234,20 +221,20 @@ impl<T: ReflectDeserializerProcessor + ?Sized> ReflectDeserializerProcessor for
234221
/// [`new_with_processor`]: Self::new_with_processor
235222
pub struct ReflectDeserializer<'a, P> {
236223
registry: &'a TypeRegistry,
237-
processor: P,
224+
processor: Option<&'a mut P>,
238225
}
239226

240227
impl<'a> ReflectDeserializer<'a, ()> {
241228
/// Creates a deserializer with no processor.
242229
///
243230
/// If you want to add custom logic for deserializing certain types, use
244-
/// [`new_with_processor`].
231+
/// [`with_processor`].
245232
///
246-
/// [`new_with_processor`]: Self::new_with_processor
233+
/// [`with_processor`]: Self::with_processor
247234
pub fn new(registry: &'a TypeRegistry) -> Self {
248235
Self {
249236
registry,
250-
processor: (),
237+
processor: None,
251238
}
252239
}
253240
}
@@ -259,10 +246,10 @@ impl<'a, P: ReflectDeserializerProcessor> ReflectDeserializer<'a, P> {
259246
/// [`new`].
260247
///
261248
/// [`new`]: Self::new
262-
pub fn new_with_processor(registry: &'a TypeRegistry, processor: P) -> Self {
249+
pub fn with_processor(registry: &'a TypeRegistry, processor: &'a mut P) -> Self {
263250
Self {
264251
registry,
265-
processor,
252+
processor: Some(processor),
266253
}
267254
}
268255
}
@@ -276,11 +263,11 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de> for ReflectDeser
276263
{
277264
struct UntypedReflectDeserializerVisitor<'a, P> {
278265
registry: &'a TypeRegistry,
279-
processor: P,
266+
processor: Option<&'a mut P>,
280267
}
281268

282-
impl<'a, 'de, P: ReflectDeserializerProcessor> Visitor<'de>
283-
for UntypedReflectDeserializerVisitor<'a, P>
269+
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de>
270+
for UntypedReflectDeserializerVisitor<'_, P>
284271
{
285272
type Value = Box<dyn PartialReflect>;
286273

@@ -289,7 +276,7 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de> for ReflectDeser
289276
.write_str("map containing `type` and `value` entries for the reflected value")
290277
}
291278

292-
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
279+
fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
293280
where
294281
A: MapAccess<'de>,
295282
{
@@ -300,7 +287,7 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de> for ReflectDeser
300287
let value = map.next_value_seed(TypedReflectDeserializer {
301288
registration,
302289
registry: self.registry,
303-
processor: self.processor,
290+
processor: self.processor.as_deref_mut(),
304291
})?;
305292

306293
if map.next_key::<IgnoredAny>()?.is_some() {
@@ -402,24 +389,24 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de> for ReflectDeser
402389
pub struct TypedReflectDeserializer<'a, P> {
403390
registration: &'a TypeRegistration,
404391
registry: &'a TypeRegistry,
405-
processor: P,
392+
processor: Option<&'a mut P>,
406393
}
407394

408395
impl<'a> TypedReflectDeserializer<'a, ()> {
409396
/// Creates a typed deserializer with no processor.
410397
///
411398
/// If you want to add custom logic for deserializing certain types, use
412-
/// [`new_with_processor`].
399+
/// [`with_processor`].
413400
///
414-
/// [`new_with_processor`]: Self::new_with_processor
401+
/// [`with_processor`]: Self::with_processor
415402
pub fn new(registration: &'a TypeRegistration, registry: &'a TypeRegistry) -> Self {
416403
#[cfg(feature = "debug_stack")]
417404
TYPE_INFO_STACK.set(crate::type_info_stack::TypeInfoStack::new());
418405

419406
Self {
420407
registration,
421408
registry,
422-
processor: (),
409+
processor: None,
423410
}
424411
}
425412
}
@@ -431,26 +418,26 @@ impl<'a, P: ReflectDeserializerProcessor> TypedReflectDeserializer<'a, P> {
431418
/// [`new`].
432419
///
433420
/// [`new`]: Self::new
434-
pub fn new_with_processor(
421+
pub fn with_processor(
435422
registration: &'a TypeRegistration,
436423
registry: &'a TypeRegistry,
437-
processor: P,
424+
processor: &'a mut P,
438425
) -> Self {
439426
#[cfg(feature = "debug_stack")]
440427
TYPE_INFO_STACK.set(crate::type_info_stack::TypeInfoStack::new());
441428

442429
Self {
443430
registration,
444431
registry,
445-
processor,
432+
processor: Some(processor),
446433
}
447434
}
448435

449436
/// An internal constructor for creating a deserializer without resetting the type info stack.
450437
pub(super) fn new_internal(
451438
registration: &'a TypeRegistration,
452439
registry: &'a TypeRegistry,
453-
processor: P,
440+
processor: Option<&'a mut P>,
454441
) -> Self {
455442
Self {
456443
registration,
@@ -472,17 +459,18 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de>
472459
let deserialize_internal = || -> Result<Self::Value, D::Error> {
473460
// First, check if our processor wants to deserialize this type
474461
// This takes priority over any other deserialization operations
475-
let deserializer = match self
476-
.processor
477-
.try_deserialize(self.registration, deserializer)
478-
{
479-
Ok(Ok(value)) => {
480-
return Ok(value);
481-
}
482-
Err(err) => {
483-
return Err(make_custom_error(err));
462+
let deserializer = if let Some(processor) = self.processor.as_deref_mut() {
463+
match processor.try_deserialize(self.registration, deserializer) {
464+
Ok(Ok(value)) => {
465+
return Ok(value);
466+
}
467+
Err(err) => {
468+
return Err(make_custom_error(err));
469+
}
470+
Ok(Err(deserializer)) => deserializer,
484471
}
485-
Ok(Err(deserializer)) => deserializer,
472+
} else {
473+
deserializer
486474
};
487475

488476
let type_path = self.registration.type_info().type_path();

crates/bevy_reflect/src/serde/de/enums.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) struct EnumVisitor<'a, P> {
2424
pub enum_info: &'static EnumInfo,
2525
pub registration: &'a TypeRegistration,
2626
pub registry: &'a TypeRegistry,
27-
pub processor: P,
27+
pub processor: Option<&'a mut P>,
2828
}
2929

3030
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for EnumVisitor<'_, P> {
@@ -149,7 +149,7 @@ struct StructVariantVisitor<'a, P> {
149149
struct_info: &'static StructVariantInfo,
150150
registration: &'a TypeRegistration,
151151
registry: &'a TypeRegistry,
152-
processor: P,
152+
processor: Option<&'a mut P>,
153153
}
154154

155155
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for StructVariantVisitor<'_, P> {
@@ -190,7 +190,7 @@ struct TupleVariantVisitor<'a, P> {
190190
tuple_info: &'static TupleVariantInfo,
191191
registration: &'a TypeRegistration,
192192
registry: &'a TypeRegistry,
193-
processor: P,
193+
processor: Option<&'a mut P>,
194194
}
195195

196196
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for TupleVariantVisitor<'_, P> {

crates/bevy_reflect/src/serde/de/lists.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::ReflectDeserializerProcessor;
1313
pub(super) struct ListVisitor<'a, P> {
1414
pub list_info: &'static ListInfo,
1515
pub registry: &'a TypeRegistry,
16-
pub processor: P,
16+
pub processor: Option<&'a mut P>,
1717
}
1818

1919
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ListVisitor<'_, P> {
@@ -32,7 +32,7 @@ impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ListVisitor<'_, P> {
3232
while let Some(value) = seq.next_element_seed(TypedReflectDeserializer::new_internal(
3333
registration,
3434
self.registry,
35-
&mut self.processor,
35+
self.processor.as_deref_mut(),
3636
))? {
3737
list.push_box(value);
3838
}

crates/bevy_reflect/src/serde/de/maps.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::ReflectDeserializerProcessor;
1313
pub(super) struct MapVisitor<'a, P> {
1414
pub map_info: &'static MapInfo,
1515
pub registry: &'a TypeRegistry,
16-
pub processor: P,
16+
pub processor: Option<&'a mut P>,
1717
}
1818

1919
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for MapVisitor<'_, P> {
@@ -33,12 +33,12 @@ impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for MapVisitor<'_, P> {
3333
while let Some(key) = map.next_key_seed(TypedReflectDeserializer::new_internal(
3434
key_registration,
3535
self.registry,
36-
&mut self.processor,
36+
self.processor.as_deref_mut(),
3737
))? {
3838
let value = map.next_value_seed(TypedReflectDeserializer::new_internal(
3939
value_registration,
4040
self.registry,
41-
&mut self.processor,
41+
self.processor.as_deref_mut(),
4242
))?;
4343
dynamic_map.insert_boxed(key, value);
4444
}

crates/bevy_reflect/src/serde/de/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ mod tests {
276276
let mut registry = get_registry();
277277
registry.register::<Foo>();
278278
let registration = registry.get(TypeId::of::<Foo>()).unwrap();
279-
let reflect_deserializer =
280-
TypedReflectDeserializer::new_internal(registration, &registry, ());
279+
let reflect_deserializer = TypedReflectDeserializer::new(registration, &registry);
281280
let mut ron_deserializer = ron::de::Deserializer::from_str(input).unwrap();
282281
let dynamic_output = reflect_deserializer
283282
.deserialize(&mut ron_deserializer)
@@ -556,8 +555,9 @@ mod tests {
556555
let mut registry = get_registry();
557556
registry.register::<Foo>();
558557
let registration = registry.get(TypeId::of::<Foo>()).unwrap();
558+
let mut processor = FooProcessor;
559559
let reflect_deserializer =
560-
TypedReflectDeserializer::new_internal(registration, &registry, FooProcessor);
560+
TypedReflectDeserializer::with_processor(registration, &registry, &mut processor);
561561
let mut ron_deserializer = ron::de::Deserializer::from_str(input).unwrap();
562562
let dynamic_output = reflect_deserializer
563563
.deserialize(&mut ron_deserializer)
@@ -610,8 +610,9 @@ mod tests {
610610
let mut registry = get_registry();
611611
registry.register::<Foo>();
612612
let registration = registry.get(TypeId::of::<Foo>()).unwrap();
613+
let mut processor = FooProcessor;
613614
let reflect_deserializer =
614-
TypedReflectDeserializer::new_internal(registration, &registry, FooProcessor);
615+
TypedReflectDeserializer::with_processor(registration, &registry, &mut processor);
615616
let mut ron_deserializer = ron::de::Deserializer::from_str(input).unwrap();
616617
let dynamic_output = reflect_deserializer
617618
.deserialize(&mut ron_deserializer)
@@ -648,8 +649,8 @@ mod tests {
648649

649650
let input = r#"{"i32":123}"#;
650651
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
651-
let reflect_deserializer =
652-
ReflectDeserializer::new_with_processor(&registry, ErroringProcessor);
652+
let mut processor = ErroringProcessor;
653+
let reflect_deserializer = ReflectDeserializer::with_processor(&registry, &mut processor);
653654
let error = reflect_deserializer
654655
.deserialize(&mut deserializer)
655656
.unwrap_err();
@@ -692,13 +693,13 @@ mod tests {
692693
let input = r#"{"i32":0}"#;
693694

694695
let mut values_found = 0_usize;
695-
let deserializer_processor = ValueCountingProcessor {
696+
let mut deserializer_processor = ValueCountingProcessor {
696697
values_found: &mut values_found,
697698
};
698699

699700
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
700701
let reflect_deserializer =
701-
ReflectDeserializer::new_with_processor(&registry, deserializer_processor);
702+
ReflectDeserializer::with_processor(&registry, &mut deserializer_processor);
702703
reflect_deserializer.deserialize(&mut deserializer).unwrap();
703704
assert_eq!(1, values_found);
704705
}

crates/bevy_reflect/src/serde/de/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::ReflectDeserializerProcessor;
1414
pub(super) struct OptionVisitor<'a, P> {
1515
pub enum_info: &'static EnumInfo,
1616
pub registry: &'a TypeRegistry,
17-
pub processor: P,
17+
pub processor: Option<&'a mut P>,
1818
}
1919

2020
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for OptionVisitor<'_, P> {

crates/bevy_reflect/src/serde/de/sets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::ReflectDeserializerProcessor;
1313
pub(super) struct SetVisitor<'a, P> {
1414
pub set_info: &'static SetInfo,
1515
pub registry: &'a TypeRegistry,
16-
pub processor: P,
16+
pub processor: Option<&'a mut P>,
1717
}
1818

1919
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for SetVisitor<'_, P> {
@@ -32,7 +32,7 @@ impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for SetVisitor<'_, P> {
3232
while let Some(value) = set.next_element_seed(TypedReflectDeserializer::new_internal(
3333
value_registration,
3434
self.registry,
35-
&mut self.processor,
35+
self.processor.as_deref_mut(),
3636
))? {
3737
dynamic_set.insert_boxed(value);
3838
}

crates/bevy_reflect/src/serde/de/struct_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub(super) fn visit_struct<'de, T, V, P>(
9090
info: &'static T,
9191
registration: &TypeRegistration,
9292
registry: &TypeRegistry,
93-
mut processor: P,
93+
mut processor: Option<&mut P>,
9494
) -> Result<DynamicStruct, V::Error>
9595
where
9696
T: StructLikeInfo,
@@ -111,7 +111,7 @@ where
111111
let value = map.next_value_seed(TypedReflectDeserializer::new_internal(
112112
registration,
113113
registry,
114-
&mut processor,
114+
processor.as_deref_mut(),
115115
))?;
116116
dynamic_struct.insert_boxed(&key, value);
117117
}
@@ -139,7 +139,7 @@ pub(super) fn visit_struct_seq<'de, T, V, P>(
139139
info: &T,
140140
registration: &TypeRegistration,
141141
registry: &TypeRegistry,
142-
mut processor: P,
142+
mut processor: Option<&mut P>,
143143
) -> Result<DynamicStruct, V::Error>
144144
where
145145
T: StructLikeInfo,
@@ -174,7 +174,7 @@ where
174174
.next_element_seed(TypedReflectDeserializer::new_internal(
175175
try_get_registration(*info.field_at(index)?.ty(), registry)?,
176176
registry,
177-
&mut processor,
177+
processor.as_deref_mut(),
178178
))?
179179
.ok_or_else(|| Error::invalid_length(index, &len.to_string().as_str()))?;
180180
dynamic_struct.insert_boxed(name, value);

crates/bevy_reflect/src/serde/de/structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) struct StructVisitor<'a, P> {
1414
pub struct_info: &'static StructInfo,
1515
pub registration: &'a TypeRegistration,
1616
pub registry: &'a TypeRegistry,
17-
pub processor: P,
17+
pub processor: Option<&'a mut P>,
1818
}
1919

2020
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for StructVisitor<'_, P> {

0 commit comments

Comments
 (0)