diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 3f14d07f..452a2708 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -29,8 +29,6 @@ //! big-endian decimals, etc.) //! -use std::collections::HashMap; -use std::hash::Hash; use std::{mem, u32}; use std::error; @@ -489,7 +487,9 @@ impl Decodable for bool { impl Encodable for String { #[inline] fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { - self.as_bytes().consensus_encode(s) + let b = self.as_bytes(); + VarInt(b.len() as u64).consensus_encode(s)?; + s.emit_slice(&b) } } @@ -550,7 +550,8 @@ impl Encodable for [u16; 8] { } } -impl> Encodable for [T] { +// Vectors +impl> Encodable for Vec { #[inline] fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { VarInt(self.len() as u64).consensus_encode(s)?; @@ -559,14 +560,6 @@ impl> Encodable for [T] { } } -// Cannot decode a slice - -// Vectors -impl> Encodable for Vec { - #[inline] - fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (&self[..]).consensus_encode(s) } -} - impl> Decodable for Vec { #[inline] fn consensus_decode(d: &mut D) -> Result, self::Error> { @@ -671,54 +664,6 @@ tuple_encode!(T0, T1, T2, T3); tuple_encode!(T0, T1, T2, T3, T4, T5); tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7); -// References -impl> Encodable for Box { - #[inline] - fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (**self).consensus_encode(s) } -} - -impl> Decodable for Box { - #[inline] - fn consensus_decode(d: &mut D) -> Result, self::Error> { - Decodable::consensus_decode(d).map(Box::new) - } -} - -// HashMap -impl Encodable for HashMap - where S: Encoder, - K: Encodable + Eq + Hash, - V: Encodable -{ - #[inline] - fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { - VarInt(self.len() as u64).consensus_encode(s)?; - for (key, value) in self.iter() { - key.consensus_encode(s)?; - value.consensus_encode(s)?; - } - Ok(()) - } -} - -impl Decodable for HashMap - where D: Decoder, - K: Decodable + Eq + Hash, - V: Decodable -{ - #[inline] - fn consensus_decode(d: &mut D) -> Result, self::Error> { - let len = VarInt::consensus_decode(d)?.0; - - let mut ret = HashMap::with_capacity(len as usize); - for _ in 0..len { - ret.insert(Decodable::consensus_decode(d)?, - Decodable::consensus_decode(d)?); - } - Ok(ret) - } -} - impl Encodable for sha256d::Hash { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { self.into_inner().consensus_encode(s) @@ -837,7 +782,6 @@ mod tests { #[test] fn serialize_vector_test() { assert_eq!(serialize(&vec![1u8, 2, 3]), vec![3u8, 1, 2, 3]); - assert_eq!(serialize(&[1u8, 2, 3][..]), vec![3u8, 1, 2, 3]); // TODO: test vectors of more interesting objects } @@ -846,13 +790,6 @@ mod tests { assert_eq!(serialize(&"Andrew".to_string()), vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]); } - #[test] - fn serialize_box_test() { - assert_eq!(serialize(&Box::new(1u8)), vec![1u8]); - assert_eq!(serialize(&Box::new(1u16)), vec![1u8, 0]); - assert_eq!(serialize(&Box::new(1u64)), vec![1u8, 0, 0, 0, 0, 0, 0, 0]); - } - #[test] fn deserialize_int_test() { // bool @@ -913,13 +850,5 @@ mod tests { let cd: Result = deserialize(&[5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]); assert_eq!(cd.ok(), Some(CheckedData(vec![1u8, 2, 3, 4, 5]))); } - - #[test] - fn deserialize_box_test() { - let zero: Result, _> = deserialize(&[0u8]); - let one: Result, _> = deserialize(&[1u8]); - assert_eq!(zero.ok(), Some(Box::new(0))); - assert_eq!(one.ok(), Some(Box::new(1))); - } }