diff --git a/db/src/database.rs b/db/src/database.rs index 0ad02b783..2360e69ca 100644 --- a/db/src/database.rs +++ b/db/src/database.rs @@ -60,7 +60,7 @@ impl WriteCache { self.entries.insert(key, WriteCacheEntry::Remove); } - fn get(&self, key: &Vec) -> Option> { + fn get(&self, key: &[u8]) -> Option> { self.entries.get(key).and_then( |vec_ref| match vec_ref { &WriteCacheEntry::Write(ref val) => Some(val.clone()), diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 4e5bc468a..e15a4b168 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -16,8 +16,6 @@ //! Blockchain block. -#![cfg_attr(feature="dev", allow(ptr_arg))] // Because of &LastHashes -> &Vec<_> - use common::*; use engine::*; use state::*; @@ -76,11 +74,11 @@ pub struct BlockRefMut<'a> { /// Block header. pub header: &'a mut Header, /// Block transactions. - pub transactions: &'a Vec, + pub transactions: &'a [SignedTransaction], /// Block uncles. - pub uncles: &'a Vec
, + pub uncles: &'a [Header], /// Transaction receipts. - pub receipts: &'a Vec, + pub receipts: &'a [Receipt], /// State. pub state: &'a mut State, /// Traces. @@ -92,11 +90,11 @@ pub struct BlockRef<'a> { /// Block header. pub header: &'a Header, /// Block transactions. - pub transactions: &'a Vec, + pub transactions: &'a [SignedTransaction], /// Block uncles. - pub uncles: &'a Vec
, + pub uncles: &'a [Header], /// Transaction receipts. - pub receipts: &'a Vec, + pub receipts: &'a [Receipt], /// State. pub state: &'a State, /// Traces. @@ -152,16 +150,16 @@ pub trait IsBlock { fn state(&self) -> &State { &self.block().state } /// Get all information on transactions in this block. - fn transactions(&self) -> &Vec { &self.block().base.transactions } + fn transactions(&self) -> &[SignedTransaction] { &self.block().base.transactions } /// Get all information on receipts in this block. - fn receipts(&self) -> &Vec { &self.block().receipts } + fn receipts(&self) -> &[Receipt] { &self.block().receipts } /// Get all information concerning transaction tracing in this block. fn traces(&self) -> &Option> { &self.block().traces } /// Get all uncles in this block. - fn uncles(&self) -> &Vec
{ &self.block().base.uncles } + fn uncles(&self) -> &[Header] { &self.block().base.uncles } } /// Trait for a object that has a state database. diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 64ccb7db1..94424d3ef 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -426,7 +426,7 @@ impl Client { }; // Commit results - let receipts = block.receipts().clone(); + let receipts = block.receipts().to_owned(); let traces = From::from(block.traces().clone().unwrap_or_else(Vec::new)); // CHECK! I *think* this is fine, even if the state_root is equal to another diff --git a/ethcore/src/header.rs b/ethcore/src/header.rs index 77ec4813b..01d0bbb3d 100644 --- a/ethcore/src/header.rs +++ b/ethcore/src/header.rs @@ -143,7 +143,7 @@ impl Header { /// Get the difficulty field of the header. pub fn difficulty(&self) -> &U256 { &self.difficulty } /// Get the seal field of the header. - pub fn seal(&self) -> &Vec { &self.seal } + pub fn seal(&self) -> &[Bytes] { &self.seal } // TODO: seal_at, set_seal_at &c. diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index c5e4b4a5c..800f8f492 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -665,7 +665,7 @@ impl MinerService for Miner { }; match (&self.options.pending_set, sealing_set) { (&PendingSet::AlwaysQueue, _) | (&PendingSet::SealingOrElseQueue, None) => queue.top_transactions(), - (_, sealing) => sealing.map_or_else(Vec::new, |s| s.transactions().clone()), + (_, sealing) => sealing.map_or_else(Vec::new, |s| s.transactions().to_owned()), } } @@ -702,7 +702,7 @@ impl MinerService for Miner { .iter() .map(|t| t.hash()); - let receipts = pending.receipts().clone().into_iter(); + let receipts = pending.receipts().iter().cloned(); hashes.zip(receipts).collect() }, diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 5c2a7d8d7..1f7bd6fd8 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -143,7 +143,7 @@ impl Spec { } /// Get the known knodes of the network in enode format. - pub fn nodes(&self) -> &Vec { &self.nodes } + pub fn nodes(&self) -> &[String] { &self.nodes } /// Get the configured Network ID. pub fn network_id(&self) -> U256 { self.params.network_id } diff --git a/json/src/bytes.rs b/json/src/bytes.rs index 7741b8d3b..8772cd207 100644 --- a/json/src/bytes.rs +++ b/json/src/bytes.rs @@ -40,9 +40,9 @@ impl Into> for Bytes { } impl Deref for Bytes { - type Target = Vec; + type Target = [u8]; - fn deref(&self) -> &Vec { + fn deref(&self) -> &[u8] { &self.0 } } diff --git a/parity/configuration.rs b/parity/configuration.rs index 3f4955d50..dc00cb237 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -238,7 +238,7 @@ impl Configuration { }) }).collect(), Some(_) => Vec::new(), - None => spec.nodes().clone(), + None => spec.nodes().to_owned(), } } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 544c337b4..7cfe251fd 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -508,7 +508,7 @@ impl Eth for EthClient where fn compilers(&self, params: Params) -> Result { try!(self.active()); match params { - Params::None => to_value(&vec![] as &Vec), + Params::None => to_value(&(&[] as &[String])), _ => Err(Error::invalid_params()) } } diff --git a/signer/src/ws_server/session.rs b/signer/src/ws_server/session.rs index 4b2eb808c..1bf0332b4 100644 --- a/signer/src/ws_server/session.rs +++ b/signer/src/ws_server/session.rs @@ -25,11 +25,11 @@ use std::str::FromStr; use jsonrpc_core::IoHandler; use util::H256; -fn origin_is_allowed(self_origin: &str, header: Option<&Vec>) -> bool { +fn origin_is_allowed(self_origin: &str, header: Option<&[u8]>) -> bool { match header { None => false, Some(h) => { - let v = String::from_utf8(h.clone()).ok(); + let v = String::from_utf8(h.to_owned()).ok(); match v { Some(ref origin) if origin.starts_with("chrome-extension://") => true, Some(ref origin) if origin.starts_with(self_origin) => true, @@ -84,8 +84,8 @@ pub struct Session { impl ws::Handler for Session { fn on_request(&mut self, req: &ws::Request) -> ws::Result<(ws::Response)> { - let origin = req.header("origin").or_else(|| req.header("Origin")); - let host = req.header("host").or_else(|| req.header("Host")); + let origin = req.header("origin").or_else(|| req.header("Origin")).map(|x| &x[..]); + let host = req.header("host").or_else(|| req.header("Host")).map(|x| &x[..]); // Check request origin and host header. if !origin_is_allowed(&self.self_origin, origin) && !(origin.is_none() && origin_is_allowed(&self.self_origin, host)) { diff --git a/util/src/bytes.rs b/util/src/bytes.rs index 4e3d73da8..7426609d6 100644 --- a/util/src/bytes.rs +++ b/util/src/bytes.rs @@ -242,7 +242,7 @@ pub enum FromBytesError { } /// Value that can be serialized from bytes array -pub trait FromRawBytes : Sized { +pub trait FromRawBytes: Sized { /// function that will instantiate and initialize object from slice fn from_bytes(d: &[u8]) -> Result; } @@ -255,7 +255,7 @@ impl FromRawBytes for T where T: FixedHash { Ordering::Equal => () }; - let mut res: Self = unsafe { mem::uninitialized() }; + let mut res = T::zero(); res.copy_raw(bytes); Ok(res) } @@ -271,7 +271,7 @@ macro_rules! sized_binary_map { ::std::cmp::Ordering::Greater => return Err(FromBytesError::TooLong), ::std::cmp::Ordering::Equal => () }; - let mut res: Self = unsafe { ::std::mem::uninitialized() }; + let mut res: Self = 0; res.copy_raw(bytes); Ok(res) } @@ -298,7 +298,7 @@ sized_binary_map!(u32); sized_binary_map!(u64); /// Value that can be serialized from variable-length byte array -pub trait FromRawBytesVariable : Sized { +pub trait FromRawBytesVariable: Sized { /// Create value from slice fn from_bytes_variable(bytes: &[u8]) -> Result; } @@ -326,7 +326,7 @@ impl FromRawBytesVariable for Vec where T: FromRawBytes { let size_of_t = mem::size_of::(); let length_in_chunks = bytes.len() / size_of_t; - let mut result = Vec::with_capacity(length_in_chunks ); + let mut result = Vec::with_capacity(length_in_chunks); unsafe { result.set_len(length_in_chunks) }; for i in 0..length_in_chunks { *result.get_mut(i).unwrap() = try!(T::from_bytes( @@ -339,7 +339,7 @@ impl FromRawBytesVariable for Vec where T: FromRawBytes { impl FromRawBytes for (V1, T2) where V1: FromRawBytesVariable, T2: FromRawBytes { fn from_bytes(bytes: &[u8]) -> Result { let header = 8usize; - let mut map: (u64, ) = unsafe { mem::uninitialized() }; + let mut map: (u64, ) = (0,); if bytes.len() < header { return Err(FromBytesError::NotLongEnough); } map.copy_raw(&bytes[0..header]); @@ -358,7 +358,7 @@ impl FromRawBytes for (V1, V2, T3) { fn from_bytes(bytes: &[u8]) -> Result { let header = 16usize; - let mut map: (u64, u64, ) = unsafe { mem::uninitialized() }; + let mut map: (u64, u64, ) = (0, 0,); if bytes.len() < header { return Err(FromBytesError::NotLongEnough); } map.copy_raw(&bytes[0..header]); @@ -373,7 +373,7 @@ impl FromRawBytes for (V1, V2, T3) } } -impl<'a, V1, T2> ToBytesWithMap for (&'a Vec, &'a T2) where V1: ToBytesWithMap, T2: ToBytesWithMap { +impl<'a, V1, X1, T2> ToBytesWithMap for (X1, &'a T2) where V1: ToBytesWithMap, X1: Deref, T2: ToBytesWithMap { fn to_bytes_map(&self) -> Vec { let header = 8usize; let v1_size = mem::size_of::(); @@ -390,9 +390,9 @@ impl<'a, V1, T2> ToBytesWithMap for (&'a Vec, &'a T2) where V1: ToBytesWithM } -impl<'a, V1, V2, T3> ToBytesWithMap for (&'a Vec, &'a Vec, &'a T3) - where V1: ToBytesWithMap, - V2: ToBytesWithMap, +impl<'a, V1, X1, V2, X2, T3> ToBytesWithMap for (X1, X2, &'a T3) + where V1: ToBytesWithMap, X1: Deref, + V2: ToBytesWithMap, X2: Deref, T3: ToBytesWithMap { fn to_bytes_map(&self) -> Vec { @@ -433,7 +433,7 @@ pub trait ToBytesWithMap { impl ToBytesWithMap for T where T: FixedHash { fn to_bytes_map(&self) -> Vec { - self.as_slice().to_vec() + self.as_slice().to_owned() } } @@ -493,7 +493,7 @@ fn populate_big_types() { fn raw_bytes_from_tuple() { type Tup = (Vec, u16); - let tup = (vec![1u16, 1u16, 1u16, 1u16], 10u16); + let tup: (&[u16], u16) = (&[1; 4], 10); let bytes = vec![ // map 8u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, @@ -505,18 +505,19 @@ fn raw_bytes_from_tuple() { // 10u16 10u8, 0u8]; - let tup_from = Tup::from_bytes(&bytes).unwrap(); - assert_eq!(tup, tup_from); + let (v, x) = Tup::from_bytes(&bytes).unwrap(); + assert_eq!(tup, (&v[..], x)); + let tup_from = (v, x); - let tup_to = (&tup_from.0, &tup_from.1); + let tup_to = (tup_from.0, &tup_from.1); let bytes_to = tup_to.to_bytes_map(); assert_eq!(bytes_to, bytes); } #[test] fn bytes_map_from_triple() { - let data = (vec![2u16; 6], vec![6u32; 3], 12u64); - let bytes_map = (&data.0, &data.1, &data.2).to_bytes_map(); + let data: (&[u16], &[u32], u64) = (&[2; 6], &[6; 3], 12u64); + let bytes_map = (data.0, data.1, &data.2).to_bytes_map(); assert_eq!(bytes_map, vec![ // data map 2 x u64 12, 0, 0, 0, 0, 0, 0, 0, diff --git a/util/src/triehash.rs b/util/src/triehash.rs index f5f4e2123..bedea9b0f 100644 --- a/util/src/triehash.rs +++ b/util/src/triehash.rs @@ -195,7 +195,7 @@ fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStre } // take slices - let key: &Vec = &input[0].0; + let key: &[u8] = &input[0].0; let value: &[u8] = &input[0].1; // if the slice contains just one item, append the suffix of the key diff --git a/util/src/vector.rs b/util/src/vector.rs index 51a167a48..0fccac586 100644 --- a/util/src/vector.rs +++ b/util/src/vector.rs @@ -17,23 +17,23 @@ //! Vector extensions. /// Returns len of prefix shared with elem -/// +/// /// ```rust /// extern crate ethcore_util as util; /// use util::vector::SharedPrefix; -/// +/// /// fn main () { /// let a = vec![1,2,3,3,5]; /// let b = vec![1,2,3]; /// assert_eq!(a.shared_prefix_len(&b), 3); /// } /// ``` -pub trait SharedPrefix { +pub trait SharedPrefix { /// Get common prefix length fn shared_prefix_len(&self, elem: &[T]) -> usize; } -impl SharedPrefix for Vec where T: Eq { +impl SharedPrefix for [T] where T: Eq { fn shared_prefix_len(&self, elem: &[T]) -> usize { use std::cmp; let len = cmp::min(self.len(), elem.len()); @@ -58,7 +58,7 @@ mod test { let b = vec![1,2,3]; assert_eq!(a.shared_prefix_len(&b), 3); } - + #[test] fn test_shared_prefix3() { let a = vec![1,2,3,4,5,6];