append Now accepts lists again
This commit is contained in:
@@ -268,7 +268,6 @@ macro_rules! impl_map_to_bytes {
|
||||
}
|
||||
|
||||
impl_map_to_bytes!(usize, u64);
|
||||
impl_map_to_bytes!(u8, u64);
|
||||
impl_map_to_bytes!(u16, u64);
|
||||
impl_map_to_bytes!(u32, u64);
|
||||
|
||||
|
||||
@@ -86,8 +86,8 @@ impl JournalDB {
|
||||
|
||||
let mut r = RlpStream::new_list(3);
|
||||
r.append(id);
|
||||
r.append_list(&self.inserts);
|
||||
r.append_list(&self.removes);
|
||||
r.append(&self.inserts);
|
||||
r.append(&self.removes);
|
||||
try!(self.backing.put(&last, r.as_raw()));
|
||||
self.inserts.clear();
|
||||
self.removes.clear();
|
||||
|
||||
@@ -202,7 +202,7 @@ impl Session {
|
||||
rlp.begin_list(5)
|
||||
.append(&host.protocol_version)
|
||||
.append(&host.client_version)
|
||||
.append_list(&host.capabilities)
|
||||
.append(&host.capabilities)
|
||||
.append(&host.listen_port)
|
||||
.append(host.id());
|
||||
self.connection.send_packet(&rlp.out())
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
//! * You want to get view onto rlp-slice.
|
||||
//! * You don't want to decode whole rlp at once.
|
||||
|
||||
use std::ops::Deref;
|
||||
/// TODO [Gav Wood] Please document me
|
||||
pub mod rlptraits;
|
||||
/// TODO [Gav Wood] Please document me
|
||||
@@ -46,7 +45,7 @@ pub mod rlpstream;
|
||||
mod tests;
|
||||
|
||||
pub use self::rlperrors::DecoderError;
|
||||
pub use self::rlptraits::{Decoder, Decodable, View, Stream, Encodable, Encoder};
|
||||
pub use self::rlptraits::{Decoder, Decodable, View, Stream, Encodable, Encoder, RlpEncodable};
|
||||
pub use self::untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, Prototype};
|
||||
pub use self::rlpin::{Rlp, RlpIterator};
|
||||
pub use self::rlpstream::{RlpStream};
|
||||
@@ -91,26 +90,8 @@ pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
|
||||
/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: Encodable {
|
||||
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: RlpEncodable {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append(object);
|
||||
stream.drain()
|
||||
}
|
||||
|
||||
/// Shortcut function to encode a list to rlp.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let animals = vec!["cat", "dog"];
|
||||
/// let out = encode_list(&animals).to_vec();
|
||||
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn encode_list<I, E>(list: &I) -> ElasticArray1024<u8> where I: Deref<Target = [E]>, E: Encodable {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.append_list(list);
|
||||
stream.drain()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::ops::Deref;
|
||||
use elastic_array::*;
|
||||
use bytes::{ToBytes, VecLike};
|
||||
use rlp::{Stream, Encoder, Encodable};
|
||||
use rlp::rlptraits::ByteEncodable;
|
||||
use rlp::rlptraits::{ByteEncodable, RlpEncodable};
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct ListInfo {
|
||||
@@ -43,7 +43,7 @@ impl Stream for RlpStream {
|
||||
stream
|
||||
}
|
||||
|
||||
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: Encodable {
|
||||
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable {
|
||||
self.finished_list = false;
|
||||
value.rlp_append(self);
|
||||
if !self.finished_list {
|
||||
@@ -70,16 +70,6 @@ impl Stream for RlpStream {
|
||||
self
|
||||
}
|
||||
|
||||
fn append_list<I, E>(&mut self, list: &I) -> &mut Self where I: Deref<Target = [E]>, E: Encodable {
|
||||
let items = list.deref();
|
||||
self.begin_list(items.len());
|
||||
for el in items.iter() {
|
||||
el.rlp_append(self);
|
||||
}
|
||||
self.note_appended(items.len());
|
||||
self
|
||||
}
|
||||
|
||||
fn append_empty_data(&mut self) -> &mut RlpStream {
|
||||
// self push raw item
|
||||
self.encoder.bytes.push(0x80);
|
||||
@@ -134,6 +124,15 @@ impl RlpStream {
|
||||
self.encoder.emit_value(object);
|
||||
}
|
||||
|
||||
fn append_internal<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: Encodable {
|
||||
self.finished_list = false;
|
||||
value.rlp_append(self);
|
||||
if !self.finished_list {
|
||||
self.note_appended(1);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Try to finish lists
|
||||
fn note_appended(&mut self, inserted_items: usize) -> () {
|
||||
if self.unfinished_lists.len() == 0 {
|
||||
@@ -234,7 +233,6 @@ impl Encoder for BasicEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T> ByteEncodable for T where T: ToBytes {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
ToBytes::to_bytes(self, out)
|
||||
@@ -245,23 +243,27 @@ impl<T> ByteEncodable for T where T: ToBytes {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ByteEncodable for &'a[u8] {
|
||||
struct U8Slice<'a>(&'a [u8]);
|
||||
|
||||
impl<'a> ByteEncodable for U8Slice<'a> {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self)
|
||||
out.vec_extend(self.0)
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize {
|
||||
self.len()
|
||||
self.0.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl ByteEncodable for Vec<u8> {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self.deref())
|
||||
impl<'a> Encodable for &'a[u8] {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&U8Slice(self))
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize {
|
||||
self.len()
|
||||
impl Encodable for Vec<u8> {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&U8Slice(self.deref()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,3 +272,41 @@ impl<T> Encodable for T where T: ByteEncodable {
|
||||
s.append_value(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct EncodableU8 (u8);
|
||||
|
||||
impl ByteEncodable for EncodableU8 {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_push(self.0)
|
||||
}
|
||||
|
||||
fn bytes_len(&self) -> usize { 1 }
|
||||
}
|
||||
|
||||
impl RlpEncodable for u8 {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_value(&EncodableU8(*self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Encodable for &'a[T] where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.begin_list(self.len());
|
||||
for el in self.iter() {
|
||||
s.append_internal(el);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodable for Vec<T> where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
Encodable::rlp_append(&self.deref(), s);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RlpEncodable for T where T: Encodable {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
Encodable::rlp_append(self, s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -223,7 +223,7 @@ pub trait ByteEncodable {
|
||||
fn bytes_len(&self) -> usize;
|
||||
}
|
||||
|
||||
/// Structure encodable to RLP
|
||||
/// Structure encodable to RLP. Implement this trait for
|
||||
pub trait Encodable {
|
||||
/// Append a value to the stream
|
||||
fn rlp_append(&self, s: &mut RlpStream);
|
||||
@@ -239,6 +239,12 @@ pub trait Encodable {
|
||||
fn rlp_sha3(&self) -> H256 { self.rlp_bytes().deref().sha3() }
|
||||
}
|
||||
|
||||
/// Encodable wrapper trait required to handle special case of encoding a &[u8] as string and not as list
|
||||
pub trait RlpEncodable {
|
||||
/// Append a value to the stream
|
||||
fn rlp_append(&self, s: &mut RlpStream);
|
||||
}
|
||||
|
||||
/// TODO [debris] Please document me
|
||||
pub trait Stream: Sized {
|
||||
|
||||
@@ -261,7 +267,7 @@ pub trait Stream: Sized {
|
||||
/// 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, value: &E) -> &'a mut Self where E: Encodable;
|
||||
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable;
|
||||
|
||||
/// Declare appending the list of given size, chainable.
|
||||
///
|
||||
@@ -279,21 +285,6 @@ pub trait Stream: Sized {
|
||||
/// ```
|
||||
fn begin_list(&mut self, len: usize) -> &mut Self;
|
||||
|
||||
/// Append the given list, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::rlp::*;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut stream = RlpStream::new_list(1);
|
||||
/// stream.append_list(&vec!["cat", "dog"]);
|
||||
/// let out = stream.out().to_vec();
|
||||
/// assert_eq!(out, vec![0xc9, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||
/// }
|
||||
/// ```
|
||||
fn append_list<I, E>(&mut self, list: &I) -> &mut Self where I: Deref<Target = [E]>, E: Encodable;
|
||||
|
||||
/// Apends null to the end of stream, chainable.
|
||||
///
|
||||
/// ```rust
|
||||
|
||||
@@ -77,7 +77,6 @@ fn rlp_iter() {
|
||||
}
|
||||
|
||||
struct ETestPair<T>(T, Vec<u8>) where T: rlp::Encodable;
|
||||
struct ETestVecPair<T>(Vec<T>, Vec<u8>) where T: rlp::Encodable;
|
||||
|
||||
fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
||||
where T: rlp::Encodable
|
||||
@@ -88,15 +87,6 @@ fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
||||
}
|
||||
}
|
||||
|
||||
fn run_encode_list_tests<T>(tests: Vec<ETestVecPair<T>>)
|
||||
where T: rlp::Encodable
|
||||
{
|
||||
for t in &tests {
|
||||
let res = rlp::encode_list(&t.0);
|
||||
assert_eq!(&res[..], &t.1[..]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u16() {
|
||||
let tests = vec![
|
||||
@@ -188,19 +178,19 @@ fn encode_vector_u8() {
|
||||
#[test]
|
||||
fn encode_vector_u64() {
|
||||
let tests = vec![
|
||||
ETestVecPair(vec![], vec![0xc0]),
|
||||
ETestVecPair(vec![15u64], vec![0xc1, 0x0f]),
|
||||
ETestVecPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
ETestVecPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
ETestPair(vec![], vec![0xc0]),
|
||||
ETestPair(vec![15u64], vec![0xc1, 0x0f]),
|
||||
ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
ETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
||||
];
|
||||
run_encode_list_tests(tests);
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_vector_str() {
|
||||
let tests = vec![ETestVecPair(vec!["cat", "dog"],
|
||||
let tests = vec![ETestPair(vec!["cat", "dog"],
|
||||
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
||||
run_encode_list_tests(tests);
|
||||
run_encode_tests(tests);
|
||||
}
|
||||
|
||||
struct DTestPair<T>(T, Vec<u8>) where T: rlp::Decodable + fmt::Debug + cmp::Eq;
|
||||
@@ -227,9 +217,8 @@ fn decode_vector_u8() {
|
||||
#[test]
|
||||
fn decode_untrusted_u16() {
|
||||
let tests = vec![
|
||||
DTestPair(0u16, vec![0u8]),
|
||||
DTestPair(0x100, vec![0x82, 0x01, 0x00]),
|
||||
DTestPair(0xffff, vec![0x82, 0xff, 0xff]),
|
||||
DTestPair(0x100u16, vec![0x82, 0x01, 0x00]),
|
||||
DTestPair(0xffffu16, vec![0x82, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
@@ -237,9 +226,8 @@ fn decode_untrusted_u16() {
|
||||
#[test]
|
||||
fn decode_untrusted_u32() {
|
||||
let tests = vec![
|
||||
DTestPair(0u32, vec![0u8]),
|
||||
DTestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]),
|
||||
DTestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]),
|
||||
DTestPair(0x10000u32, vec![0x83, 0x01, 0x00, 0x00]),
|
||||
DTestPair(0xffffffu32, vec![0x83, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
@@ -247,9 +235,8 @@ fn decode_untrusted_u32() {
|
||||
#[test]
|
||||
fn decode_untrusted_u64() {
|
||||
let tests = vec![
|
||||
DTestPair(0u64, vec![0u8]),
|
||||
DTestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
DTestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
DTestPair(0x1000000u64, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
||||
DTestPair(0xFFFFFFFFu64, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
||||
];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
@@ -350,7 +337,7 @@ fn test_rlp_json() {
|
||||
#[test]
|
||||
fn test_decoding_array() {
|
||||
let v = vec![5u16, 2u16];
|
||||
let res = rlp::encode_list(&v);
|
||||
let res = rlp::encode(&v);
|
||||
let arr: [u16; 2] = rlp::decode(&res);
|
||||
assert_eq!(arr[0], 5);
|
||||
assert_eq!(arr[1], 2);
|
||||
|
||||
Reference in New Issue
Block a user