Skip to main content

hexga_math/geometry/grid/
serde_impl.rs

1use super::*;
2
3// Todo : check https://github.com/RReverser/serde-ndim/tree/main
4// Support nested array during deserialization
5
6use serde::de::{DeserializeSeed, Deserializer, SeqAccess, Visitor};
7use std::fmt;
8use std::marker::PhantomData;
9
10impl<'de, T, Idx, const N: usize> Deserialize<'de> for GridOf<T, Idx, N>
11where
12    Idx: Integer + Deserialize<'de>,
13    T: Deserialize<'de>,
14{
15    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
16    where
17        D: Deserializer<'de>,
18    {
19        #[derive(Deserialize)]
20        pub struct Grid<T, Idx, const N: usize>
21        where
22            Idx: Integer,
23        {
24            // TODO : Can use the `size` value in json/ron to pre allocate the right among of values in the vector
25            // The size is will be deserialized first before the values,
26            // then we can give a hint to serde about the size of the vector to alloc when deserializing
27            size: Vector<Idx, N>,
28            values: Vec<T>,
29        }
30
31        let Grid { size, values } = Grid::deserialize(deserializer)?;
32        GridOf::try_from_vec(size, values).map_err(serde::de::Error::custom)
33    }
34}