2015-12-26 15:48:41 +01:00
|
|
|
use rlp::{DecoderError, UntrustedRlp};
|
2015-12-08 00:07:07 +01:00
|
|
|
|
2015-12-14 12:03:48 +01:00
|
|
|
pub trait Decoder: Sized {
|
2015-12-08 00:07:07 +01:00
|
|
|
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
|
|
|
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
2015-12-14 12:03:48 +01:00
|
|
|
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
|
2015-12-26 15:48:41 +01:00
|
|
|
fn as_rlp<'a>(&'a self) -> &'a UntrustedRlp<'a>;
|
2016-01-07 23:59:50 +01:00
|
|
|
fn as_raw(&self) -> &[u8];
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Decodable: Sized {
|
2015-12-08 00:27:12 +01:00
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder;
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait View<'a, 'view>: Sized {
|
2015-12-07 16:32:06 +01:00
|
|
|
type Prototype;
|
|
|
|
type PayloadInfo;
|
|
|
|
type Data;
|
|
|
|
type Item;
|
2015-12-07 23:47:26 +01:00
|
|
|
type Iter;
|
2015-12-07 16:32:06 +01:00
|
|
|
|
2015-12-07 23:47:26 +01:00
|
|
|
/// Creates a new instance of `Rlp` reader
|
2015-12-07 16:32:06 +01:00
|
|
|
fn new(bytes: &'a [u8]) -> Self;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// The raw data of the RLP.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
2016-01-08 15:52:43 +01:00
|
|
|
/// let dog = rlp.at(1).as_raw();
|
2015-12-07 23:47:26 +01:00
|
|
|
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-01-08 15:52:43 +01:00
|
|
|
fn as_raw(&'view self) -> &'a [u8];
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Get the prototype of the RLP.
|
2015-12-07 16:32:06 +01:00
|
|
|
fn prototype(&self) -> Self::Prototype;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
2015-12-07 16:32:06 +01:00
|
|
|
fn payload_info(&self) -> Self::PayloadInfo;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
2015-12-07 16:32:06 +01:00
|
|
|
fn data(&'view self) -> Self::Data;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Returns number of RLP items.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
|
|
|
/// assert_eq!(rlp.item_count(), 2);
|
|
|
|
/// let view = rlp.at(1);
|
|
|
|
/// assert_eq!(view.item_count(), 0);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn item_count(&self) -> usize;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Returns the number of bytes in the data, or zero if it isn't data.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
|
|
|
/// assert_eq!(rlp.size(), 0);
|
|
|
|
/// let view = rlp.at(1);
|
|
|
|
/// assert_eq!(view.size(), 3);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn size(&self) -> usize;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Get view onto RLP-slice at index.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// Caches offset to given index, so access to successive
|
|
|
|
/// slices is faster.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
2015-12-08 12:40:11 +01:00
|
|
|
/// let dog: String = rlp.at(1).as_val();
|
2015-12-07 23:47:26 +01:00
|
|
|
/// assert_eq!(dog, "dog".to_string());
|
|
|
|
/// }
|
2015-12-07 16:32:06 +01:00
|
|
|
fn at(&'view self, index: usize) -> Self::Item;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// No value
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_null());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn is_null(&self) -> bool;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Contains a zero-length string or zero-length list.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc0];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_empty());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn is_empty(&self) -> bool;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// List value
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
|
|
|
/// assert!(rlp.is_list());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn is_list(&self) -> bool;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// String value
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
|
|
|
/// assert!(rlp.at(1).is_data());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn is_data(&self) -> bool;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Int value
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc1, 0x10];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert_eq!(rlp.is_int(), false);
|
|
|
|
/// assert_eq!(rlp.at(0).is_int(), true);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-07 16:32:06 +01:00
|
|
|
fn is_int(&self) -> bool;
|
2015-12-07 23:47:26 +01:00
|
|
|
|
|
|
|
/// Get iterator over rlp-slices
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-07 23:47:26 +01:00
|
|
|
/// 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);
|
2015-12-08 12:40:11 +01:00
|
|
|
/// let strings: Vec<String> = rlp.iter().map(| i | i.as_val()).collect();
|
2015-12-07 23:47:26 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn iter(&'view self) -> Self::Iter;
|
|
|
|
|
2015-12-08 00:07:07 +01:00
|
|
|
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable;
|
2015-12-08 14:44:22 +01:00
|
|
|
|
|
|
|
fn val_at<T>(&self, index: usize) -> Result<T, DecoderError> where T: Decodable;
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Encoder {
|
|
|
|
fn emit_value(&mut self, bytes: &[u8]) -> ();
|
|
|
|
fn emit_list<F>(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> ();
|
2016-01-07 23:59:50 +01:00
|
|
|
fn emit_raw(&mut self, bytes: &[u8]) -> ();
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Encodable {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder;
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2015-12-07 23:47:26 +01:00
|
|
|
pub trait Stream: Sized {
|
2015-12-08 12:33:05 +01:00
|
|
|
|
|
|
|
/// Initializes instance of empty `Stream`.
|
2015-12-07 23:47:26 +01:00
|
|
|
fn new() -> Self;
|
2015-12-08 12:33:05 +01:00
|
|
|
|
|
|
|
/// Initializes the `Stream` as a list.
|
2015-12-07 23:47:26 +01:00
|
|
|
fn new_list(len: usize) -> Self;
|
2015-12-08 12:33:05 +01:00
|
|
|
|
|
|
|
/// Apends value to the end of stream, chainable.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append(&"cat").append(&"dog");
|
|
|
|
/// let out = stream.out();
|
|
|
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn append<'a, E>(&'a mut self, object: &E) -> &'a mut Self where E: Encodable;
|
|
|
|
|
|
|
|
/// Declare appending the list of given size, chainable.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append_list(2).append(&"cat").append(&"dog");
|
2015-12-26 15:48:41 +01:00
|
|
|
/// stream.append(&"");
|
2015-12-08 12:33:05 +01:00
|
|
|
/// 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]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn append_list<'a>(&'a mut self, len: usize) -> &'a mut Self;
|
|
|
|
|
|
|
|
/// Apends null to the end of stream, chainable.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append_empty_data().append_empty_data();
|
|
|
|
/// let out = stream.out();
|
|
|
|
/// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn append_empty_data<'a>(&'a mut self) -> &'a mut Self;
|
|
|
|
|
|
|
|
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
|
|
|
|
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut Self;
|
|
|
|
|
|
|
|
/// Clear the output stream so far.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(3);
|
|
|
|
/// stream.append(&"cat");
|
|
|
|
/// stream.clear();
|
|
|
|
/// stream.append(&"dog");
|
|
|
|
/// let out = stream.out();
|
|
|
|
/// assert_eq!(out, vec![0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
fn clear(&mut self);
|
|
|
|
|
|
|
|
/// Returns true if stream doesnt expect any more items.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append(&"cat");
|
|
|
|
/// assert_eq!(stream.is_finished(), false);
|
|
|
|
/// stream.append(&"dog");
|
|
|
|
/// assert_eq!(stream.is_finished(), true);
|
|
|
|
/// let out = stream.out();
|
|
|
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
fn is_finished(&self) -> bool;
|
|
|
|
|
2016-01-08 15:57:50 +01:00
|
|
|
fn as_raw(&self) -> &[u8];
|
2015-12-12 15:57:08 +01:00
|
|
|
|
2015-12-08 12:33:05 +01:00
|
|
|
/// Streams out encoded bytes.
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// panic! if stream is not finished.
|
|
|
|
fn out(self) -> Vec<u8>;
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|