rlp uses elastic array

This commit is contained in:
debris 2015-12-07 11:27:12 +01:00
parent 182f356467
commit 8cf437b349
3 changed files with 17 additions and 14 deletions

View File

@ -19,6 +19,7 @@ rocksdb = "0.2.1"
lazy_static = "0.1.*"
secp256k1 = "0.5.1"
rust-crypto = "0.2.34"
elastic-array = "0.4"
[dev-dependencies]
json-tests = { path = "json-tests" }

View File

@ -42,6 +42,7 @@ extern crate time;
extern crate crypto as rcrypto;
extern crate secp256k1;
extern crate arrayvec;
extern crate elastic_array;
pub mod macros;
pub mod error;

View File

@ -33,6 +33,7 @@
use std::fmt;
use std::cell::Cell;
use std::error::Error as StdError;
use elastic_array::*;
use bytes::{ToBytes, FromBytes, FromBytesError};
use vector::InsertSlice;
@ -758,7 +759,7 @@ impl Decoder for BasicDecoder {
}
}
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
struct ListInfo {
position: usize,
current: usize,
@ -777,7 +778,7 @@ impl ListInfo {
/// Appendable rlp encoder.
pub struct RlpStream {
unfinished_lists: Vec<ListInfo>,
unfinished_lists: ElasticArray16<ListInfo>,
encoder: BasicEncoder,
}
@ -785,7 +786,7 @@ impl RlpStream {
/// Initializes instance of empty `RlpStream`.
pub fn new() -> RlpStream {
RlpStream {
unfinished_lists: vec![],
unfinished_lists: ElasticArray16::new(),
encoder: BasicEncoder::new(),
}
}
@ -846,7 +847,7 @@ impl RlpStream {
},
_ => {
// reserve at least double size of the len
self.encoder.bytes.reserve(len * 2);
//self.encoder.bytes.reserve(len * 2);
self.unfinished_lists.push(ListInfo::new(position, len));
},
}
@ -882,7 +883,7 @@ impl RlpStream {
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
pub fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream {
// push raw items
self.encoder.bytes.extend(bytes);
self.encoder.bytes.append_slice(bytes);
// try to finish and prepend the length
self.note_appended(item_count);
@ -937,7 +938,7 @@ impl RlpStream {
/// panic! if stream is not finished.
pub fn out(self) -> Vec<u8> {
match self.is_finished() {
true => self.encoder.out(),
true => self.encoder.out().to_vec(),
false => panic!()
}
}
@ -985,7 +986,7 @@ pub fn encode<E>(object: &E) -> Vec<u8> where E: Encodable
{
let mut encoder = BasicEncoder::new();
object.encode(&mut encoder);
encoder.out()
encoder.out().to_vec()
}
pub trait Encodable {
@ -1038,12 +1039,12 @@ impl Encodable for Vec<u8> {
}
struct BasicEncoder {
bytes: Vec<u8>,
bytes: ElasticArray1024<u8>,
}
impl BasicEncoder {
fn new() -> BasicEncoder {
BasicEncoder { bytes: vec![] }
BasicEncoder { bytes: ElasticArray1024::new() }
}
/// inserts list prefix at given position
@ -1062,7 +1063,7 @@ impl BasicEncoder {
}
/// get encoded value
fn out(self) -> Vec<u8> {
fn out(self) -> ElasticArray1024<u8> {
self.bytes
}
}
@ -1073,17 +1074,17 @@ impl Encoder for BasicEncoder {
// just 0
0 => self.bytes.push(0x80u8),
// byte is its own encoding
1 if bytes[0] < 0x80 => self.bytes.extend(bytes),
1 if bytes[0] < 0x80 => self.bytes.append_slice(bytes),
// (prefix + length), followed by the string
len @ 1 ... 55 => {
self.bytes.push(0x80u8 + len as u8);
self.bytes.extend(bytes);
self.bytes.append_slice(bytes);
}
// (prefix + length of length), followed by the length, followd by the string
len => {
self.bytes.push(0xb7 + len.to_bytes_len() as u8);
self.bytes.extend(len.to_bytes());
self.bytes.extend(bytes);
self.bytes.append_slice(&len.to_bytes());
self.bytes.append_slice(bytes);
}
}
}