diff --git a/util/src/bytes.rs b/util/src/bytes.rs index 3f637abaf..6db5a8cf2 100644 --- a/util/src/bytes.rs +++ b/util/src/bytes.rs @@ -14,9 +14,10 @@ //! fn to_bytes() { //! use util::bytes::ToBytes; //! -//! let a: Vec = "hello_world".to_bytes(); -//! let b: Vec = 400u32.to_bytes(); -//! let c: Vec = 0xffffffffffffffffu64.to_bytes(); +//! let mut out: Vec = Vec::new(); +//! "hello_world".to_bytes(&mut out); +//! 400u32.to_bytes(&mut out); +//! 0xffffffffffffffffu64.to_bytes(&mut out); //! } //! //! fn from_bytes() { @@ -47,20 +48,29 @@ use elastic_array::*; /// Vector like object pub trait VecLike { /// Add an element to the collection - fn push(&mut self, value: T); + fn vec_push(&mut self, value: T); /// Add a slice to the collection - fn extend(&mut self, slice: &[T]); + fn vec_extend(&mut self, slice: &[T]); } +impl VecLike for Vec where T: Copy { + fn vec_push(&mut self, value: T) { + Vec::::push(self, value) + } + + fn vec_extend(&mut self, slice: &[T]) { + Vec::::extend_from_slice(self, slice) + } +} macro_rules! impl_veclike_for_elastic_array { ($from: ident) => { impl VecLike for $from where T: Copy { - fn push(&mut self, value: T) { + fn vec_push(&mut self, value: T) { $from::::push(self, value) } - fn extend(&mut self, slice: &[T]) { + fn vec_extend(&mut self, slice: &[T]) { $from::::append_slice(self, slice) } @@ -207,7 +217,7 @@ pub trait ToBytes { impl <'a> ToBytes for &'a str { fn to_bytes>(&self, out: &mut V) { - out.extend(self.as_bytes()); + out.vec_extend(self.as_bytes()); } fn to_bytes_len(&self) -> usize { @@ -217,7 +227,7 @@ impl <'a> ToBytes for &'a str { impl ToBytes for String { fn to_bytes>(&self, out: &mut V) { - out.extend(self.as_bytes()); + out.vec_extend(self.as_bytes()); } fn to_bytes_len(&self) -> usize { @@ -230,7 +240,7 @@ impl ToBytes for u64 { let count = self.to_bytes_len(); for i in 0..count { let j = count - 1 - i; - out.push((*self >> (j * 8)) as u8); + out.vec_push((*self >> (j * 8)) as u8); } } @@ -239,7 +249,7 @@ impl ToBytes for u64 { impl ToBytes for bool { fn to_bytes>(&self, out: &mut V) { - out.push(if *self { 1u8 } else { 0u8 }) + out.vec_push(if *self { 1u8 } else { 0u8 }) } fn to_bytes_len(&self) -> usize { 1 } @@ -269,7 +279,7 @@ macro_rules! impl_uint_to_bytes { let count = self.to_bytes_len(); for i in 0..count { let j = count - 1 - i; - out.push(self.byte(j)); + out.vec_push(self.byte(j)); } } fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 } @@ -282,7 +292,7 @@ impl_uint_to_bytes!(U128); impl ToBytes for T where T: FixedHash { fn to_bytes>(&self, out: &mut V) { - out.extend(self.bytes()); + out.vec_extend(self.bytes()); } fn to_bytes_len(&self) -> usize { self.bytes().len() } } diff --git a/util/src/rlp/mod.rs b/util/src/rlp/mod.rs index 8b7814e5a..8dddf3acc 100644 --- a/util/src/rlp/mod.rs +++ b/util/src/rlp/mod.rs @@ -87,7 +87,7 @@ pub fn decode(bytes: &[u8]) -> T where T: Decodable { /// /// fn main () { /// let animal = "cat"; -/// let out = encode(&animal); +/// let out = encode(&animal).to_vec(); /// assert_eq!(out, vec![0x83, b'c', b'a', b't']); /// } /// ``` @@ -105,7 +105,7 @@ pub fn encode(object: &E) -> ElasticArray1024 where E: Encodable { /// /// fn main () { /// let animals = vec!["cat", "dog"]; -/// let out = encode_list(&animals); +/// 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']); /// } /// ``` diff --git a/util/src/rlp/rlpstream.rs b/util/src/rlp/rlpstream.rs index b5e9d2152..7a0223d23 100644 --- a/util/src/rlp/rlpstream.rs +++ b/util/src/rlp/rlpstream.rs @@ -69,8 +69,9 @@ impl Stream for RlpStream { let items = list.deref(); self.begin_list(items.len()); for el in items.iter() { - self.append(el); + el.rlp_append(self); } + self.note_appended(items.len()); self } @@ -240,7 +241,7 @@ impl ByteEncodable for T where T: ToBytes { impl<'a> ByteEncodable for &'a[u8] { fn to_bytes>(&self, out: &mut V) { - out.extend(self) + out.vec_extend(self) } fn bytes_len(&self) -> usize { @@ -250,7 +251,7 @@ impl<'a> ByteEncodable for &'a[u8] { impl ByteEncodable for Vec { fn to_bytes>(&self, out: &mut V) { - out.extend(self.deref()) + out.vec_extend(self.deref()) } fn bytes_len(&self) -> usize { diff --git a/util/src/rlp/rlptraits.rs b/util/src/rlp/rlptraits.rs index aa964c26c..437d86dc6 100644 --- a/util/src/rlp/rlptraits.rs +++ b/util/src/rlp/rlptraits.rs @@ -286,10 +286,10 @@ pub trait Stream: Sized { /// use util::rlp::*; /// /// fn main () { - /// let mut stream = RlpStream::new_list(2); - /// stream.begin_list([&"cat", &"dog"]); - /// let out = stream.out(); - /// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]); + /// 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(&mut self, list: &I) -> &mut Self where I: Deref, E: Encodable;