Merge pull request #17 from gavofyork/rlp_array
decoding to rust array [T; $len] using macro
This commit is contained in:
commit
c7768e110e
@ -8,6 +8,7 @@ pub enum DecoderError {
|
||||
RlpIsTooShort,
|
||||
RlpExpectedToBeList,
|
||||
RlpExpectedToBeData,
|
||||
RlpIncorrectListLen
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
use rlp::DecoderError;
|
||||
|
||||
pub trait Decoder {
|
||||
pub trait Decoder: Sized {
|
||||
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
||||
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[Self]) -> Result<T, DecoderError>;
|
||||
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
|
||||
}
|
||||
|
||||
pub trait Decodable: Sized {
|
||||
|
@ -305,13 +305,11 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[Self]) -> Result<T, DecoderError> {
|
||||
|
||||
fn as_list(&self) -> Result<Vec<Self>, DecoderError> {
|
||||
let v: Vec<BasicDecoder<'a>> = self.rlp.iter()
|
||||
.map(| i | BasicDecoder::new(i))
|
||||
.collect();
|
||||
f(&v)
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,9 +323,8 @@ impl<T> Decodable for T where T: FromBytes {
|
||||
|
||||
impl<T> Decodable for Vec<T> where T: Decodable {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
decoder.read_list(| decoders | {
|
||||
decoders.iter().map(|d| T::decode(d)).collect()
|
||||
})
|
||||
let decoders = try!(decoder.as_list());
|
||||
decoders.iter().map(|d| T::decode(d)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
@ -352,3 +349,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 {
|
||||
let decoders = try!(decoder.as_list());
|
||||
|
||||
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