Path: blob/main/crates/bevy_reflect/src/serde/de/arrays.rs
9441 views
use crate::{1array::{ArrayInfo, DynamicArray},2serde::{de::registration_utils::try_get_registration, TypedReflectDeserializer},3TypeRegistry,4};5use alloc::{string::ToString, vec::Vec};6use core::{fmt, fmt::Formatter};7use serde::de::{Error, SeqAccess, Visitor};89use super::ReflectDeserializerProcessor;1011/// A [`Visitor`] for deserializing [`Array`] values.12///13/// [`Array`]: crate::array::Array14pub(super) struct ArrayVisitor<'a, P> {15pub array_info: &'static ArrayInfo,16pub registry: &'a TypeRegistry,17pub processor: Option<&'a mut P>,18}1920impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ArrayVisitor<'_, P> {21type Value = DynamicArray;2223fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {24formatter.write_str("reflected array value")25}2627fn visit_seq<V>(mut self, mut seq: V) -> Result<Self::Value, V::Error>28where29V: SeqAccess<'de>,30{31let mut vec = Vec::with_capacity(seq.size_hint().unwrap_or_default());32let registration = try_get_registration(self.array_info.item_ty(), self.registry)?;33while let Some(value) = seq.next_element_seed(TypedReflectDeserializer::new_internal(34registration,35self.registry,36self.processor.as_deref_mut(),37))? {38vec.push(value);39}4041if vec.len() != self.array_info.capacity() {42return Err(Error::invalid_length(43vec.len(),44&self.array_info.capacity().to_string().as_str(),45));46}4748Ok(DynamicArray::new(vec.into_boxed_slice()))49}50}515253