From 21a4f9f82133e157760e7c035ab6374661dba70b Mon Sep 17 00:00:00 2001 From: Robert Habermeier <rphmeier@gmail.com> Date: Tue, 23 Jan 2018 17:43:35 +0100 Subject: [PATCH] fix grumbles --- .../polkadot/src/codec/endiansensitive.rs | 20 ++++++++++++++++++- .../wasm-runtime/polkadot/src/codec/joiner.rs | 2 +- .../polkadot/src/codec/keyedvec.rs | 2 +- .../polkadot/src/codec/slicable.rs | 5 ++--- .../polkadot/src/runtime/consensus.rs | 2 +- .../polkadot/src/runtime/session.rs | 2 +- .../polkadot/src/runtime/staking.rs | 2 +- .../polkadot/src/runtime/system.rs | 2 +- .../polkadot/src/support/primitives.rs | 2 +- .../polkadot/src/support/storable.rs | 4 ++-- 10 files changed, 30 insertions(+), 13 deletions(-) diff --git a/substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs b/substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs index a1576284228..c4978d5ce47 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs +++ b/substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs @@ -17,7 +17,8 @@ //! Endian manager. /// Trait to allow conversion to a know endian representation when sensitive. -pub trait EndianSensitive: Sized { +// note: the copy bound and static lifetimes are necessary for safety of `Slicable` blanket implementation. +pub trait EndianSensitive: Copy + 'static { fn to_le(self) -> Self { self } fn to_be(self) -> Self { self } fn from_le(self) -> Self { self } @@ -48,3 +49,20 @@ impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize); impl_non_endians!(u8, i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8], [u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40], [u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128]); + +#[cfg(test)] +mod tests { + use super::EndianSensitive; + + #[test] + fn endian_sensitive_is_copy() { + fn _takes_copy<T: Copy>() { } + fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_copy::<T>() } + } + + #[test] + fn endian_sensitive_outlives_static() { + fn _takes_static<T: 'static>() { } + fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_static::<T>() } + } +} diff --git a/substrate/wasm-runtime/polkadot/src/codec/joiner.rs b/substrate/wasm-runtime/polkadot/src/codec/joiner.rs index db38cacbd95..7fdb1e16456 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::Vec; +use runtime_support::prelude::*; 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 b1f903643c1..210606ab7c9 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::Vec; +use runtime_support::prelude::*; 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 2dfc2db49fd..40f8a532b71 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/slicable.rs +++ b/substrate/wasm-runtime/polkadot/src/codec/slicable.rs @@ -16,7 +16,7 @@ //! Serialisation. -use runtime_support::vec::Vec; +use runtime_support::prelude::*; use runtime_support::{mem, slice}; use joiner::Joiner; use endiansensitive::EndianSensitive; @@ -44,8 +44,7 @@ pub trait Slicable: Sized { /// Trait to mark that a type is not trivially (essentially "in place") serialisable. pub trait NonTrivialSlicable: Slicable {} -// note: the copy bound and static lifetimes are necessary for safety of `set_as_slice`. -impl<T: Copy + EndianSensitive + 'static> Slicable for T { +impl<T: EndianSensitive> Slicable for T { fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(fill_slice: F) -> Option<Self> { let size = mem::size_of::<T>(); let mut result: T = unsafe { mem::zeroed() }; diff --git a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs index 1b897a86cab..689fdca21e6 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::Vec; +use runtime_support::prelude::*; 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 34c67c9811c..6914bdc6558 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::Vec; +use runtime_support::prelude::*; 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 5f39ec14b26..9b0c659f5c9 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::Vec; +use runtime_support::prelude::*; 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 f6243ff4f7c..072e17df010 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/system.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/system.rs @@ -19,7 +19,7 @@ use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder, Hashable}; use runtime_support::mem; -use runtime_support::vec::Vec; +use runtime_support::prelude::*; use storable::Storable; use keyedvec::KeyedVec; use environment::with_env; diff --git a/substrate/wasm-runtime/polkadot/src/support/primitives.rs b/substrate/wasm-runtime/polkadot/src/support/primitives.rs index ea94c031905..338be807105 100644 --- a/substrate/wasm-runtime/polkadot/src/support/primitives.rs +++ b/substrate/wasm-runtime/polkadot/src/support/primitives.rs @@ -16,7 +16,7 @@ //! Primitive types. -use runtime_support::vec::Vec; +use runtime_support::prelude::*; use streamreader::StreamReader; use joiner::Joiner; use slicable::{Slicable, NonTrivialSlicable}; diff --git a/substrate/wasm-runtime/polkadot/src/support/storable.rs b/substrate/wasm-runtime/polkadot/src/support/storable.rs index 835e70ff3d5..9a871a638be 100644 --- a/substrate/wasm-runtime/polkadot/src/support/storable.rs +++ b/substrate/wasm-runtime/polkadot/src/support/storable.rs @@ -19,7 +19,7 @@ use slicable::Slicable; use endiansensitive::EndianSensitive; use keyedvec::KeyedVec; -use runtime_support::vec::Vec; +use runtime_support::prelude::*; use runtime_support::{self, twox_128}; /// Trait for a value which may be stored in the storage DB. @@ -37,7 +37,7 @@ pub trait Storable { /// Remove `key` from storage. pub fn kill(key: &[u8]) { runtime_support::set_storage(&twox_128(key)[..], b""); } -impl<T: Default + Copy + EndianSensitive + 'static> Storable for T { +impl<T: Default + EndianSensitive> Storable for T { fn lookup(key: &[u8]) -> Option<Self> { Slicable::set_as_slice(|out| runtime_support::read_storage(&twox_128(key)[..], out) == out.len()) } -- GitLab