Skip to content

Commit f1188c7

Browse files
committed
remove decode macro
this came from hypercore but has been removed
1 parent 2f6f694 commit f1188c7

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/schema.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use compact_encoding::{
2-
map_encode, sum_encoded_size, take_array, take_array_mut, write_array, write_slice,
2+
map_decode, map_encode, sum_encoded_size, take_array, take_array_mut, write_array, write_slice,
33
CompactEncoding, EncodingError,
44
};
55
use hypercore::{
6-
decode, DataBlock, DataHash, DataSeek, DataUpgrade, Proof, RequestBlock, RequestSeek,
7-
RequestUpgrade,
6+
DataBlock, DataHash, DataSeek, DataUpgrade, Proof, RequestBlock, RequestSeek, RequestUpgrade,
87
};
98
use tracing::instrument;
109

@@ -50,9 +49,8 @@ impl CompactEncoding for Open {
5049
where
5150
Self: Sized,
5251
{
53-
let (channel, rest) = u64::decode(buffer)?;
54-
let (protocol, rest) = String::decode(rest)?;
55-
let (discovery_key, rest) = <Vec<u8>>::decode(rest)?;
52+
let ((channel, protocol, discovery_key), rest) =
53+
map_decode!(buffer, [u64, String, Vec<u8>]);
5654
// TODO this is a CLEAR bug it assumes nothing is encoded after this message
5755
let (capability, rest) = if !rest.is_empty() {
5856
let (_, rest) = take_array::<1>(rest)?;
@@ -62,7 +60,7 @@ impl CompactEncoding for Open {
6260
(None, rest)
6361
};
6462
Ok((
65-
Open {
63+
Self {
6664
channel,
6765
protocol,
6866
discovery_key,
@@ -93,7 +91,8 @@ impl CompactEncoding for Close {
9391
where
9492
Self: Sized,
9593
{
96-
decode!(Close, buffer, {channel: u64})
94+
let (channel, rest) = u64::decode(buffer)?;
95+
Ok((Self { channel }, rest))
9796
}
9897
}
9998

@@ -138,10 +137,7 @@ impl CompactEncoding for Synchronize {
138137
Self: Sized,
139138
{
140139
let ([flags], rest) = take_array::<1>(buffer)?;
141-
dbg!(flags);
142-
let (fork, rest) = u64::decode(rest)?;
143-
let (length, rest) = u64::decode(rest)?;
144-
let (remote_length, rest) = u64::decode(rest)?;
140+
let ((fork, length, remote_length), rest) = map_decode!(rest, [u64, u64, u64]);
145141
let can_upgrade = flags & 1 != 0;
146142
let uploading = flags & 2 != 0;
147143
let downloading = flags & 4 != 0;
@@ -234,8 +230,7 @@ impl CompactEncoding for Request {
234230
Self: Sized,
235231
{
236232
let ([flags], rest) = take_array::<1>(buffer)?;
237-
let (id, rest) = u64::decode(rest)?;
238-
let (fork, rest) = u64::decode(rest)?;
233+
let ((id, fork), rest) = map_decode!(rest, [u64, u64]);
239234

240235
let (block, rest) = maybe_decode!(flags & 1 != 0, RequestBlock, rest);
241236
let (hash, rest) = maybe_decode!(flags & 2 != 0, RequestBlock, rest);
@@ -345,8 +340,7 @@ impl CompactEncoding for Data {
345340
Self: Sized,
346341
{
347342
let ([flags], rest) = take_array::<1>(buffer)?;
348-
let (request, rest) = u64::decode(rest)?;
349-
let (fork, rest) = u64::decode(rest)?;
343+
let ((request, fork), rest) = map_decode!(rest, [u64, u64]);
350344
let (block, rest) = maybe_decode!(flags & 1 != 0, DataBlock, rest);
351345
let (hash, rest) = maybe_decode!(flags & 2 != 0, DataHash, rest);
352346
let (seek, rest) = maybe_decode!(flags & 4 != 0, DataSeek, rest);
@@ -398,7 +392,8 @@ impl CompactEncoding for NoData {
398392
where
399393
Self: Sized,
400394
{
401-
decode!(NoData, buffer, { request: u64 })
395+
let (request, rest) = u64::decode(buffer)?;
396+
Ok((Self { request }, rest))
402397
}
403398
}
404399

@@ -424,7 +419,8 @@ impl CompactEncoding for Want {
424419
where
425420
Self: Sized,
426421
{
427-
decode!(Self, buffer, { start: u64, length: u64 })
422+
let ((start, length), rest) = map_decode!(buffer, [u64, u64]);
423+
Ok((Self { start, length }, rest))
428424
}
429425
}
430426

@@ -450,7 +446,8 @@ impl CompactEncoding for Unwant {
450446
where
451447
Self: Sized,
452448
{
453-
decode!(Self, buffer, { start: u64, length: u64 })
449+
let ((start, length), rest) = map_decode!(buffer, [u64, u64]);
450+
Ok((Self { start, length }, rest))
454451
}
455452
}
456453

@@ -475,7 +472,8 @@ impl CompactEncoding for Bitfield {
475472
where
476473
Self: Sized,
477474
{
478-
decode!(Self, buffer, { start: u64, bitfield: Vec<u32> })
475+
let ((start, bitfield), rest) = map_decode!(buffer, [u64, Vec<u32>]);
476+
Ok((Self { start, bitfield }, rest))
479477
}
480478
}
481479

@@ -556,6 +554,7 @@ impl CompactEncoding for Extension {
556554
where
557555
Self: Sized,
558556
{
559-
decode!(Self, buffer, { name: String, message: Vec<u8> })
557+
let ((name, message), rest) = map_decode!(buffer, [String, Vec<u8>]);
558+
Ok((Self { name, message }, rest))
560559
}
561560
}

0 commit comments

Comments
 (0)