Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/serde/de/maps.rs
6600 views
1
use crate::{
2
serde::{de::registration_utils::try_get_registration, TypedReflectDeserializer},
3
DynamicMap, Map, MapInfo, TypeRegistry,
4
};
5
use core::{fmt, fmt::Formatter};
6
use serde::de::{MapAccess, Visitor};
7
8
use super::ReflectDeserializerProcessor;
9
10
/// A [`Visitor`] for deserializing [`Map`] values.
11
///
12
/// [`Map`]: crate::Map
13
pub(super) struct MapVisitor<'a, P> {
14
pub map_info: &'static MapInfo,
15
pub registry: &'a TypeRegistry,
16
pub processor: Option<&'a mut P>,
17
}
18
19
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for MapVisitor<'_, P> {
20
type Value = DynamicMap;
21
22
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
23
formatter.write_str("reflected map value")
24
}
25
26
fn visit_map<V>(mut self, mut map: V) -> Result<Self::Value, V::Error>
27
where
28
V: MapAccess<'de>,
29
{
30
let mut dynamic_map = DynamicMap::default();
31
let key_registration = try_get_registration(self.map_info.key_ty(), self.registry)?;
32
let value_registration = try_get_registration(self.map_info.value_ty(), self.registry)?;
33
while let Some(key) = map.next_key_seed(TypedReflectDeserializer::new_internal(
34
key_registration,
35
self.registry,
36
self.processor.as_deref_mut(),
37
))? {
38
let value = map.next_value_seed(TypedReflectDeserializer::new_internal(
39
value_registration,
40
self.registry,
41
self.processor.as_deref_mut(),
42
))?;
43
dynamic_map.insert_boxed(key, value);
44
}
45
46
Ok(dynamic_map)
47
}
48
}
49
50