mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 22:11:06 +00:00
Repot and introduce keccak-256 external.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
pub trait EndianSensitive: Sized {
|
||||
fn to_le(self) -> Self { self }
|
||||
fn to_be(self) -> Self { self }
|
||||
fn from_le(self) -> Self { self }
|
||||
fn from_be(self) -> Self { self }
|
||||
fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
|
||||
fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
|
||||
}
|
||||
|
||||
macro_rules! impl_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl EndianSensitive for $t {
|
||||
fn to_le(self) -> Self { <$t>::to_le(self) }
|
||||
fn to_be(self) -> Self { <$t>::to_be(self) }
|
||||
fn from_le(self) -> Self { <$t>::from_le(self) }
|
||||
fn from_be(self) -> Self { <$t>::from_be(self) }
|
||||
fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_be(); f(&d) }
|
||||
fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_le(); f(&d) }
|
||||
}
|
||||
)* }
|
||||
}
|
||||
macro_rules! impl_non_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl EndianSensitive for $t {}
|
||||
)* }
|
||||
}
|
||||
|
||||
impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize);
|
||||
impl_non_endians!(u8, i8, [u8; 20], [u8; 32]);
|
||||
@@ -0,0 +1,13 @@
|
||||
use runtime_support::Vec;
|
||||
use slicable::Slicable;
|
||||
|
||||
pub trait Joiner {
|
||||
fn join<T: Slicable + Sized>(self, value: &T) -> Self;
|
||||
}
|
||||
|
||||
impl Joiner for Vec<u8> {
|
||||
fn join<T: Slicable + Sized>(mut self, value: &T) -> Vec<u8> {
|
||||
value.as_slice_then(|s| self.extend_from_slice(s));
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use runtime_support::Vec;
|
||||
use primitives::AccountID;
|
||||
use slicable::Slicable;
|
||||
|
||||
pub trait KeyedVec {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8>;
|
||||
}
|
||||
|
||||
impl KeyedVec for AccountID {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
|
||||
let mut r = prepend_key.to_vec();
|
||||
r.extend_from_slice(self);
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_endians {
|
||||
( $( $t:ty ),* ) => { $(
|
||||
impl KeyedVec for $t {
|
||||
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
|
||||
self.as_slice_then(|slice| {
|
||||
let mut r = prepend_key.to_vec();
|
||||
r.extend_from_slice(slice);
|
||||
r
|
||||
})
|
||||
}
|
||||
}
|
||||
)* }
|
||||
}
|
||||
|
||||
impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize);
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod endiansensitive;
|
||||
pub mod streamreader;
|
||||
pub mod joiner;
|
||||
pub mod slicable;
|
||||
pub mod keyedvec;
|
||||
@@ -0,0 +1,69 @@
|
||||
use runtime_support::{Vec, size_of, transmute, uninitialized, slice};
|
||||
use joiner::Joiner;
|
||||
use endiansensitive::EndianSensitive;
|
||||
|
||||
/// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
|
||||
pub trait Slicable: Sized {
|
||||
fn from_slice(value: &[u8]) -> Option<Self> {
|
||||
Self::set_as_slice(|out| if value.len() == out.len() {
|
||||
out.copy_from_slice(&value);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
})
|
||||
}
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
self.as_slice_then(|s| s.to_vec())
|
||||
}
|
||||
fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(set_slice: F) -> Option<Self>;
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec())
|
||||
}
|
||||
fn size_of(_value: &[u8]) -> Option<usize>;
|
||||
}
|
||||
|
||||
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 result_slice = unsafe {
|
||||
slice::from_raw_parts_mut(transmute::<*mut T, *mut u8>(&mut result), size)
|
||||
};
|
||||
if fill_slice(result_slice) {
|
||||
Some(result.from_le())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
let size = size_of::<Self>();
|
||||
self.as_le_then(|le| {
|
||||
let value_slice = unsafe {
|
||||
slice::from_raw_parts(transmute::<*const Self, *const u8>(le), size)
|
||||
};
|
||||
f(value_slice)
|
||||
})
|
||||
}
|
||||
fn size_of(_value: &[u8]) -> Option<usize> {
|
||||
Some(size_of::<Self>())
|
||||
}
|
||||
}
|
||||
|
||||
impl Slicable for Vec<u8> {
|
||||
fn from_slice(value: &[u8]) -> Option<Self> {
|
||||
Some(value[4..].to_vec())
|
||||
}
|
||||
fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
|
||||
unimplemented!();
|
||||
}
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let mut r: Vec<u8> = Vec::new().join(&(self.len() as u32));
|
||||
r.extend_from_slice(&self);
|
||||
r
|
||||
}
|
||||
fn size_of(data: &[u8]) -> Option<usize> {
|
||||
u32::from_slice(&data[0..4]).map(|i| (i + 4) as usize)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
use slicable::Slicable;
|
||||
|
||||
pub struct StreamReader<'a> {
|
||||
data: &'a[u8],
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
impl<'a> StreamReader<'a> {
|
||||
pub fn new(data: &'a[u8]) -> Self {
|
||||
StreamReader {
|
||||
data: data,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
pub fn read<T: Slicable>(&mut self) -> Option<T> {
|
||||
let size = T::size_of(&self.data[self.offset..])?;
|
||||
let new_offset = self.offset + size;
|
||||
let slice = &self.data[self.offset..new_offset];
|
||||
self.offset = new_offset;
|
||||
Slicable::from_slice(slice)
|
||||
}
|
||||
}
|
||||
/*
|
||||
// Not in use yet
|
||||
// TODO: introduce fn size_will_be(&self) -> usize; to Slicable trait and implement
|
||||
struct StreamWriter<'a> {
|
||||
data: &'a mut[u8],
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
impl<'a> StreamWriter<'a> {
|
||||
pub fn new(data: &'a mut[u8]) -> Self {
|
||||
StreamWriter {
|
||||
data: data,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
pub fn write<T: Slicable>(&mut self, value: &T) -> bool {
|
||||
value.as_slice_then(|s| {
|
||||
let new_offset = self.offset + s.len();
|
||||
if self.data.len() <= new_offset {
|
||||
let slice = &mut self.data[self.offset..new_offset];
|
||||
self.offset = new_offset;
|
||||
slice.copy_from_slice(s);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user