
# Objective - Implement common traits on primitives ## Solution - Derive PartialEq on types that were missing it. - Derive Copy on small types that were missing it. - Derive Serialize/Deserialize if the feature on bevy_math is enabled. - Add a lot of cursed stuff to the bevy_reflect `impls` module.
64 lines
2.0 KiB
Rust
64 lines
2.0 KiB
Rust
//! This module defines serialization/deserialization for const generic arrays.
|
|
//! Unlike serde's default behavior, it supports arbitrarily large arrays.
|
|
//! The code is based on this github comment:
|
|
//! <https://github.com/serde-rs/serde/issues/1937#issuecomment-812137971>
|
|
|
|
pub(crate) mod array {
|
|
use serde::{
|
|
de::{SeqAccess, Visitor},
|
|
ser::SerializeTuple,
|
|
Deserialize, Deserializer, Serialize, Serializer,
|
|
};
|
|
use std::marker::PhantomData;
|
|
|
|
pub fn serialize<S: Serializer, T: Serialize, const N: usize>(
|
|
data: &[T; N],
|
|
ser: S,
|
|
) -> Result<S::Ok, S::Error> {
|
|
let mut s = ser.serialize_tuple(N)?;
|
|
for item in data {
|
|
s.serialize_element(item)?;
|
|
}
|
|
s.end()
|
|
}
|
|
|
|
struct GenericArrayVisitor<T, const N: usize>(PhantomData<T>);
|
|
|
|
impl<'de, T, const N: usize> Visitor<'de> for GenericArrayVisitor<T, N>
|
|
where
|
|
T: Deserialize<'de>,
|
|
{
|
|
type Value = [T; N];
|
|
|
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
formatter.write_str(&format!("an array of length {}", N))
|
|
}
|
|
|
|
#[inline]
|
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
where
|
|
A: SeqAccess<'de>,
|
|
{
|
|
let mut data = Vec::with_capacity(N);
|
|
for _ in 0..N {
|
|
match (seq.next_element())? {
|
|
Some(val) => data.push(val),
|
|
None => return Err(serde::de::Error::invalid_length(N, &self)),
|
|
}
|
|
}
|
|
match data.try_into() {
|
|
Ok(arr) => Ok(arr),
|
|
Err(_) => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn deserialize<'de, D, T, const N: usize>(deserializer: D) -> Result<[T; N], D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
T: Deserialize<'de>,
|
|
{
|
|
deserializer.deserialize_tuple(N, GenericArrayVisitor::<T, N>(PhantomData))
|
|
}
|
|
}
|