Path: blob/main/crates/bevy_reflect/src/serde/de/maps.rs
6600 views
use crate::{1serde::{de::registration_utils::try_get_registration, TypedReflectDeserializer},2DynamicMap, Map, MapInfo, TypeRegistry,3};4use core::{fmt, fmt::Formatter};5use serde::de::{MapAccess, Visitor};67use super::ReflectDeserializerProcessor;89/// A [`Visitor`] for deserializing [`Map`] values.10///11/// [`Map`]: crate::Map12pub(super) struct MapVisitor<'a, P> {13pub map_info: &'static MapInfo,14pub registry: &'a TypeRegistry,15pub processor: Option<&'a mut P>,16}1718impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for MapVisitor<'_, P> {19type Value = DynamicMap;2021fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {22formatter.write_str("reflected map value")23}2425fn visit_map<V>(mut self, mut map: V) -> Result<Self::Value, V::Error>26where27V: MapAccess<'de>,28{29let mut dynamic_map = DynamicMap::default();30let key_registration = try_get_registration(self.map_info.key_ty(), self.registry)?;31let value_registration = try_get_registration(self.map_info.value_ty(), self.registry)?;32while let Some(key) = map.next_key_seed(TypedReflectDeserializer::new_internal(33key_registration,34self.registry,35self.processor.as_deref_mut(),36))? {37let value = map.next_value_seed(TypedReflectDeserializer::new_internal(38value_registration,39self.registry,40self.processor.as_deref_mut(),41))?;42dynamic_map.insert_boxed(key, value);43}4445Ok(dynamic_map)46}47}484950