decoder refactor applied

This commit is contained in:
debris 2015-12-08 00:45:04 +01:00
parent cb5ec8c0af
commit 4419924154
4 changed files with 31 additions and 17 deletions

View File

@ -122,7 +122,7 @@ impl OverlayDB {
.expect("Low-level database error. Some issue with your hard disk?") .expect("Low-level database error. Some issue with your hard disk?")
.map(|d| { .map(|d| {
let r = Rlp::new(d.deref()); let r = Rlp::new(d.deref());
(Bytes::decode(&r.at(1)), u32::decode(&r.at(0))) (r.at(1).as_val(), r.at(0).as_val())
}) })
} }

View File

@ -37,4 +37,14 @@ pub mod rlp;
pub mod untrusted_rlp; pub mod untrusted_rlp;
pub mod rlpstream; pub mod rlpstream;
pub use self::old::*; pub use self::faces::{DecoderError, Decoder, Decodable, View};
pub use self::rlp::*;
pub use self::untrusted_rlp::*;
pub use self::old::{encode, RlpStream, Encodable};
//pub use self::old::*;
pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
let rlp = Rlp::new(bytes);
rlp.as_val()
}

View File

@ -1119,7 +1119,7 @@ mod tests {
use std::{fmt, cmp}; use std::{fmt, cmp};
use std::str::FromStr; use std::str::FromStr;
use rlp; use rlp;
use rlp::{UntrustedRlp, RlpStream, Decodable}; use rlp::{UntrustedRlp, RlpStream, Decodable, View};
use uint::U256; use uint::U256;
#[test] #[test]
@ -1128,23 +1128,27 @@ mod tests {
{ {
let rlp = UntrustedRlp::new(&data); let rlp = UntrustedRlp::new(&data);
assert!(rlp.is_list()); assert!(rlp.is_list());
let animals = <Vec<String> as rlp::Decodable>::decode_untrusted(&rlp).unwrap(); //let animals = <Vec<String> as rlp::Decodable>::decode_untrusted(&rlp).unwrap();
let animals: Vec<String> = rlp.as_val().unwrap();
assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]); assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]);
let cat = rlp.at(0).unwrap(); let cat = rlp.at(0).unwrap();
assert!(cat.is_data()); assert!(cat.is_data());
assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); assert_eq!(cat.raw(), &[0x83, b'c', b'a', b't']);
assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_string()); //assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_string());
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_string());
let dog = rlp.at(1).unwrap(); let dog = rlp.at(1).unwrap();
assert!(dog.is_data()); assert!(dog.is_data());
assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); assert_eq!(dog.raw(), &[0x83, b'd', b'o', b'g']);
assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_string()); //assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_string());
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_string());
let cat_again = rlp.at(0).unwrap(); let cat_again = rlp.at(0).unwrap();
assert!(cat_again.is_data()); assert!(cat_again.is_data());
assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); assert_eq!(cat_again.raw(), &[0x83, b'c', b'a', b't']);
assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_string()); //assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_string());
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_string());
} }
} }
@ -1172,18 +1176,18 @@ mod tests {
let cat = iter.next().unwrap(); let cat = iter.next().unwrap();
assert!(cat.is_data()); assert!(cat.is_data());
assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); assert_eq!(cat.raw(), &[0x83, b'c', b'a', b't']);
let dog = iter.next().unwrap(); let dog = iter.next().unwrap();
assert!(dog.is_data()); assert!(dog.is_data());
assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); assert_eq!(dog.raw(), &[0x83, b'd', b'o', b'g']);
let none = iter.next(); let none = iter.next();
assert!(none.is_none()); assert!(none.is_none());
let cat_again = rlp.at(0).unwrap(); let cat_again = rlp.at(0).unwrap();
assert!(cat_again.is_data()); assert!(cat_again.is_data());
assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); assert_eq!(cat_again.raw(), &[0x83, b'c', b'a', b't']);
} }
} }

View File

@ -131,7 +131,7 @@ impl Diff {
fn delete_node(&mut self, old: &Rlp) { fn delete_node(&mut self, old: &Rlp) {
if old.is_data() && old.size() == 32 { if old.is_data() && old.size() == 32 {
self.delete_node_sha3(H256::decode(old)); self.delete_node_sha3(old.as_val());
} }
} }
@ -310,7 +310,7 @@ impl TrieDB {
let mut handle_payload = |payload| { let mut handle_payload = |payload| {
let p = Rlp::new(payload); let p = Rlp::new(payload);
if p.is_data() && p.size() == 32 { if p.is_data() && p.size() == 32 {
acc.push(H256::decode(&p)); acc.push(p.as_val());
} }
self.accumulate_keys(self.get_node(payload), acc); self.accumulate_keys(self.get_node(payload), acc);
@ -417,7 +417,7 @@ impl TrieDB {
// check if its sha3 + len // check if its sha3 + len
let r = Rlp::new(node); let r = Rlp::new(node);
match r.is_data() && r.size() == 32 { match r.is_data() && r.size() == 32 {
true => self.db.lookup(&H256::decode(&r)).expect("Not found!"), true => self.db.lookup(&r.as_val::<H256>()).expect("Not found!"),
false => node false => node
} }
} }
@ -495,7 +495,7 @@ impl TrieDB {
rlp.raw() rlp.raw()
} }
else if rlp.is_data() && rlp.size() == 32 { else if rlp.is_data() && rlp.size() == 32 {
let h = H256::decode(rlp); let h = rlp.as_val();
let r = self.db.lookup(&h).unwrap_or_else(||{ let r = self.db.lookup(&h).unwrap_or_else(||{
println!("Node not found! rlp={:?}, node_hash={:?}", rlp.raw().pretty(), h); println!("Node not found! rlp={:?}, node_hash={:?}", rlp.raw().pretty(), h);
println!("Diff: {:?}", diff); println!("Diff: {:?}", diff);