1use super::{U256, U128};
11use rlp::{RlpStream, Encodable, Decodable, DecoderError, UntrustedRlp};
12
13macro_rules! impl_encodable_for_uint {
14 ($name: ident, $size: expr) => {
15 impl Encodable for $name {
16 fn rlp_append(&self, s: &mut RlpStream) {
17 let leading_empty_bytes = $size - (self.bits() + 7) / 8;
18 let mut buffer = [0u8; $size];
19 self.to_big_endian(&mut buffer);
20 s.encoder().encode_value(&buffer[leading_empty_bytes..]);
21 }
22 }
23 }
24}
25
26macro_rules! impl_decodable_for_uint {
27 ($name: ident, $size: expr) => {
28 impl Decodable for $name {
29 fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
30 rlp.decoder().decode_value(|bytes| {
31 if !bytes.is_empty() && bytes[0] == 0 {
32 Err(DecoderError::RlpInvalidIndirection)
33 } else if bytes.len() <= $size {
34 Ok($name::from(bytes))
35 } else {
36 Err(DecoderError::RlpIsTooBig)
37 }
38 })
39 }
40 }
41 }
42}
43
44impl_encodable_for_uint!(U256, 32);
45impl_encodable_for_uint!(U128, 16);
46
47impl_decodable_for_uint!(U256, 32);
48impl_decodable_for_uint!(U128, 16);