val_at rlp method

This commit is contained in:
debris 2015-12-08 14:44:22 +01:00
parent 1c58ba4764
commit b657a15100
3 changed files with 16 additions and 2 deletions

View File

@ -84,16 +84,24 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable {
self.rlp.as_val()
}
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: Decodable {
self.at(index).rlp.as_val()
}
}
impl <'a, 'view> Rlp<'a> where 'a: 'view {
fn reader_as_val<T, R>(r: &R) -> T where R: View<'a, 'view>, T: Decodable {
fn view_as_val<T, R>(r: &R) -> T where R: View<'a, 'view>, T: Decodable {
let res: Result<T, DecoderError> = r.as_val();
res.unwrap_or_else(|_| panic!())
}
pub fn as_val<T>(&self) -> T where T: Decodable {
Self::reader_as_val(self)
Self::view_as_val(self)
}
pub fn val_at<T>(&self, index: usize) -> T where T: Decodable {
Self::view_as_val(&self.at(index))
}
}

View File

@ -179,6 +179,8 @@ pub trait View<'a, 'view>: Sized {
fn iter(&'view self) -> Self::Iter;
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable;
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: Decodable;
}
pub trait Encoder {

View File

@ -176,6 +176,10 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
// optimize, so it doesn't use clone (although This clone is cheap)
T::decode(&BasicDecoder::new(self.clone()))
}
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: Decodable {
self.at(index).unwrap().as_val()
}
}
impl<'a> UntrustedRlp<'a> {