From ce5f4da90771160b1c4b71da5f735d9f8a119e54 Mon Sep 17 00:00:00 2001
From: Robert Habermeier <rphmeier@gmail.com>
Date: Tue, 23 Jan 2018 16:51:04 +0100
Subject: [PATCH] reorganize native-support exports

---
 substrate/native-runtime/support/src/lib.rs    | 18 +++++++++++++-----
 .../wasm-runtime/polkadot/src/codec/joiner.rs  |  2 +-
 .../polkadot/src/codec/keyedvec.rs             |  2 +-
 .../polkadot/src/codec/slicable.rs             | 17 ++++++++++-------
 .../polkadot/src/codec/streamreader.rs         |  4 ++--
 substrate/wasm-runtime/polkadot/src/lib.rs     |  2 +-
 .../polkadot/src/runtime/consensus.rs          |  2 +-
 .../polkadot/src/runtime/session.rs            |  2 +-
 .../polkadot/src/runtime/staking.rs            |  2 +-
 .../polkadot/src/runtime/system.rs             |  5 +++--
 .../polkadot/src/support/environment.rs        |  9 ++++++---
 .../polkadot/src/support/primitives.rs         | 10 +++++-----
 .../polkadot/src/support/storable.rs           |  3 ++-
 substrate/wasm-runtime/support/src/lib.rs      | 12 ++++++------
 14 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/substrate/native-runtime/support/src/lib.rs b/substrate/native-runtime/support/src/lib.rs
index 348c0b17a9c..91005a2f1d7 100644
--- a/substrate/native-runtime/support/src/lib.rs
+++ b/substrate/native-runtime/support/src/lib.rs
@@ -24,12 +24,20 @@ extern crate polkadot_primitives as primitives;
 use std::fmt;
 use primitives::ed25519;
 
-pub use std::vec::Vec;
-pub use std::rc::Rc;
-pub use std::cell::RefCell;
-pub use std::boxed::Box;
+pub use std::vec;
+pub use std::rc;
+pub use std::cell;
+pub use std::boxed;
 pub use std::slice;
-pub use std::mem::{size_of, transmute, swap, uninitialized};
+pub use std::mem;
+
+/// Prelude of common useful imports.
+///
+/// This should include only things which are in the normal std prelude.
+pub mod prelude {
+	pub use std::vec::Vec;
+	pub use std::boxed::Box;
+}
 
 pub use polkadot_state_machine::Externalities;
 
diff --git a/substrate/wasm-runtime/polkadot/src/codec/joiner.rs b/substrate/wasm-runtime/polkadot/src/codec/joiner.rs
index 92a5aa87fca..db38cacbd95 100644
--- a/substrate/wasm-runtime/polkadot/src/codec/joiner.rs
+++ b/substrate/wasm-runtime/polkadot/src/codec/joiner.rs
@@ -16,7 +16,7 @@
 
 //! Vec<u8> serialiser.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use slicable::Slicable;
 
 /// Trait to allow itself to be serialised into a `Vec<u8>`
diff --git a/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs b/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs
index 1f803b7c620..b1f903643c1 100644
--- a/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs
+++ b/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs
@@ -16,7 +16,7 @@
 
 //! Serialiser and prepender.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use slicable::Slicable;
 
 /// Trait to allow itselg to be serialised and prepended by a given slice.
diff --git a/substrate/wasm-runtime/polkadot/src/codec/slicable.rs b/substrate/wasm-runtime/polkadot/src/codec/slicable.rs
index 5ec042ec9bf..a013abb9980 100644
--- a/substrate/wasm-runtime/polkadot/src/codec/slicable.rs
+++ b/substrate/wasm-runtime/polkadot/src/codec/slicable.rs
@@ -16,7 +16,8 @@
 
 //! Serialisation.
 
-use runtime_support::{Vec, size_of, transmute, uninitialized, slice};
+use runtime_support::vec::Vec;
+use runtime_support::{mem, slice};
 use joiner::Joiner;
 use endiansensitive::EndianSensitive;
 
@@ -45,10 +46,11 @@ pub trait NonTrivialSlicable: Slicable {}
 
 impl<T: EndianSensitive> Slicable for T {
 	fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(fill_slice: F) -> Option<Self> {
-		let size = size_of::<T>();
-		let mut result: T = unsafe { uninitialized() };
+		let size = mem::size_of::<T>();
+		let mut result: T = unsafe { mem::uninitialized() };
 		let result_slice = unsafe {
-			slice::from_raw_parts_mut(transmute::<*mut T, *mut u8>(&mut result), size)
+			let ptr = &mut result as *mut _ as *mut u8;
+			slice::from_raw_parts_mut(ptr, size)
 		};
 		if fill_slice(result_slice) {
 			Some(result.from_le())
@@ -57,16 +59,17 @@ impl<T: EndianSensitive> Slicable for T {
 		}
 	}
 	fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
-		let size = size_of::<Self>();
+		let size = mem::size_of::<Self>();
 		self.as_le_then(|le| {
 			let value_slice = unsafe {
-				slice::from_raw_parts(transmute::<*const Self, *const u8>(le), size)
+				let ptr = le as *const _ as *const u8;
+				slice::from_raw_parts(ptr, size)
 			};
 			f(value_slice)
 		})
 	}
 	fn size_of(_value: &[u8]) -> Option<usize> {
-		Some(size_of::<Self>())
+		Some(mem::size_of::<Self>())
 	}
 }
 
diff --git a/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs b/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs
index 371ceed4eea..33056a5ac39 100644
--- a/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs
+++ b/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs
@@ -20,13 +20,13 @@ use slicable::Slicable;
 
 /// Simple deserialiser.
 pub struct StreamReader<'a> {
-	data: &'a[u8],
+	data: &'a [u8],
 	offset: usize,
 }
 
 impl<'a> StreamReader<'a> {
 	/// Create a new deserialiser based on the `data`.
-	pub fn new(data: &'a[u8]) -> Self {
+	pub fn new(data: &'a [u8]) -> Self {
 		StreamReader {
 			data: data,
 			offset: 0,
diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs
index 7a96b6cee44..a08cc046d36 100644
--- a/substrate/wasm-runtime/polkadot/src/lib.rs
+++ b/substrate/wasm-runtime/polkadot/src/lib.rs
@@ -34,7 +34,7 @@ pub use support::{primitives, function, environment, storable};
 #[cfg(test)]
 pub use support::{testing, statichex};
 
-use runtime_support::Vec;
+use runtime_support::prelude::*;
 use slicable::Slicable;
 use primitives::{Block, UncheckedTransaction};
 
diff --git a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs
index 1e12135806f..1b897a86cab 100644
--- a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs
+++ b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs
@@ -16,7 +16,7 @@
 
 //! Conensus module for runtime; manages the authority set ready for the native code.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use storable::StorageVec;
 use primitives::SessionKey;
 
diff --git a/substrate/wasm-runtime/polkadot/src/runtime/session.rs b/substrate/wasm-runtime/polkadot/src/runtime/session.rs
index 907b3e16b16..34c67c9811c 100644
--- a/substrate/wasm-runtime/polkadot/src/runtime/session.rs
+++ b/substrate/wasm-runtime/polkadot/src/runtime/session.rs
@@ -17,7 +17,7 @@
 //! Session manager: is told the validators and allows them to manage their session keys for the
 //! consensus module.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use keyedvec::KeyedVec;
 use storable::{kill, Storable, StorageVec};
 use primitives::{AccountID, SessionKey, BlockNumber};
diff --git a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs
index fc7c12798e3..5f39ec14b26 100644
--- a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs
+++ b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs
@@ -16,7 +16,7 @@
 
 //! Staking manager: Handles balances and periodically determines the best set of validators.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use keyedvec::KeyedVec;
 use storable::{Storable, StorageVec};
 use primitives::{BlockNumber, AccountID};
diff --git a/substrate/wasm-runtime/polkadot/src/runtime/system.rs b/substrate/wasm-runtime/polkadot/src/runtime/system.rs
index 5ffa03ed818..f6243ff4f7c 100644
--- a/substrate/wasm-runtime/polkadot/src/runtime/system.rs
+++ b/substrate/wasm-runtime/polkadot/src/runtime/system.rs
@@ -18,7 +18,8 @@
 //! and depositing logs.
 
 use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder, Hashable};
-use runtime_support::{Vec, swap};
+use runtime_support::mem;
+use runtime_support::vec::Vec;
 use storable::Storable;
 use keyedvec::KeyedVec;
 use environment::with_env;
@@ -46,7 +47,7 @@ pub fn execute_block(mut block: Block) {
 	// populate environment from header.
 	with_env(|e| {
 		e.block_number = block.header.number;
-		swap(&mut e.digest, &mut block.header.digest);
+		mem::swap(&mut e.digest, &mut block.header.digest);
 		e.next_log_index = 0;
 	});
 
diff --git a/substrate/wasm-runtime/polkadot/src/support/environment.rs b/substrate/wasm-runtime/polkadot/src/support/environment.rs
index c01a1f4e928..7d6acb62a82 100644
--- a/substrate/wasm-runtime/polkadot/src/support/environment.rs
+++ b/substrate/wasm-runtime/polkadot/src/support/environment.rs
@@ -16,7 +16,10 @@
 
 //! Environment API: Allows certain information to be accessed throughout the runtime.
 
-use runtime_support::{Rc, RefCell, transmute, Box};
+use runtime_support::mem;
+use runtime_support::cell::RefCell;
+use runtime_support::rc::Rc;
+
 use primitives::{BlockNumber, Digest};
 
 #[derive(Default)]
@@ -48,7 +51,7 @@ fn env() -> Rc<RefCell<Environment>> {
 			let singleton: Rc<RefCell<Environment>> = Rc::new(RefCell::new(Default::default()));
 
 			// Put it in the heap so it can outlive this call
-			SINGLETON = transmute(Box::new(singleton));
+			SINGLETON = mem::transmute(Box::new(singleton));
 		}
 
 		// Now we give out a copy of the data that is safe to use concurrently.
@@ -69,7 +72,7 @@ fn env() -> Rc<RefCell<Environment>> {
 			let singleton: Rc<RefCell<Environment>> = Rc::new(RefCell::new(Default::default()));
 
 			// Put it in the heap so it can outlive this call
-			*s.borrow_mut() = transmute(Box::new(singleton));
+			*s.borrow_mut() = mem::transmute(Box::new(singleton));
 		}
 
 		// Now we give out a copy of the data that is safe to use concurrently.
diff --git a/substrate/wasm-runtime/polkadot/src/support/primitives.rs b/substrate/wasm-runtime/polkadot/src/support/primitives.rs
index 3ab385d4889..76859375f84 100644
--- a/substrate/wasm-runtime/polkadot/src/support/primitives.rs
+++ b/substrate/wasm-runtime/polkadot/src/support/primitives.rs
@@ -16,12 +16,12 @@
 
 //! Primitive types.
 
-use runtime_support::Vec;
+use runtime_support::vec::Vec;
 use streamreader::StreamReader;
 use joiner::Joiner;
 use slicable::{Slicable, NonTrivialSlicable};
 use function::Function;
-use runtime_support::{size_of, blake2_256, twox_128, twox_256, ed25519_verify};
+use runtime_support::{mem, blake2_256, twox_128, twox_256, ed25519_verify};
 
 #[cfg(test)]
 use std::fmt;
@@ -90,7 +90,7 @@ impl Slicable for Header {
 	}
 
 	fn size_of(data: &[u8]) -> Option<usize> {
-		let first_part = size_of::<Hash>() + size_of::<BlockNumber>() + size_of::<Hash>() + size_of::<Hash>();
+		let first_part = mem::size_of::<Hash>() + mem::size_of::<BlockNumber>() + mem::size_of::<Hash>() + mem::size_of::<Hash>();
 		let second_part = <Vec<Vec<u8>>>::size_of(&data[first_part..])?;
 		Some(first_part + second_part)
 	}
@@ -135,7 +135,7 @@ impl Slicable for Transaction {
 	}
 
 	fn size_of(data: &[u8]) -> Option<usize> {
-		let first_part = size_of::<AccountID>() + size_of::<TxOrder>() + size_of::<u8>();
+		let first_part = mem::size_of::<AccountID>() + mem::size_of::<TxOrder>() + mem::size_of::<u8>();
 		let second_part = <Vec<u8>>::size_of(&data[first_part..])?;
 		Some(first_part + second_part)
 	}
@@ -211,7 +211,7 @@ impl Slicable for UncheckedTransaction {
 	}
 
 	fn size_of(data: &[u8]) -> Option<usize> {
-		let first_part = size_of::<[u8; 64]>();
+		let first_part = mem::size_of::<[u8; 64]>();
 		let second_part = <Transaction>::size_of(&data[first_part..])?;
 		Some(first_part + second_part)
 	}
diff --git a/substrate/wasm-runtime/polkadot/src/support/storable.rs b/substrate/wasm-runtime/polkadot/src/support/storable.rs
index 3caf3ed19bf..90807168e18 100644
--- a/substrate/wasm-runtime/polkadot/src/support/storable.rs
+++ b/substrate/wasm-runtime/polkadot/src/support/storable.rs
@@ -19,7 +19,8 @@
 use slicable::Slicable;
 use endiansensitive::EndianSensitive;
 use keyedvec::KeyedVec;
-use runtime_support::{self, twox_128, Vec};
+use runtime_support::vec::Vec;
+use runtime_support::{self, twox_128};
 
 /// Trait for a value which may be stored in the storage DB.
 pub trait Storable {
diff --git a/substrate/wasm-runtime/support/src/lib.rs b/substrate/wasm-runtime/support/src/lib.rs
index b65b5743515..4c34a9f6683 100644
--- a/substrate/wasm-runtime/support/src/lib.rs
+++ b/substrate/wasm-runtime/support/src/lib.rs
@@ -4,14 +4,14 @@
 #![cfg_attr(feature = "strict", deny(warnings))]
 
 #![feature(alloc)]
-//#[macro_use]
 extern crate alloc;
-pub use alloc::vec::Vec;
-pub use alloc::boxed::Box;
+
+pub use alloc::vec;
+pub use alloc::boxed;
 pub use alloc::rc::Rc;
-pub use core::mem::{transmute, size_of, uninitialized, swap};
+pub use core::mem;
 pub use core::slice;
-pub use core::cell::{RefCell, Ref, RefMut};
+pub use core::cell;
 
 extern crate pwasm_libc;
 extern crate pwasm_alloc;
@@ -109,7 +109,7 @@ pub trait Printable {
 impl<'a> Printable for &'a [u8] {
 	fn print(self) {
 		unsafe {
-			ext_print(&self[0] as *const u8, self.len() as u32);
+			ext_print(self.as_ptr(), self.len() as u32);
 		}
 	}
 }
-- 
GitLab