refector init

This commit is contained in:
debris
2015-12-07 16:32:06 +01:00
parent 8903480a44
commit 36bea4692e
7 changed files with 256 additions and 6 deletions

23
src/rlp/faces.rs Normal file
View File

@@ -0,0 +1,23 @@
pub trait Reader<'a, 'view>: Sized {
type Prototype;
type PayloadInfo;
type Data;
type Item;
fn new(bytes: &'a [u8]) -> Self;
fn raw(&'view self) -> &'a [u8];
fn prototype(&self) -> Self::Prototype;
fn payload_info(&self) -> Self::PayloadInfo;
fn data(&'view self) -> Self::Data;
fn item_count(&self) -> usize;
fn size(&self) -> usize;
fn at(&'view self, index: usize) -> Self::Item;
fn is_null(&self) -> bool;
fn is_empty(&self) -> bool;
fn is_list(&self) -> bool;
fn is_data(&self) -> bool;
fn is_int(&self) -> bool;
}
pub trait Stream {
}

7
src/rlp/mod.rs Normal file
View File

@@ -0,0 +1,7 @@
pub mod old;
pub mod faces;
pub mod rlp;
pub mod untrusted_rlp;
pub use self::old::*;

1457
src/rlp/old.rs Normal file

File diff suppressed because it is too large Load Diff

72
src/rlp/rlp.rs Normal file
View File

@@ -0,0 +1,72 @@
use super::faces::Reader;
use super::untrusted_rlp::*;
/// 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.
pub struct Rlp<'a> {
rlp: UntrustedRlp<'a>
}
impl<'a, 'view> Reader<'a, 'view> for Rlp<'a> where 'a: 'view {
type Prototype = Prototype;
type PayloadInfo = PayloadInfo;
type Data = &'a [u8];
type Item = Rlp<'a>;
/// Create a new instance of `Rlp`
fn new(bytes: &'a [u8]) -> Rlp<'a> {
Rlp {
rlp: UntrustedRlp::new(bytes)
}
}
fn raw(&'view self) -> &'a [u8] {
self.rlp.raw()
}
fn prototype(&self) -> Self::Prototype {
unimplemented!()
}
fn payload_info(&self) -> Self::PayloadInfo {
unimplemented!()
}
fn data(&'view self) -> Self::Data {
unimplemented!()
}
fn item_count(&self) -> usize {
unimplemented!()
}
fn size(&self) -> usize {
unimplemented!()
}
fn at(&'view self, index: usize) -> Self::Item {
unimplemented!()
}
fn is_null(&self) -> bool {
unimplemented!()
}
fn is_empty(&self) -> bool {
unimplemented!()
}
fn is_list(&self) -> bool {
unimplemented!()
}
fn is_data(&self) -> bool {
unimplemented!()
}
fn is_int(&self) -> bool {
unimplemented!()
}
}

0
src/rlp/rlpstream.rs Normal file
View File

143
src/rlp/untrusted_rlp.rs Normal file
View File

@@ -0,0 +1,143 @@
use std::fmt;
use std::cell::Cell;
use std::error::Error as StdError;
use bytes::{FromBytesError};
use super::faces::Reader;
/// rlp offset
#[derive(Copy, Clone, Debug)]
struct OffsetCache {
index: usize,
offset: usize,
}
impl OffsetCache {
fn new(index: usize, offset: usize) -> OffsetCache {
OffsetCache {
index: index,
offset: offset,
}
}
}
#[derive(Debug)]
pub enum Prototype {
Null,
Data(usize),
List(usize),
}
/// Stores basic information about item
pub struct PayloadInfo {
pub header_len: usize,
pub value_len: usize,
}
impl PayloadInfo {
fn new(header_len: usize, value_len: usize) -> PayloadInfo {
PayloadInfo {
header_len: header_len,
value_len: value_len,
}
}
}
#[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<FromBytesError> for DecoderError {
fn from(err: FromBytesError) -> DecoderError {
DecoderError::FromBytesError(err)
}
}
/// Data-oriented view onto rlp-slice.
///
/// This is immutable structere. No operations change it.
///
/// Should be used in places where, error handling is required,
/// eg. on input
#[derive(Debug)]
pub struct UntrustedRlp<'a> {
bytes: &'a [u8],
cache: Cell<OffsetCache>,
}
impl<'a, 'view> Reader<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
type Prototype = Result<Prototype, DecoderError>;
type PayloadInfo = Result<PayloadInfo, DecoderError>;
type Data = Result<&'a [u8], DecoderError>;
type Item = Result<UntrustedRlp<'a>, DecoderError>;
//returns new instance of `UntrustedRlp`
fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> {
UntrustedRlp {
bytes: bytes,
cache: Cell::new(OffsetCache::new(usize::max_value(), 0)),
}
}
fn raw(&'view self) -> &'a [u8] {
self.bytes
}
fn prototype(&self) -> Self::Prototype {
unimplemented!()
}
fn payload_info(&self) -> Self::PayloadInfo {
unimplemented!()
}
fn data(&'view self) -> Self::Data {
unimplemented!()
}
fn item_count(&self) -> usize {
unimplemented!()
}
fn size(&self) -> usize {
unimplemented!()
}
fn at(&'view self, index: usize) -> Self::Item {
unimplemented!()
}
fn is_null(&self) -> bool {
unimplemented!()
}
fn is_empty(&self) -> bool {
unimplemented!()
}
fn is_list(&self) -> bool {
unimplemented!()
}
fn is_data(&self) -> bool {
unimplemented!()
}
fn is_int(&self) -> bool {
unimplemented!()
}
}