//! This adds a few utility functions for serializing and deserializing //! [arkworks](http://arkworks.rs/) types that implement [CanonicalSerialize] and [CanonicalDeserialize]. use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use serde_with::Bytes; // // Serialization with serde // pub mod ser { //! You can use this module for serialization and deserializing arkworks types with [serde]. //! Simply use the following attribute on your field: //! `#[serde(with = "o1_utils::serialization::ser") attribute"]` use super::*; use serde_with::{DeserializeAs, SerializeAs}; /// You can use this to serialize an arkworks type with serde and the "serialize_with" attribute. /// See pub fn serialize(val: impl CanonicalSerialize, serializer: S) -> Result where S: serde::Serializer, { let mut bytes = vec![]; val.serialize(&mut bytes) .map_err(serde::ser::Error::custom)?; Bytes::serialize_as(&bytes, serializer) } /// You can use this to deserialize an arkworks type with serde and the "deserialize_with" attribute. /// See pub fn deserialize<'de, T, D>(deserializer: D) -> Result where T: CanonicalDeserialize, D: serde::Deserializer<'de>, { let bytes: Vec = Bytes::deserialize_as(deserializer)?; T::deserialize(&mut &bytes[..]).map_err(serde::de::Error::custom) } } // // Serialization with [serde_with] // /// You can use [SerdeAs] with [serde_with] in order to serialize and deserialize types that implement [CanonicalSerialize] and [CanonicalDeserialize], /// or containers of types that implement these traits (Vec, arrays, etc.) /// Simply add annotations like `#[serde_as(as = "o1_utils::serialization::SerdeAs")]` /// See pub struct SerdeAs; impl serde_with::SerializeAs for SerdeAs where T: CanonicalSerialize, { fn serialize_as(val: &T, serializer: S) -> Result where S: serde::Serializer, { let mut bytes = vec![]; val.serialize(&mut bytes) .map_err(serde::ser::Error::custom)?; Bytes::serialize_as(&bytes, serializer) } } impl<'de, T> serde_with::DeserializeAs<'de, T> for SerdeAs where T: CanonicalDeserialize, { fn deserialize_as(deserializer: D) -> Result where D: serde::Deserializer<'de>, { let bytes: Vec = Bytes::deserialize_as(deserializer)?; T::deserialize(&mut &bytes[..]).map_err(serde::de::Error::custom) } }