diff --git a/src/rlp/faces.rs b/src/rlp/faces.rs index 69e39b06d..d72f2f96c 100644 --- a/src/rlp/faces.rs +++ b/src/rlp/faces.rs @@ -1,15 +1,43 @@ +use std::fmt; +use std::error::Error as StdError; +use bytes::FromBytesError; + +#[derive(Debug, PartialEq, Eq)] +pub enum DecoderError { + FromBytesError(FromBytesError), + RlpIsTooShort, + RlpExpectedToBeList, + RlpExpectedToBeData, +} + +impl StdError for DecoderError { + fn description(&self) -> &str { + "builder error" + } +} + +impl fmt::Display for DecoderError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self, f) + } +} + +impl From for DecoderError { + fn from(err: FromBytesError) -> DecoderError { + DecoderError::FromBytesError(err) + } +} + pub trait Decoder { - type Error; + fn read_value(&self, f: F) -> Result + where F: FnOnce(&[u8]) -> Result; - fn read_value(&self, f: F) -> Result - where F: FnOnce(&[u8]) -> Result; - - fn read_list(&self, f: F) -> Result - where F: FnOnce(&[Self]) -> Result; + fn read_list(&self, f: F) -> Result + where F: FnOnce(&[Self]) -> Result; } pub trait Decodable: Sized { - fn decode(decoder: &D) -> Result where D: Decoder; + fn decode(decoder: &D) -> Result where D: Decoder; } pub trait View<'a, 'view>: Sized { @@ -18,7 +46,6 @@ pub trait View<'a, 'view>: Sized { type Data; type Item; type Iter; - type Error; /// Creates a new instance of `Rlp` reader fn new(bytes: &'a [u8]) -> Self; @@ -179,7 +206,7 @@ pub trait View<'a, 'view>: Sized { /// ``` fn iter(&'view self) -> Self::Iter; - fn as_val(&self) -> Result where T: Decodable; + fn as_val(&self) -> Result where T: Decodable; } pub trait Encoder { diff --git a/src/rlp/mod.rs b/src/rlp/mod.rs index af6da14a2..98cb43e80 100644 --- a/src/rlp/mod.rs +++ b/src/rlp/mod.rs @@ -1,8 +1,40 @@ +//! Rlp serialization module +//! +//! Allows encoding, decoding, and view onto rlp-slice +//! +//!# What should you use when? +//! +//!### Use `encode` function when: +//! * You want to encode something inline. +//! * You do not work on big set of data. +//! * You want to encode whole data structure at once. +//! +//!### Use `decode` function when: +//! * You want to decode something inline. +//! * You do not work on big set of data. +//! * You want to decode whole rlp at once. +//! +//!### Use `RlpStream` when: +//! * You want to encode something in portions. +//! * You encode a big set of data. +//! +//!### Use `Rlp` when: +//! * You are working on trusted data (not corrupted). +//! * You want to get view onto rlp-slice. +//! * You don't want to decode whole rlp at once. +//! +//!### Use `UntrustedRlp` when: +//! * You are working on untrusted data (~corrupted). +//! * You need to handle data corruption errors. +//! * You are working on input data. +//! * You want to get view onto rlp-slice. +//! * You don't want to decode whole rlp at once. + pub mod old; pub mod faces; -pub mod coders; pub mod rlp; pub mod untrusted_rlp; +pub mod rlpstream; pub use self::old::*; diff --git a/src/rlp/rlp.rs b/src/rlp/rlp.rs index 54807bfda..2effb7297 100644 --- a/src/rlp/rlp.rs +++ b/src/rlp/rlp.rs @@ -1,4 +1,4 @@ -use super::faces::{View, Decodable}; +use super::faces::{View, Decodable, DecoderError}; use super::untrusted_rlp::*; impl<'a> From> for Rlp<'a> { @@ -22,7 +22,6 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view { type Data = &'a [u8]; type Item = Rlp<'a>; type Iter = RlpIterator<'a, 'view>; - type Error = DecoderError; /// Create a new instance of `Rlp` fn new(bytes: &'a [u8]) -> Rlp<'a> { @@ -83,14 +82,14 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view { self.into_iter() } - fn as_val(&self) -> Result where T: Decodable { + fn as_val(&self) -> Result where T: Decodable { self.rlp.as_val() } } impl <'a, 'view> Rlp<'a> where 'a: 'view { fn reader_as_val(r: &R) -> T where R: View<'a, 'view>, T: Decodable { - let res: Result = r.as_val(); + let res: Result = r.as_val(); res.unwrap_or_else(|_| panic!()) } diff --git a/src/rlp/untrusted_rlp.rs b/src/rlp/untrusted_rlp.rs index 910696565..89c3b664f 100644 --- a/src/rlp/untrusted_rlp.rs +++ b/src/rlp/untrusted_rlp.rs @@ -1,8 +1,6 @@ -use std::fmt; use std::cell::Cell; -use std::error::Error as StdError; -use bytes::{FromBytes, FromBytesError}; -use super::faces::{View, Decoder, Decodable}; +use bytes::{FromBytes}; +use super::faces::{View, Decoder, Decodable, DecoderError}; /// rlp offset #[derive(Copy, Clone, Debug)] @@ -42,31 +40,6 @@ impl PayloadInfo { } } -#[derive(Debug, PartialEq, Eq)] -pub enum DecoderError { - FromBytesError(FromBytesError), - RlpIsTooShort, - RlpExpectedToBeList, - RlpExpectedToBeData, -} -impl StdError for DecoderError { - fn description(&self) -> &str { - "builder error" - } -} - -impl fmt::Display for DecoderError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self, f) - } -} - -impl From for DecoderError { - fn from(err: FromBytesError) -> DecoderError { - DecoderError::FromBytesError(err) - } -} - /// Data-oriented view onto rlp-slice. /// /// This is immutable structere. No operations change it. @@ -94,7 +67,6 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view { type Data = Result<&'a [u8], DecoderError>; type Item = Result, DecoderError>; type Iter = UntrustedRlpIterator<'a, 'view>; - type Error = DecoderError; //returns new instance of `UntrustedRlp` fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> { @@ -200,7 +172,7 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view { self.into_iter() } - fn as_val(&self) -> Result where T: Decodable { + fn as_val(&self) -> Result where T: Decodable { // optimize, so it doesn't use clone (although This clone is cheap) T::decode(&BasicDecoder::new(self.clone())) } @@ -305,10 +277,8 @@ impl<'a> BasicDecoder<'a> { } impl<'a> Decoder for BasicDecoder<'a> { - type Error = DecoderError; - - fn read_value(&self, f: F) -> Result - where F: FnOnce(&[u8]) -> Result { + fn read_value(&self, f: F) -> Result + where F: FnOnce(&[u8]) -> Result { let bytes = self.rlp.raw(); @@ -331,8 +301,8 @@ impl<'a> Decoder for BasicDecoder<'a> { } } - fn read_list(&self, f: F) -> Result - where F: FnOnce(&[Self]) -> Result { + fn read_list(&self, f: F) -> Result + where F: FnOnce(&[Self]) -> Result { let v: Vec> = self.rlp.iter() .map(| i | BasicDecoder::new(i)) @@ -341,3 +311,20 @@ impl<'a> Decoder for BasicDecoder<'a> { } } +impl Decodable for T where T: FromBytes { + fn decode(decoder: &D) -> Result where D: Decoder { + unimplemented!() + } +} + +impl Decodable for Vec where T: Decodable { + fn decode(decoder: &D) -> Result where D: Decoder { + unimplemented!() + } +} + +impl Decodable for Vec { + fn decode(decoder: &D) -> Result where D: Decoder { + unimplemented!() + } +}