openethereum/src/rlp/faces.rs

199 lines
4.6 KiB
Rust
Raw Normal View History

2015-12-07 23:47:26 +01:00
pub trait Decoder {
type Error;
fn read_value<T, F>(&self, f: F) -> Result<T, Self::Error>
where F: FnOnce(&[u8]) -> Result<T, Self::Error>;
fn read_list<T, F>(&self, f: F) -> Result<T, Self::Error>
where F: FnOnce(&[Self]) -> Result<T, Self::Error>;
}
pub trait Decodable: Sized {
fn decode<T, D>(decoder: &D) -> Result<T, D::Error> where D: Decoder;
}
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;
type Error;
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.
///
/// ```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);
/// let dog = rlp.at(1).raw();
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
/// }
/// ```
2015-12-07 16:32:06 +01:00
fn 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.
///
/// ```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);
/// 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.
///
/// ```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);
/// 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.
///
/// 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);
/// let dog = String::decode(&rlp.at(1));
/// 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
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
/// 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.
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
/// 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
///
/// ```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);
/// 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
///
/// ```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);
/// 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
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::rlp::*;
///
/// 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
///
/// ```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);
/// let strings: Vec<String> = rlp.iter().map(| i | String::decode(&i)).collect();
/// }
/// ```
fn iter(&'view self) -> Self::Iter;
fn as_val<T>(&self) -> Result<T, Self::Error> where T: Decodable;
}
pub trait Encoder {
fn emit_value(&mut self, bytes: &[u8]) -> ();
fn emit_list<F>(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> ();
}
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 {
fn new() -> Self;
fn new_list(len: usize) -> Self;
fn append<'a, E>(&'a mut self, object: &E) -> &'a mut Self;
2015-12-07 16:32:06 +01:00
}