reorganize native-support exports

This commit is contained in:
Robert Habermeier
2018-01-23 16:51:04 +01:00
parent 3402f169a7
commit ce5f4da907
14 changed files with 53 additions and 37 deletions
+13 -5
View File
@@ -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;
@@ -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>`
@@ -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.
@@ -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>())
}
}
@@ -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,
+1 -1
View File
@@ -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};
@@ -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;
@@ -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};
@@ -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};
@@ -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;
});
@@ -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.
@@ -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)
}
@@ -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 {
+6 -6
View File
@@ -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);
}
}
}