Skip to content

Commit

Permalink
Implement borsh serialization routines
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Feb 29, 2024
1 parent ae38b91 commit 0804a16
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/borsh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#![cfg_attr(docsrs, doc(cfg(feature = "borsh")))]

use alloc::vec::Vec;
use core::hash::BuildHasher;
use core::hash::Hash;
use core::iter::ExactSizeIterator;
use core::mem::size_of;

use borsh::error::ERROR_ZST_FORBIDDEN;
use borsh::io::{Error, ErrorKind, Read, Result, Write};
use borsh::{BorshDeserialize, BorshSerialize};

use crate::map::IndexMap;
use crate::set::IndexSet;

impl<K, V, H> BorshSerialize for IndexMap<K, V, H>
where
K: BorshSerialize,
V: BorshSerialize,
H: BuildHasher,
{
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
check_zst::<K>()?;

let iterator = self.iter();

u32::try_from(iterator.len())
.map_err(|_| ErrorKind::InvalidData)?
.serialize(writer)?;

for (key, value) in iterator {
key.serialize(writer)?;
value.serialize(writer)?;
}

Ok(())
}
}

impl<K, V, H> BorshDeserialize for IndexMap<K, V, H>
where
K: BorshDeserialize + Eq + Hash,
V: BorshDeserialize,
H: BuildHasher + Default,
{
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
check_zst::<K>()?;
let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
Ok(vec.into_iter().collect::<IndexMap<K, V, H>>())
}
}

impl<T, H> BorshSerialize for IndexSet<T, H>
where
T: BorshSerialize,
H: BuildHasher,
{
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
check_zst::<T>()?;

let iterator = self.iter();

u32::try_from(iterator.len())
.map_err(|_| ErrorKind::InvalidData)?
.serialize(writer)?;

for item in iterator {
item.serialize(writer)?;
}

Ok(())
}
}

impl<T, H> BorshDeserialize for IndexSet<T, H>
where
T: BorshDeserialize + Eq + Hash,
H: BuildHasher + Default,
{
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
check_zst::<T>()?;
let vec = <Vec<T>>::deserialize_reader(reader)?;
Ok(vec.into_iter().collect::<IndexSet<T, H>>())
}
}

fn check_zst<T>() -> Result<()> {
if size_of::<T>() == 0 {
return Err(Error::new(ErrorKind::InvalidData, ERROR_ZST_FORBIDDEN));
}
Ok(())
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
//! to [`IndexMap`] and [`IndexSet`]. Alternative implementations for
//! (de)serializing [`IndexMap`] as an ordered sequence are available in the
//! [`map::serde_seq`] module.
//! * `borsh`: Adds implementations for [`BorshSerialize`] and [`BorshDeserialize`]
//! to [`IndexMap`] and [`IndexSet`].
//! * `arbitrary`: Adds implementations for the [`arbitrary::Arbitrary`] trait
//! to [`IndexMap`] and [`IndexSet`].
//! * `quickcheck`: Adds implementations for the [`quickcheck::Arbitrary`] trait
Expand All @@ -46,6 +48,8 @@
//! [`no_std`]: #no-standard-library-targets
//! [`Serialize`]: `::serde::Serialize`
//! [`Deserialize`]: `::serde::Deserialize`
//! [`BorshSerialize`]: `::borsh::BorshSerialize`
//! [`BorshDeserialize`]: `::borsh::BorshDeserialize`
//! [`arbitrary::Arbitrary`]: `::arbitrary::Arbitrary`
//! [`quickcheck::Arbitrary`]: `::quickcheck::Arbitrary`
//!
Expand Down Expand Up @@ -110,6 +114,8 @@ use alloc::vec::{self, Vec};
mod arbitrary;
#[macro_use]
mod macros;
#[cfg(feature = "borsh")]
mod borsh;
mod mutable_keys;
#[cfg(feature = "serde")]
mod serde;
Expand Down

0 comments on commit 0804a16

Please sign in to comment.