use crate::sp_std::prelude::*;
use codec::{Codec, Encode, Decode};
pub use sp_core::storage::{ChildInfo, ChildType};
pub fn get<T: Decode + Sized>(
child_info: &ChildInfo,
key: &[u8],
) -> Option<T> {
match child_info.child_type() {
ChildType::ParentKeyId => {
let storage_key = child_info.storage_key();
sp_io::default_child_storage::get(
storage_key,
key,
).and_then(|v| {
Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
runtime_print!("ERROR: Corrupted state in child trie at {:?}/{:?}", storage_key, key);
None
})
})
},
}
}
pub fn get_or_default<T: Decode + Sized + Default>(
child_info: &ChildInfo,
key: &[u8],
) -> T {
get(child_info, key).unwrap_or_else(Default::default)
}
pub fn get_or<T: Decode + Sized>(
child_info: &ChildInfo,
key: &[u8],
default_value: T,
) -> T {
get(child_info, key).unwrap_or(default_value)
}
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
get(child_info, key).unwrap_or_else(default_value)
}
pub fn put<T: Encode>(
child_info: &ChildInfo,
key: &[u8],
value: &T,
) {
match child_info.child_type() {
ChildType::ParentKeyId => value.using_encoded(|slice|
sp_io::default_child_storage::set(
child_info.storage_key(),
key,
slice,
)
),
}
}
pub fn take<T: Decode + Sized>(
child_info: &ChildInfo,
key: &[u8],
) -> Option<T> {
let r = get(child_info, key);
if r.is_some() {
kill(child_info, key);
}
r
}
pub fn take_or_default<T: Codec + Sized + Default>(
child_info: &ChildInfo,
key: &[u8],
) -> T {
take(child_info, key).unwrap_or_else(Default::default)
}
pub fn take_or<T: Codec + Sized>(
child_info: &ChildInfo,
key: &[u8],
default_value: T,
) -> T {
take(child_info, key).unwrap_or(default_value)
}
pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
take(child_info, key).unwrap_or_else(default_value)
}
pub fn exists(
child_info: &ChildInfo,
key: &[u8],
) -> bool {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::read(
child_info.storage_key(),
key, &mut [0;0][..], 0,
).is_some(),
}
}
pub fn kill_storage(
child_info: &ChildInfo,
) {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::storage_kill(
child_info.storage_key(),
),
}
}
pub fn kill(
child_info: &ChildInfo,
key: &[u8],
) {
match child_info.child_type() {
ChildType::ParentKeyId => {
sp_io::default_child_storage::clear(
child_info.storage_key(),
key,
);
},
}
}
pub fn get_raw(
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::get(
child_info.storage_key(),
key,
),
}
}
pub fn put_raw(
child_info: &ChildInfo,
key: &[u8],
value: &[u8],
) {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::set(
child_info.storage_key(),
key,
value,
),
}
}
pub fn root(
child_info: &ChildInfo,
) -> Vec<u8> {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::root(
child_info.storage_key(),
),
}
}