Skip to content

Commit 804631a

Browse files
committed
use map_decode
1 parent 2788bb1 commit 804631a

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/encoding.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ impl CompactEncoding for Node {
2222
where
2323
Self: Sized,
2424
{
25-
let (index, rest) = u64::decode(buffer)?;
26-
let (length, rest) = u64::decode(rest)?;
27-
let (hash, rest) = <[u8; 32]>::decode(rest)?;
28-
25+
let ((index, length, hash), rest) = map_decode!(buffer, [u64, u64, [u8; 32]]);
2926
Ok((Node::new(index, hash.to_vec(), length), rest))
3027
}
3128
}
@@ -56,8 +53,7 @@ impl CompactEncoding for RequestBlock {
5653
where
5754
Self: Sized,
5855
{
59-
let (index, rest) = u64::decode(buffer)?;
60-
let (nodes, rest) = u64::decode(rest)?;
56+
let ((index, nodes), rest) = map_decode!(buffer, [u64, u64]);
6157
Ok((RequestBlock { index, nodes }, rest))
6258
}
6359
}
@@ -93,8 +89,7 @@ impl CompactEncoding for RequestUpgrade {
9389
where
9490
Self: Sized,
9591
{
96-
let (start, rest) = u64::decode(buffer)?;
97-
let (length, rest) = u64::decode(rest)?;
92+
let ((start, length), rest) = map_decode!(buffer, [u64, u64]);
9893
Ok((RequestUpgrade { start, length }, rest))
9994
}
10095
}
@@ -112,9 +107,7 @@ impl CompactEncoding for DataBlock {
112107
where
113108
Self: Sized,
114109
{
115-
let (index, rest) = u64::decode(buffer)?;
116-
let (value, rest) = Vec::<u8>::decode(rest)?;
117-
let (nodes, rest) = Vec::<Node>::decode(rest)?;
110+
let ((index, value, nodes), rest) = map_decode!(buffer, [u64, Vec<u8>, Vec<Node>]);
118111
Ok((
119112
DataBlock {
120113
index,
@@ -139,8 +132,7 @@ impl CompactEncoding for DataHash {
139132
where
140133
Self: Sized,
141134
{
142-
let (index, rest) = u64::decode(buffer)?;
143-
let (nodes, rest) = Vec::<Node>::decode(rest)?;
135+
let ((index, nodes), rest) = map_decode!(buffer, [u64, Vec<Node>]);
144136
Ok((DataHash { index, nodes }, rest))
145137
}
146138
}
@@ -158,8 +150,7 @@ impl CompactEncoding for DataSeek {
158150
where
159151
Self: Sized,
160152
{
161-
let (bytes, rest) = u64::decode(buffer)?;
162-
let (nodes, rest) = Vec::<Node>::decode(rest)?;
153+
let ((bytes, nodes), rest) = map_decode!(buffer, [u64, Vec<Node>]);
163154
Ok((DataSeek { bytes, nodes }, rest))
164155
}
165156
}
@@ -192,11 +183,8 @@ impl CompactEncoding for DataUpgrade {
192183
where
193184
Self: Sized,
194185
{
195-
let (start, rest) = u64::decode(buffer)?;
196-
let (length, rest) = u64::decode(rest)?;
197-
let (nodes, rest) = Vec::<Node>::decode(rest)?;
198-
let (additional_nodes, rest) = Vec::<Node>::decode(rest)?;
199-
let (signature, rest) = <Vec<u8>>::decode(rest)?;
186+
let ((start, length, nodes, additional_nodes, signature), rest) =
187+
map_decode!(buffer, [u64, u64, Vec<Node>, Vec<Node>, Vec<u8>]);
200188
Ok((
201189
DataUpgrade {
202190
start,

0 commit comments

Comments
 (0)