Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/serde/de/arrays.rs
9441 views
1
use crate::{
2
array::{ArrayInfo, DynamicArray},
3
serde::{de::registration_utils::try_get_registration, TypedReflectDeserializer},
4
TypeRegistry,
5
};
6
use alloc::{string::ToString, vec::Vec};
7
use core::{fmt, fmt::Formatter};
8
use serde::de::{Error, SeqAccess, Visitor};
9
10
use super::ReflectDeserializerProcessor;
11
12
/// A [`Visitor`] for deserializing [`Array`] values.
13
///
14
/// [`Array`]: crate::array::Array
15
pub(super) struct ArrayVisitor<'a, P> {
16
pub array_info: &'static ArrayInfo,
17
pub registry: &'a TypeRegistry,
18
pub processor: Option<&'a mut P>,
19
}
20
21
impl<'de, P: ReflectDeserializerProcessor> Visitor<'de> for ArrayVisitor<'_, P> {
22
type Value = DynamicArray;
23
24
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
25
formatter.write_str("reflected array value")
26
}
27
28
fn visit_seq<V>(mut self, mut seq: V) -> Result<Self::Value, V::Error>
29
where
30
V: SeqAccess<'de>,
31
{
32
let mut vec = Vec::with_capacity(seq.size_hint().unwrap_or_default());
33
let registration = try_get_registration(self.array_info.item_ty(), self.registry)?;
34
while let Some(value) = seq.next_element_seed(TypedReflectDeserializer::new_internal(
35
registration,
36
self.registry,
37
self.processor.as_deref_mut(),
38
))? {
39
vec.push(value);
40
}
41
42
if vec.len() != self.array_info.capacity() {
43
return Err(Error::invalid_length(
44
vec.len(),
45
&self.array_info.capacity().to_string().as_str(),
46
));
47
}
48
49
Ok(DynamicArray::new(vec.into_boxed_slice()))
50
}
51
}
52
53