openethereum/src/rlp/rlp.rs

130 lines
2.5 KiB
Rust
Raw Normal View History

2015-12-07 23:47:26 +01:00
use super::faces::{View, Decodable};
2015-12-07 16:32:06 +01:00
use super::untrusted_rlp::*;
2015-12-07 23:47:26 +01:00
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
Rlp { rlp: rlp }
}
}
2015-12-07 16:32:06 +01:00
/// Data-oriented view onto trusted rlp-slice.
///
/// Unlikely to `UntrustedRlp` doesn't bother you with error
/// handling. It assumes that you know what you are doing.
2015-12-07 23:47:26 +01:00
#[derive(Debug)]
2015-12-07 16:32:06 +01:00
pub struct Rlp<'a> {
rlp: UntrustedRlp<'a>
}
2015-12-07 23:47:26 +01:00
impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
2015-12-07 16:32:06 +01:00
type Prototype = Prototype;
type PayloadInfo = PayloadInfo;
type Data = &'a [u8];
type Item = Rlp<'a>;
2015-12-07 23:47:26 +01:00
type Iter = RlpIterator<'a, 'view>;
type Error = DecoderError;
2015-12-07 16:32:06 +01:00
/// Create a new instance of `Rlp`
fn new(bytes: &'a [u8]) -> Rlp<'a> {
Rlp {
rlp: UntrustedRlp::new(bytes)
}
}
fn raw(&'view self) -> &'a [u8] {
self.rlp.raw()
}
fn prototype(&self) -> Self::Prototype {
2015-12-07 23:47:26 +01:00
self.rlp.prototype().unwrap()
2015-12-07 16:32:06 +01:00
}
fn payload_info(&self) -> Self::PayloadInfo {
2015-12-07 23:47:26 +01:00
self.rlp.payload_info().unwrap()
2015-12-07 16:32:06 +01:00
}
fn data(&'view self) -> Self::Data {
2015-12-07 23:47:26 +01:00
self.rlp.data().unwrap()
2015-12-07 16:32:06 +01:00
}
fn item_count(&self) -> usize {
2015-12-07 23:47:26 +01:00
self.rlp.item_count()
2015-12-07 16:32:06 +01:00
}
fn size(&self) -> usize {
2015-12-07 23:47:26 +01:00
self.rlp.size()
2015-12-07 16:32:06 +01:00
}
fn at(&'view self, index: usize) -> Self::Item {
2015-12-07 23:47:26 +01:00
From::from(self.rlp.at(index).unwrap())
2015-12-07 16:32:06 +01:00
}
fn is_null(&self) -> bool {
2015-12-07 23:47:26 +01:00
self.rlp.is_null()
2015-12-07 16:32:06 +01:00
}
fn is_empty(&self) -> bool {
2015-12-07 23:47:26 +01:00
self.rlp.is_empty()
2015-12-07 16:32:06 +01:00
}
fn is_list(&self) -> bool {
2015-12-07 23:47:26 +01:00
self.rlp.is_list()
2015-12-07 16:32:06 +01:00
}
fn is_data(&self) -> bool {
2015-12-07 23:47:26 +01:00
self.rlp.is_data()
2015-12-07 16:32:06 +01:00
}
fn is_int(&self) -> bool {
2015-12-07 23:47:26 +01:00
self.rlp.is_int()
}
fn iter(&'view self) -> Self::Iter {
self.into_iter()
}
fn as_val<T>(&self) -> Result<T, Self::Error> where T: Decodable {
self.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 {
let res: Result<T, R::Error> = r.as_val();
res.unwrap_or_else(|_| panic!())
}
pub fn as_val<T>(&self) -> T where T: Decodable {
Self::reader_as_val(self)
}
}
/// Iterator over trusted rlp-slice list elements.
pub struct RlpIterator<'a, 'view> where 'a: 'view {
rlp: &'view Rlp<'a>,
index: usize
}
impl<'a, 'view> IntoIterator for &'view Rlp<'a> where 'a: 'view {
type Item = Rlp<'a>;
type IntoIter = RlpIterator<'a, 'view>;
fn into_iter(self) -> Self::IntoIter {
RlpIterator {
rlp: self,
index: 0,
}
}
}
impl<'a, 'view> Iterator for RlpIterator<'a, 'view> {
type Item = Rlp<'a>;
fn next(&mut self) -> Option<Rlp<'a>> {
let index = self.index;
let result = self.rlp.rlp.at(index).ok().map(| iter | { From::from(iter) });
self.index += 1;
result
2015-12-07 16:32:06 +01:00
}
}