mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 12:51:05 +00:00
Bring back zero copy
This commit is contained in:
@@ -22,11 +22,30 @@ use codec::{Slicable, KeyedVec};
|
|||||||
|
|
||||||
// TODO: consider using blake256 to avoid possible preimage attack.
|
// TODO: consider using blake256 to avoid possible preimage attack.
|
||||||
|
|
||||||
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
|
struct IncrementalInput<'a> {
|
||||||
|
key: &'a [u8],
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Input for IncrementalInput<'a> {
|
||||||
|
fn read(&mut self, into: &mut [u8]) -> usize {
|
||||||
|
let len = runtime_io::read_storage(self.key, into, self.pos).unwrap_or(0);
|
||||||
|
let read = ::rstd::cmp::min(len, into.len());
|
||||||
|
self.pos += read;
|
||||||
|
read
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
|
||||||
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
|
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
|
||||||
let maybe_raw = runtime_io::storage(&twox_128(key)[..]);
|
let key = twox_128(key);
|
||||||
maybe_raw.map(|raw| Slicable::decode(&mut &raw[..])
|
runtime_io::read_storage(&key[..], &mut [0; 0][..], 0).map(|_| {
|
||||||
.expect("storage should contain a decodable value if there is some entry"))
|
let mut input = IncrementalInput {
|
||||||
|
key: &key[..],
|
||||||
|
pos: 0,
|
||||||
|
};
|
||||||
|
Slicable::decode(&mut input)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the value of the item in storage under `key`, or the type's default if there is no
|
/// Return the value of the item in storage under `key`, or the type's default if there is no
|
||||||
@@ -153,9 +172,13 @@ pub mod unhashed {
|
|||||||
|
|
||||||
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
|
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
|
||||||
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
|
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
|
||||||
let maybe_raw = runtime_io::storage(key);
|
runtime_io::read_storage(key, &mut [0; 0][..], 0).map(|_| {
|
||||||
maybe_raw.map(|raw| Slicable::decode(&mut &raw[..])
|
let mut input = IncrementalInput {
|
||||||
.expect("storage should contain a decodable value if there is some entry"))
|
key,
|
||||||
|
pos: 0,
|
||||||
|
};
|
||||||
|
Slicable::decode(&mut input)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the value of the item in storage under `key`, or the type's default if there is no
|
/// Return the value of the item in storage under `key`, or the type's default if there is no
|
||||||
|
|||||||
Reference in New Issue
Block a user