Debian Codegraph @

third_party/rust/indexmap/src/borsh.rs

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