Skip to content

Commit 32f13fa

Browse files
Seulgi Kimsgkim126
authored andcommitted
Implement Encodable/Decodable for Tuple with three items
1 parent 8648ca6 commit 32f13fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

util/rlp/src/impls.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,25 @@ impl Decodable for String {
320320
}
321321
}
322322

323+
impl<T1: Encodable, T2: Encodable, T3: Encodable> Encodable for (T1, T2, T3) {
324+
fn rlp_append(&self, s: &mut RlpStream) {
325+
s.begin_list(3).append(&self.0).append(&self.1).append(&self.2);
326+
}
327+
}
328+
329+
impl<T1: Decodable, T2: Decodable, T3: Decodable> Decodable for (T1, T2, T3) {
330+
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
331+
let item_count = rlp.item_count()?;
332+
if item_count != 3 {
333+
return Err(DecoderError::RlpIncorrectListLen {
334+
expected: 3,
335+
got: item_count,
336+
})
337+
}
338+
Ok((rlp.val_at(0)?, rlp.val_at(1)?, rlp.val_at(2)?))
339+
}
340+
}
341+
323342
#[macro_export]
324343
macro_rules! rlp_encode_and_decode_test {
325344
($origin:expr) => {
@@ -426,4 +445,10 @@ mod tests {
426445
stream.begin_list(0);
427446
assert_eq!(vec![0xC0], stream.out());
428447
}
448+
449+
#[test]
450+
fn tuple() {
451+
let tuple: (u32, u32, u32) = (1, 2, 3);
452+
rlp_encode_and_decode_test!(tuple);
453+
}
429454
}

0 commit comments

Comments
 (0)