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