-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! 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)) | ||
} | ||
} |