frame_support/storage/
hashed.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Operation on runtime storage using hashed keys.
19
20use super::unhashed;
21use alloc::vec::Vec;
22use codec::{Decode, Encode};
23
24/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
25pub fn get<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<T>
26where
27	T: Decode + Sized,
28	HashFn: Fn(&[u8]) -> R,
29	R: AsRef<[u8]>,
30{
31	unhashed::get(hash(key).as_ref())
32}
33
34/// Return the value of the item in storage under `key`, or the type's default if there is no
35/// explicit entry.
36pub fn get_or_default<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> T
37where
38	T: Decode + Sized + Default,
39	HashFn: Fn(&[u8]) -> R,
40	R: AsRef<[u8]>,
41{
42	unhashed::get_or_default(hash(key).as_ref())
43}
44
45/// Return the value of the item in storage under `key`, or `default_value` if there is no
46/// explicit entry.
47pub fn get_or<T, HashFn, R>(hash: &HashFn, key: &[u8], default_value: T) -> T
48where
49	T: Decode + Sized,
50	HashFn: Fn(&[u8]) -> R,
51	R: AsRef<[u8]>,
52{
53	unhashed::get_or(hash(key).as_ref(), default_value)
54}
55
56/// Return the value of the item in storage under `key`, or `default_value()` if there is no
57/// explicit entry.
58pub fn get_or_else<T, F, HashFn, R>(hash: &HashFn, key: &[u8], default_value: F) -> T
59where
60	T: Decode + Sized,
61	F: FnOnce() -> T,
62	HashFn: Fn(&[u8]) -> R,
63	R: AsRef<[u8]>,
64{
65	unhashed::get_or_else(hash(key).as_ref(), default_value)
66}
67
68/// Put `value` in storage under `key`.
69pub fn put<T, HashFn, R>(hash: &HashFn, key: &[u8], value: &T)
70where
71	T: Encode,
72	HashFn: Fn(&[u8]) -> R,
73	R: AsRef<[u8]>,
74{
75	unhashed::put(hash(key).as_ref(), value)
76}
77
78/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
79pub fn take<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<T>
80where
81	T: Decode + Sized,
82	HashFn: Fn(&[u8]) -> R,
83	R: AsRef<[u8]>,
84{
85	unhashed::take(hash(key).as_ref())
86}
87
88/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
89/// the default for its type.
90pub fn take_or_default<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> T
91where
92	T: Decode + Sized + Default,
93	HashFn: Fn(&[u8]) -> R,
94	R: AsRef<[u8]>,
95{
96	unhashed::take_or_default(hash(key).as_ref())
97}
98
99/// Return the value of the item in storage under `key`, or `default_value` if there is no
100/// explicit entry. Ensure there is no explicit entry on return.
101pub fn take_or<T, HashFn, R>(hash: &HashFn, key: &[u8], default_value: T) -> T
102where
103	T: Decode + Sized,
104	HashFn: Fn(&[u8]) -> R,
105	R: AsRef<[u8]>,
106{
107	unhashed::take_or(hash(key).as_ref(), default_value)
108}
109
110/// Return the value of the item in storage under `key`, or `default_value()` if there is no
111/// explicit entry. Ensure there is no explicit entry on return.
112pub fn take_or_else<T, F, HashFn, R>(hash: &HashFn, key: &[u8], default_value: F) -> T
113where
114	T: Decode + Sized,
115	F: FnOnce() -> T,
116	HashFn: Fn(&[u8]) -> R,
117	R: AsRef<[u8]>,
118{
119	unhashed::take_or_else(hash(key).as_ref(), default_value)
120}
121
122/// Check to see if `key` has an explicit entry in storage.
123pub fn exists<HashFn, R>(hash: &HashFn, key: &[u8]) -> bool
124where
125	HashFn: Fn(&[u8]) -> R,
126	R: AsRef<[u8]>,
127{
128	unhashed::exists(hash(key).as_ref())
129}
130
131/// Ensure `key` has no explicit entry in storage.
132pub fn kill<HashFn, R>(hash: &HashFn, key: &[u8])
133where
134	HashFn: Fn(&[u8]) -> R,
135	R: AsRef<[u8]>,
136{
137	unhashed::kill(hash(key).as_ref())
138}
139
140/// Get a Vec of bytes from storage.
141pub fn get_raw<HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<Vec<u8>>
142where
143	HashFn: Fn(&[u8]) -> R,
144	R: AsRef<[u8]>,
145{
146	unhashed::get_raw(hash(key).as_ref())
147}
148
149/// Put a raw byte slice into storage.
150pub fn put_raw<HashFn, R>(hash: &HashFn, key: &[u8], value: &[u8])
151where
152	HashFn: Fn(&[u8]) -> R,
153	R: AsRef<[u8]>,
154{
155	unhashed::put_raw(hash(key).as_ref(), value)
156}