Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add borsh serialization support #313

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
features: rustc-rayon
- rust: stable
features: serde
- rust: stable
features: borsh
- rust: stable
features: std
- rust: beta
Expand Down Expand Up @@ -119,8 +121,10 @@ jobs:
with:
tool: cargo-hack
- run: cargo +nightly hack generate-lockfile --remove-dev-deps -Z direct-minimal-versions
- name: Build
run: cargo build --verbose --all-features
- name: Build (nightly)
run: cargo +nightly build --verbose --all-features
- name: Build (MSRV)
run: cargo build --verbose --features arbitrary,quickcheck,serde,rayon

# One job that "summarizes" the success state of this pipeline. This can then be added to branch
# protection, rather than having to add each job separately.
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ equivalent = { version = "1.0", default-features = false }
arbitrary = { version = "1.0", optional = true, default-features = false }
quickcheck = { version = "1.0", optional = true, default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
borsh = { version = "1.2", optional = true, default-features = false }
rayon = { version = "1.5.3", optional = true }

# Internal feature, only used when building as part of rustc,
Expand Down Expand Up @@ -54,7 +55,7 @@ no-dev-version = true
tag-name = "{{version}}"

[package.metadata.docs.rs]
features = ["arbitrary", "quickcheck", "serde", "rayon"]
features = ["arbitrary", "quickcheck", "serde", "borsh", "rayon"]
rustdoc-args = ["--cfg", "docsrs"]

[workspace]
Expand Down
123 changes: 123 additions & 0 deletions src/borsh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#![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, S> BorshSerialize for IndexMap<K, V, S>
where
K: BorshSerialize,
V: BorshSerialize,
{
#[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, S> BorshDeserialize for IndexMap<K, V, S>
where
K: BorshDeserialize + Eq + Hash,
V: BorshDeserialize,
S: BuildHasher + Default,
{
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
check_zst::<K>()?;
let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
sug0 marked this conversation as resolved.
Show resolved Hide resolved
Ok(vec.into_iter().collect::<IndexMap<K, V, S>>())
}
}

impl<T, S> BorshSerialize for IndexSet<T, S>
where
T: BorshSerialize,
{
#[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, S> BorshDeserialize for IndexSet<T, S>
where
T: BorshDeserialize + Eq + Hash,
S: 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, S>>())
}
}

fn check_zst<T>() -> Result<()> {
if size_of::<T>() == 0 {
return Err(Error::new(ErrorKind::InvalidData, ERROR_ZST_FORBIDDEN));
}
Ok(())
}

#[cfg(test)]
sug0 marked this conversation as resolved.
Show resolved Hide resolved
mod borsh_tests {
use super::*;

#[test]
fn map_borsh_roundtrip() {
let original_map: IndexMap<i32, i32> = {
let mut map = IndexMap::new();
map.insert(1, 2);
map.insert(3, 4);
map.insert(5, 6);
map
};
let serialized_map = borsh::to_vec(&original_map).unwrap();
let deserialized_map: IndexMap<i32, i32> =
BorshDeserialize::try_from_slice(&serialized_map).unwrap();
assert_eq!(original_map, deserialized_map);
}

#[test]
fn set_borsh_roundtrip() {
let original_map: IndexSet<i32> = [1, 2, 3, 4, 5, 6].into_iter().collect();
let serialized_map = borsh::to_vec(&original_map).unwrap();
let deserialized_map: IndexSet<i32> =
BorshDeserialize::try_from_slice(&serialized_map).unwrap();
assert_eq!(original_map, deserialized_map);
}
}
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