read_list -> as_list

This commit is contained in:
debris 2015-12-14 12:03:48 +01:00
parent de4a227a47
commit 9231bc6622
2 changed files with 17 additions and 21 deletions

View File

@ -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 {

View File

@ -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 | {
let decoders = try!(decoder.as_list());
decoders.iter().map(|d| T::decode(d)).collect()
})
}
}
@ -357,7 +354,8 @@ 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 decoders = try!(decoder.as_list());
let mut result: [T; $len] = unsafe { ::std::mem::uninitialized() };
if decoders.len() != $len {
return Err(DecoderError::RlpIncorrectListLen);
@ -368,7 +366,6 @@ macro_rules! impl_array_decodable {
}
Ok(result)
})
}
}
)