decoding fixed sized arrays
This commit is contained in:
		
							parent
							
								
									d88c4db470
								
							
						
					
					
						commit
						de4a227a47
					
				@ -8,6 +8,7 @@ pub enum DecoderError {
 | 
				
			|||||||
	RlpIsTooShort,
 | 
						RlpIsTooShort,
 | 
				
			||||||
	RlpExpectedToBeList,
 | 
						RlpExpectedToBeList,
 | 
				
			||||||
	RlpExpectedToBeData,
 | 
						RlpExpectedToBeData,
 | 
				
			||||||
 | 
						RlpIncorrectListLen
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl StdError for DecoderError {
 | 
					impl StdError for DecoderError {
 | 
				
			||||||
 | 
				
			|||||||
@ -343,3 +343,11 @@ fn test_rlp_json() {
 | 
				
			|||||||
	});
 | 
						});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn test_decoding_array() {
 | 
				
			||||||
 | 
						let v = vec![5u16, 2u16];
 | 
				
			||||||
 | 
						let res = rlp::encode(&v);
 | 
				
			||||||
 | 
						let arr: [u16; 2] = rlp::decode(&res);
 | 
				
			||||||
 | 
						assert_eq!(arr[0], 5);
 | 
				
			||||||
 | 
						assert_eq!(arr[1], 2);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -352,3 +352,38 @@ impl<T> Decodable for Option<T> where T: Decodable {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! impl_array_decodable {
 | 
				
			||||||
 | 
						($index_type:ty, $len:expr ) => (
 | 
				
			||||||
 | 
							impl<T> Decodable for [T; $len] where T: Decodable {
 | 
				
			||||||
 | 
								fn decode<D>(decoder: &D) -> Result<Self, DecoderError>  where D: Decoder {
 | 
				
			||||||
 | 
									decoder.read_list(| decoders | {
 | 
				
			||||||
 | 
										let mut result: [T; $len] = unsafe { ::std::mem::uninitialized() };
 | 
				
			||||||
 | 
										if decoders.len() != $len {
 | 
				
			||||||
 | 
											return Err(DecoderError::RlpIncorrectListLen);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										
 | 
				
			||||||
 | 
										for i in 0..decoders.len() {
 | 
				
			||||||
 | 
											result[i] = try!(T::decode(&decoders[i]));
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										Ok(result)
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! impl_array_decodable_recursive {
 | 
				
			||||||
 | 
						($index_type:ty, ) => ();
 | 
				
			||||||
 | 
						($index_type:ty, $len:expr, $($more:expr,)*) => (
 | 
				
			||||||
 | 
							impl_array_decodable!($index_type, $len);
 | 
				
			||||||
 | 
							impl_array_decodable_recursive!($index_type, $($more,)*);
 | 
				
			||||||
 | 
						);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl_array_decodable_recursive!(
 | 
				
			||||||
 | 
						u8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
 | 
				
			||||||
 | 
						16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 | 
				
			||||||
 | 
						32, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224,
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user