mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 08:41:07 +00:00
Revise storage API.
This commit is contained in:
@@ -17,7 +17,9 @@
|
||||
//! Endian manager.
|
||||
|
||||
/// Trait to allow conversion to a know endian representation when sensitive.
|
||||
// note: the copy bound and static lifetimes are necessary for safety of `Slicable` blanket implementation.
|
||||
/// Types implementing this trait must have a size > 0.
|
||||
// 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 }
|
||||
|
||||
@@ -49,6 +49,7 @@ 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 = mem::size_of::<T>();
|
||||
assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
|
||||
let mut result: T = unsafe { mem::zeroed() };
|
||||
let result_slice = unsafe {
|
||||
let ptr = &mut result as *mut _ as *mut u8;
|
||||
@@ -62,6 +63,7 @@ impl<T: EndianSensitive> Slicable for T {
|
||||
}
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
let size = mem::size_of::<Self>();
|
||||
assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
|
||||
self.as_le_then(|le| {
|
||||
let value_slice = unsafe {
|
||||
let ptr = le as *const _ as *const u8;
|
||||
|
||||
Reference in New Issue
Block a user