2015-12-08 12:59:19 +01:00
|
|
|
use rlp::{View, Decodable, DecoderError, UntrustedRlp, PayloadInfo, Prototype};
|
2015-12-07 16:32:06 +01:00
|
|
|
|
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>;
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:52:43 +01:00
|
|
|
fn as_raw(&'view self) -> &'a [u8] {
|
|
|
|
self.rlp.as_raw()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-12-08 00:07:07 +01:00
|
|
|
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.as_val()
|
|
|
|
}
|
2015-12-08 14:44:22 +01:00
|
|
|
|
|
|
|
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: Decodable {
|
|
|
|
self.at(index).rlp.as_val()
|
|
|
|
}
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a, 'view> Rlp<'a> where 'a: 'view {
|
2015-12-08 14:44:22 +01:00
|
|
|
fn view_as_val<T, R>(r: &R) -> T where R: View<'a, 'view>, T: Decodable {
|
2015-12-08 00:07:07 +01:00
|
|
|
let res: Result<T, DecoderError> = r.as_val();
|
2015-12-07 23:47:26 +01:00
|
|
|
res.unwrap_or_else(|_| panic!())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_val<T>(&self) -> T where T: Decodable {
|
2015-12-08 14:44:22 +01:00
|
|
|
Self::view_as_val(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn val_at<T>(&self, index: usize) -> T where T: Decodable {
|
|
|
|
Self::view_as_val(&self.at(index))
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
}
|