minor fixes

This commit is contained in:
arkpar
2015-12-26 15:48:41 +01:00
parent 416c2ec3e4
commit fa1b74fa53
7 changed files with 103 additions and 57 deletions

View File

@@ -1,10 +1,10 @@
//! Rlp serialization module
//!
//! Allows encoding, decoding, and view onto rlp-slice
//! Rlp serialization module
//!
//! Allows encoding, decoding, and view onto rlp-slice
//!
//!# What should you use when?
//!
//!### Use `encode` function 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.
@@ -23,7 +23,7 @@
//! * You want to get view onto rlp-slice.
//! * You don't want to decode whole rlp at once.
//!
//!### Use `UntrustedRlp` when:
//!### Use `UntrustedRlp` when:
//! * You are working on untrusted data (~corrupted).
//! * You need to handle data corruption errors.
//! * You are working on input data.
@@ -47,14 +47,16 @@ pub use self::rlpstream::{RlpStream};
use super::hash::H256;
pub const NULL_RLP: [u8; 1] = [0x80; 1];
pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] );
/// Shortcut function to decode trusted rlp
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let animals: Vec<String> = decode(&data);
@@ -71,7 +73,7 @@ pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let animals = vec!["cat", "dog"];
/// let out = encode(&animals);

View File

@@ -1,10 +1,11 @@
use rlp::DecoderError;
use rlp::{DecoderError, UntrustedRlp};
pub trait Decoder: Sized {
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
fn as_rlp<'a>(&'a self) -> &'a UntrustedRlp<'a>;
}
pub trait Decodable: Sized {
@@ -22,11 +23,11 @@ pub trait View<'a, 'view>: Sized {
fn new(bytes: &'a [u8]) -> Self;
/// The raw data of the RLP.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -34,7 +35,7 @@ pub trait View<'a, 'view>: Sized {
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
/// }
/// ```
fn raw(&'view self) -> &'a [u8];
fn raw(&'view self) -> &'a [u8];
/// Get the prototype of the RLP.
fn prototype(&self) -> Self::Prototype;
@@ -44,11 +45,11 @@ pub trait View<'a, 'view>: Sized {
fn data(&'view self) -> Self::Data;
/// Returns number of RLP items.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -60,11 +61,11 @@ pub trait View<'a, 'view>: Sized {
fn item_count(&self) -> usize;
/// Returns the number of bytes in the data, or zero if it isn't data.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -76,14 +77,14 @@ pub trait View<'a, 'view>: Sized {
fn size(&self) -> usize;
/// Get view onto RLP-slice at index.
///
///
/// Caches offset to given index, so access to successive
/// slices is faster.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -93,11 +94,11 @@ pub trait View<'a, 'view>: Sized {
fn at(&'view self, index: usize) -> Self::Item;
/// No value
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![];
/// let rlp = Rlp::new(&data);
@@ -107,11 +108,11 @@ pub trait View<'a, 'view>: Sized {
fn is_null(&self) -> bool;
/// Contains a zero-length string or zero-length list.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc0];
/// let rlp = Rlp::new(&data);
@@ -121,11 +122,11 @@ pub trait View<'a, 'view>: Sized {
fn is_empty(&self) -> bool;
/// List value
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -135,11 +136,11 @@ pub trait View<'a, 'view>: Sized {
fn is_list(&self) -> bool;
/// String value
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -149,11 +150,11 @@ pub trait View<'a, 'view>: Sized {
fn is_data(&self) -> bool;
/// Int value
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc1, 0x10];
/// let rlp = Rlp::new(&data);
@@ -164,11 +165,11 @@ pub trait View<'a, 'view>: Sized {
fn is_int(&self) -> bool;
/// Get iterator over rlp-slices
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data);
@@ -204,7 +205,7 @@ pub trait Stream: Sized {
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let mut stream = RlpStream::new_list(2);
/// stream.append(&"cat").append(&"dog");
@@ -219,11 +220,11 @@ pub trait Stream: Sized {
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let mut stream = RlpStream::new_list(2);
/// stream.append_list(2).append(&"cat").append(&"dog");
/// stream.append(&"");
/// stream.append(&"");
/// let out = stream.out();
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
/// }
@@ -235,7 +236,7 @@ pub trait Stream: Sized {
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let mut stream = RlpStream::new_list(2);
/// stream.append_empty_data().append_empty_data();
@@ -249,11 +250,11 @@ pub trait Stream: Sized {
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut Self;
/// Clear the output stream so far.
///
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let mut stream = RlpStream::new_list(3);
/// stream.append(&"cat");
@@ -269,7 +270,7 @@ pub trait Stream: Sized {
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
///
/// fn main () {
/// let mut stream = RlpStream::new_list(2);
/// stream.append(&"cat");
@@ -284,7 +285,7 @@ pub trait Stream: Sized {
fn raw(&self) -> &[u8];
/// Streams out encoded bytes.
///
///
/// panic! if stream is not finished.
fn out(self) -> Vec<u8>;
}

View File

@@ -321,6 +321,10 @@ impl<'a> Decoder for BasicDecoder<'a> {
.collect();
Ok(v)
}
fn as_rlp<'s>(&'s self) -> &'s UntrustedRlp<'s> {
&self.rlp
}
}
impl<T> Decodable for T where T: FromBytes {