mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 12:11:09 +00:00
Merge branch 'master' into governance
This commit is contained in:
@@ -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 }
|
||||
@@ -53,3 +54,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>() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
//! Vec<u8> serialiser.
|
||||
|
||||
use runtime_support::Vec;
|
||||
use runtime_support::prelude::*;
|
||||
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::prelude::*;
|
||||
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::prelude::*;
|
||||
use runtime_support::{mem, slice};
|
||||
use joiner::Joiner;
|
||||
use endiansensitive::EndianSensitive;
|
||||
|
||||
@@ -35,7 +36,7 @@ pub trait Slicable: Sized {
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
self.as_slice_then(|s| s.to_vec())
|
||||
}
|
||||
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(set_slice: &F) -> Option<Self>;
|
||||
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(set_slice: &F) -> Option<Self>;
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec())
|
||||
}
|
||||
@@ -46,11 +47,12 @@ pub trait Slicable: Sized {
|
||||
pub trait NonTrivialSlicable: Slicable {}
|
||||
|
||||
impl<T: EndianSensitive> Slicable for T {
|
||||
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
|
||||
let size = size_of::<T>();
|
||||
let mut result: T = unsafe { uninitialized() };
|
||||
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
|
||||
let size = mem::size_of::<T>();
|
||||
let mut result: T = unsafe { mem::zeroed() };
|
||||
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, 0) {
|
||||
Some(result.from_le())
|
||||
@@ -59,16 +61,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>())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +79,7 @@ impl Slicable for Vec<u8> {
|
||||
fn from_slice(value: &[u8]) -> Option<Self> {
|
||||
Some(value[4..].to_vec())
|
||||
}
|
||||
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
|
||||
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
|
||||
u32::set_as_slice(fill_slice).and_then(|len| {
|
||||
let mut v = Vec::with_capacity(len as usize);
|
||||
unsafe { v.set_len(len as usize); }
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user