Skip to content

Commit

Permalink
perf: manual serde for quantity vec (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 17, 2024
1 parent 7de9d84 commit 3170c6f
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions crates/serde/src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,25 @@ pub mod opt {
/// See [`quantity`](self) for more information.
pub mod vec {
use super::private::ConvertRuint;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::{fmt, marker::PhantomData};
use serde::{
de::{SeqAccess, Visitor},
ser::SerializeSeq,
Deserializer, Serializer,
};

/// Serializes a vector of primitive numbers as a "quantity" hex string.
pub fn serialize<T, S>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
where
T: ConvertRuint,
S: Serializer,
{
let vec = value.iter().copied().map(T::into_ruint).collect::<Vec<_>>();
vec.serialize(serializer)
let mut seq = serializer.serialize_seq(Some(value.len()))?;
for val in value {
seq.serialize_element(&val.into_ruint())?;
}
seq.end()
}

/// Deserializes a vector of primitive numbers from a "quantity" hex string.
Expand All @@ -84,8 +90,35 @@ pub mod vec {
T: ConvertRuint,
D: Deserializer<'de>,
{
let vec = Vec::<T::Ruint>::deserialize(deserializer)?;
Ok(vec.into_iter().map(T::from_ruint).collect())
struct VecVisitor<T> {
marker: PhantomData<T>,
}

impl<'de, T> Visitor<'de> for VecVisitor<T>
where
T: ConvertRuint,
{
type Value = Vec<T>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a sequence")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut values = Vec::<T>::with_capacity(seq.size_hint().unwrap_or(0));

while let Some(value) = seq.next_element::<T::Ruint>()? {
values.push(T::from_ruint(value));
}
Ok(values)
}
}

let visitor = VecVisitor { marker: PhantomData };
deserializer.deserialize_seq(visitor)
}
}

Expand Down

0 comments on commit 3170c6f

Please sign in to comment.