Path: blob/main/crates/bevy_reflect/src/serde/de/maps.rs
9402 views
use crate::{1map::{DynamicMap, Map, MapInfo},2serde::{de::registration_utils::try_get_registration, TypedReflectDeserializer},3TypeRegistry,4};5use core::{fmt, fmt::Formatter};6use serde::de::{MapAccess, Visitor};78use super::ReflectDeserializerProcessor;910/// A [`Visitor`] for deserializing [`Map`] values.11///12/// [`Map`]: crate::map::Map13pub(super) struct MapVisitor<'a, P> {14pub map_info: &'static MapInfo,15pub registry: &'a TypeRegistry,16pub processor: Option<&'a mut P>,17}1819impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for MapVisitor<'_, P> {20type Value = DynamicMap;2122fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {23formatter.write_str("reflected map value")24}2526fn visit_map<V>(mut self, mut map: V) -> Result<Self::Value, V::Error>27where28V: MapAccess<'de>,29{30let mut dynamic_map = DynamicMap::default();31let key_registration = try_get_registration(self.map_info.key_ty(), self.registry)?;32let value_registration = try_get_registration(self.map_info.value_ty(), self.registry)?;33while let Some(key) = map.next_key_seed(TypedReflectDeserializer::new_internal(34key_registration,35self.registry,36self.processor.as_deref_mut(),37))? {38let value = map.next_value_seed(TypedReflectDeserializer::new_internal(39value_registration,40self.registry,41self.processor.as_deref_mut(),42))?;43dynamic_map.insert_boxed(key, value);44}4546Ok(dynamic_map)47}48}495051