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

View File

@ -91,6 +91,6 @@ fn bench_stream_1000_empty_lists(b: &mut Bencher) {
for _ in 0..1000 {
stream.append_list(0);
}
let _ = stream.out();
//let _ = stream.out();
});
}

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::*;

View File

@ -32,10 +32,10 @@
use std::fmt;
use std::cell::Cell;
use std::ops::Deref;
use std::error::Error as StdError;
use elastic_array::*;
use bytes::{ToBytes, FromBytes, FromBytesError};
use vector::InsertSlice;
/// Data-oriented view onto rlp-slice.
///
@ -113,6 +113,14 @@ pub struct Rlp<'a> {
rlp: UntrustedRlp<'a>
}
//impl<'a> Deref for Rlp<'a> {
//type Target = UntrustedRlp<'a>;
//fn deref(&self) -> &Self::Target {
//&self.rlp
//}
//}
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
Rlp { rlp: rlp }
@ -837,8 +845,6 @@ impl RlpStream {
/// }
/// ```
pub fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream {
// push new list
let position = self.encoder.bytes.len();
match len {
0 => {
// we may finish, if the appended list len is equal 0
@ -846,8 +852,7 @@ impl RlpStream {
self.note_appended(1);
},
_ => {
// reserve at least double size of the len
//self.encoder.bytes.reserve(len * 2);
let position = self.encoder.bytes.len();
self.unfinished_lists.push(ListInfo::new(position, len));
},
}

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!()
}
}