| 1 |
#![cfg_attr(docsrs, doc(cfg(feature = "borsh")))] |
| 2 |
|
| 3 |
use alloc::vec::Vec; |
| 4 |
use core::hash::BuildHasher; |
| 5 |
use core::hash::Hash; |
| 6 |
|
| 7 |
use borsh::error::ERROR_ZST_FORBIDDEN; |
| 8 |
use borsh::io::{Error, ErrorKind, Read, Result, Write}; |
| 9 |
use borsh::{BorshDeserialize, BorshSerialize}; |
| 10 |
|
| 11 |
use crate::map::IndexMap; |
| 12 |
use crate::set::IndexSet; |
| 13 |
|
| 14 |
// NOTE: the real `#[deprecated]` attribute doesn't work for trait implementations, |
| 15 |
// but we can get close by mimicking the message style for documentation. |
| 16 |
/// <div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: use borsh's <code>indexmap</code> feature instead.</span></div> |
| 17 |
impl<K, V, S> BorshSerialize for IndexMap<K, V, S> |
| 18 |
where |
| 19 |
K: BorshSerialize, |
| 20 |
V: BorshSerialize, |
| 21 |
{ |
| 22 |
#[inline] |
| 23 |
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
| 24 |
check_zst::<K>()?; |
| 25 |
|
| 26 |
let iterator = self.iter(); |
| 27 |
|
| 28 |
u32::try_from(iterator.len()) |
| 29 |
.map_err(|_| ErrorKind::InvalidData)? |
| 30 |
.serialize(writer)?; |
| 31 |
|
| 32 |
for (key, value) in iterator { |
| 33 |
key.serialize(writer)?; |
| 34 |
value.serialize(writer)?; |
| 35 |
} |
| 36 |
|
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// <div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: use borsh's <code>indexmap</code> feature instead.</span></div> |
| 42 |
impl<K, V, S> BorshDeserialize for IndexMap<K, V, S> |
| 43 |
where |
| 44 |
K: BorshDeserialize + Eq + Hash, |
| 45 |
V: BorshDeserialize, |
| 46 |
S: BuildHasher + Default, |
| 47 |
{ |
| 48 |
#[inline] |
| 49 |
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> { |
| 50 |
check_zst::<K>()?; |
| 51 |
let vec = <Vec<(K, V)>>::deserialize_reader(reader)?; |
| 52 |
Ok(vec.into_iter().collect::<IndexMap<K, V, S>>()) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// <div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: use borsh's <code>indexmap</code> feature instead.</span></div> |
| 57 |
impl<T, S> BorshSerialize for IndexSet<T, S> |
| 58 |
where |
| 59 |
T: BorshSerialize, |
| 60 |
{ |
| 61 |
#[inline] |
| 62 |
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
| 63 |
check_zst::<T>()?; |
| 64 |
|
| 65 |
let iterator = self.iter(); |
| 66 |
|
| 67 |
u32::try_from(iterator.len()) |
| 68 |
.map_err(|_| ErrorKind::InvalidData)? |
| 69 |
.serialize(writer)?; |
| 70 |
|
| 71 |
for item in iterator { |
| 72 |
item.serialize(writer)?; |
| 73 |
} |
| 74 |
|
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/// <div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: use borsh's <code>indexmap</code> feature instead.</span></div> |
| 80 |
impl<T, S> BorshDeserialize for IndexSet<T, S> |
| 81 |
where |
| 82 |
T: BorshDeserialize + Eq + Hash, |
| 83 |
S: BuildHasher + Default, |
| 84 |
{ |
| 85 |
#[inline] |
| 86 |
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> { |
| 87 |
check_zst::<T>()?; |
| 88 |
let vec = <Vec<T>>::deserialize_reader(reader)?; |
| 89 |
Ok(vec.into_iter().collect::<IndexSet<T, S>>()) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
fn check_zst<T>() -> Result<()> { |
| 94 |
if size_of::<T>() == 0 { |
| 95 |
return Err(Error::new(ErrorKind::InvalidData, ERROR_ZST_FORBIDDEN)); |
| 96 |
} |
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(test)] |
| 101 |
mod borsh_tests { |
| 102 |
use super::*; |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn map_borsh_roundtrip() { |
| 106 |
let original_map: IndexMap<i32, i32> = { |
| 107 |
let mut map = IndexMap::new(); |
| 108 |
map.insert(1, 2); |
| 109 |
map.insert(3, 4); |
| 110 |
map.insert(5, 6); |
| 111 |
map |
| 112 |
}; |
| 113 |
let serialized_map = borsh::to_vec(&original_map).unwrap(); |
| 114 |
let deserialized_map: IndexMap<i32, i32> = |
| 115 |
BorshDeserialize::try_from_slice(&serialized_map).unwrap(); |
| 116 |
assert_eq!(original_map, deserialized_map); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn set_borsh_roundtrip() { |
| 121 |
let original_map: IndexSet<i32> = [1, 2, 3, 4, 5, 6].into_iter().collect(); |
| 122 |
let serialized_map = borsh::to_vec(&original_map).unwrap(); |
| 123 |
let deserialized_map: IndexSet<i32> = |
| 124 |
BorshDeserialize::try_from_slice(&serialized_map).unwrap(); |
| 125 |
assert_eq!(original_map, deserialized_map); |
| 126 |
} |
| 127 |
} |