1#![warn(missing_docs)]
21#![cfg_attr(not(feature = "std"), no_std)]
22
23#[macro_export]
28macro_rules! map {
29 ($( $name:expr => $value:expr ),* $(,)? ) => (
30 vec![ $( ( $name, $value ) ),* ].into_iter().collect()
31 );
32}
33
34extern crate alloc;
35
36use alloc::vec::Vec;
37#[doc(hidden)]
38pub use codec::{Decode, Encode, MaxEncodedLen};
39use core::ops::Deref;
40use scale_info::TypeInfo;
41#[cfg(feature = "serde")]
42pub use serde;
43#[cfg(feature = "serde")]
44use serde::{Deserialize, Serialize};
45use sp_runtime_interface::pass_by::{PassByEnum, PassByInner};
46
47pub use sp_debug_derive::RuntimeDebug;
48
49#[cfg(feature = "serde")]
50pub use impl_serde::serialize as bytes;
51
52#[deprecated(
53 since = "27.0.0",
54 note = "`sp-crypto-hashing` re-exports will be removed after June 2024. Use `sp-crypto-hashing` instead."
55)]
56pub use sp_crypto_hashing::{self as hashing, *};
57
58pub mod const_hex2array;
59pub mod crypto;
60pub mod hexdisplay;
61pub use paste;
62mod address_uri;
63pub mod defer;
64pub mod hash;
65#[cfg(feature = "std")]
66mod hasher;
67pub mod offchain;
68pub mod testing;
69#[cfg(feature = "std")]
70pub mod traits;
71pub mod uint;
72
73#[cfg(feature = "bandersnatch-experimental")]
74pub mod bandersnatch;
75#[cfg(feature = "bls-experimental")]
76pub mod bls;
77pub mod crypto_bytes;
78pub mod ecdsa;
79pub mod ed25519;
80pub mod paired_crypto;
81pub mod sr25519;
82
83#[cfg(feature = "bls-experimental")]
84pub use bls::{bls377, bls381};
85#[cfg(feature = "bls-experimental")]
86pub use paired_crypto::{ecdsa_bls377, ecdsa_bls381};
87
88pub use self::{
89 hash::{convert_hash, H160, H256, H512},
90 uint::{U256, U512},
91};
92pub use crypto::{ByteArray, DeriveJunction, Pair, Public};
93
94#[cfg(feature = "std")]
95pub use self::hasher::blake2::Blake2Hasher;
96#[cfg(feature = "std")]
97pub use self::hasher::keccak::KeccakHasher;
98pub use hash_db::Hasher;
99
100pub use bounded_collections as bounded;
101#[cfg(feature = "std")]
102pub use bounded_collections::{bounded_btree_map, bounded_vec};
103pub use bounded_collections::{
104 parameter_types, ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128,
105 ConstU16, ConstU32, ConstU64, ConstU8, Get, GetDefault, TryCollect, TypedGet,
106};
107pub use sp_storage as storage;
108
109#[doc(hidden)]
110pub use sp_std;
111
112#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
114#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, Hash, PartialOrd, Ord))]
115pub struct Bytes(#[cfg_attr(feature = "serde", serde(with = "bytes"))] pub Vec<u8>);
116
117impl From<Vec<u8>> for Bytes {
118 fn from(s: Vec<u8>) -> Self {
119 Bytes(s)
120 }
121}
122
123impl From<OpaqueMetadata> for Bytes {
124 fn from(s: OpaqueMetadata) -> Self {
125 Bytes(s.0)
126 }
127}
128
129impl Deref for Bytes {
130 type Target = [u8];
131 fn deref(&self) -> &[u8] {
132 &self.0[..]
133 }
134}
135
136impl codec::WrapperTypeEncode for Bytes {}
137
138impl codec::WrapperTypeDecode for Bytes {
139 type Wrapped = Vec<u8>;
140}
141
142#[cfg(feature = "std")]
143impl alloc::str::FromStr for Bytes {
144 type Err = bytes::FromHexError;
145
146 fn from_str(s: &str) -> Result<Self, Self::Err> {
147 bytes::from_hex(s).map(Bytes)
148 }
149}
150
151#[derive(Encode, Decode, PartialEq, TypeInfo)]
153pub struct OpaqueMetadata(Vec<u8>);
154
155impl OpaqueMetadata {
156 pub fn new(metadata: Vec<u8>) -> Self {
158 OpaqueMetadata(metadata)
159 }
160}
161
162impl Deref for OpaqueMetadata {
163 type Target = Vec<u8>;
164
165 fn deref(&self) -> &Self::Target {
166 &self.0
167 }
168}
169
170#[derive(
172 Default,
173 Clone,
174 Eq,
175 PartialEq,
176 Ord,
177 PartialOrd,
178 Encode,
179 Decode,
180 RuntimeDebug,
181 PassByInner,
182 TypeInfo,
183)]
184#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
185pub struct OpaquePeerId(pub Vec<u8>);
186
187impl OpaquePeerId {
188 pub fn new(vec: Vec<u8>) -> Self {
190 OpaquePeerId(vec)
191 }
192}
193
194pub trait TypeId {
196 const TYPE_ID: [u8; 4];
198}
199
200#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
204pub enum LogLevel {
205 Error = 1_isize,
207 Warn = 2_isize,
209 Info = 3_isize,
211 Debug = 4_isize,
213 Trace = 5_isize,
215}
216
217impl From<u32> for LogLevel {
218 fn from(val: u32) -> Self {
219 match val {
220 x if x == LogLevel::Warn as u32 => LogLevel::Warn,
221 x if x == LogLevel::Info as u32 => LogLevel::Info,
222 x if x == LogLevel::Debug as u32 => LogLevel::Debug,
223 x if x == LogLevel::Trace as u32 => LogLevel::Trace,
224 _ => LogLevel::Error,
225 }
226 }
227}
228
229impl From<log::Level> for LogLevel {
230 fn from(l: log::Level) -> Self {
231 use log::Level::*;
232 match l {
233 Error => Self::Error,
234 Warn => Self::Warn,
235 Info => Self::Info,
236 Debug => Self::Debug,
237 Trace => Self::Trace,
238 }
239 }
240}
241
242impl From<LogLevel> for log::Level {
243 fn from(l: LogLevel) -> Self {
244 use self::LogLevel::*;
245 match l {
246 Error => Self::Error,
247 Warn => Self::Warn,
248 Info => Self::Info,
249 Debug => Self::Debug,
250 Trace => Self::Trace,
251 }
252 }
253}
254
255#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
259pub enum LogLevelFilter {
260 Off = 0_isize,
262 Error = 1_isize,
264 Warn = 2_isize,
266 Info = 3_isize,
268 Debug = 4_isize,
270 Trace = 5_isize,
272}
273
274impl From<LogLevelFilter> for log::LevelFilter {
275 fn from(l: LogLevelFilter) -> Self {
276 use self::LogLevelFilter::*;
277 match l {
278 Off => Self::Off,
279 Error => Self::Error,
280 Warn => Self::Warn,
281 Info => Self::Info,
282 Debug => Self::Debug,
283 Trace => Self::Trace,
284 }
285 }
286}
287
288impl From<log::LevelFilter> for LogLevelFilter {
289 fn from(l: log::LevelFilter) -> Self {
290 use log::LevelFilter::*;
291 match l {
292 Off => Self::Off,
293 Error => Self::Error,
294 Warn => Self::Warn,
295 Info => Self::Info,
296 Debug => Self::Debug,
297 Trace => Self::Trace,
298 }
299 }
300}
301
302#[cfg(not(feature = "std"))]
309pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 {
310 let encoded = value.encode();
311
312 let ptr = encoded.as_ptr() as u64;
313 let length = encoded.len() as u64;
314 let res = ptr | (length << 32);
315
316 core::mem::forget(encoded);
320
321 res
322}
323
324#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
327pub enum Void {}
328
329#[macro_export]
346macro_rules! impl_maybe_marker {
347 (
348 $(
349 $(#[$doc:meta] )+
350 trait $trait_name:ident: $( $trait_bound:path ),+;
351 )+
352 ) => {
353 $(
354 $(#[$doc])+
355 #[cfg(feature = "std")]
356 pub trait $trait_name: $( $trait_bound + )+ {}
357 #[cfg(feature = "std")]
358 impl<T: $( $trait_bound + )+> $trait_name for T {}
359
360 $(#[$doc])+
361 #[cfg(not(feature = "std"))]
362 pub trait $trait_name {}
363 #[cfg(not(feature = "std"))]
364 impl<T> $trait_name for T {}
365 )+
366 }
367}
368
369#[macro_export]
385macro_rules! impl_maybe_marker_std_or_serde {
386 (
387 $(
388 $(#[$doc:meta] )+
389 trait $trait_name:ident: $( $trait_bound:path ),+;
390 )+
391 ) => {
392 $(
393 $(#[$doc])+
394 #[cfg(any(feature = "serde", feature = "std"))]
395 pub trait $trait_name: $( $trait_bound + )+ {}
396 #[cfg(any(feature = "serde", feature = "std"))]
397 impl<T: $( $trait_bound + )+> $trait_name for T {}
398
399 $(#[$doc])+
400 #[cfg(not(any(feature = "serde", feature = "std")))]
401 pub trait $trait_name {}
402 #[cfg(not(any(feature = "serde", feature = "std")))]
403 impl<T> $trait_name for T {}
404 )+
405 }
406}
407
408pub const MAX_POSSIBLE_ALLOCATION: u32 = 33554432; #[macro_export]
430#[rustfmt::skip]
433macro_rules! generate_feature_enabled_macro {
434 ( $macro_name:ident, $feature_name:meta, $d:tt ) => {
435 $crate::paste::paste!{
436 #[cfg($feature_name)]
438 #[macro_export]
439 macro_rules! [<_ $macro_name>] {
440 ( $d ( $d input:tt )* ) => {
441 $d ( $d input )*
442 }
443 }
444
445 #[cfg(not($feature_name))]
447 #[macro_export]
448 macro_rules! [<_ $macro_name>] {
449 ( $d ( $d input:tt )* ) => {};
450 }
451
452 #[doc = concat!("`", stringify!($feature_name), "`")]
454 #[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
461 pub use [<_ $macro_name>] as $macro_name;
464 }
465 };
466}
467
468#[cfg(test)]
469mod tests {
470 use super::*;
471
472 #[test]
473 #[should_panic]
474 fn generate_feature_enabled_macro_panics() {
475 generate_feature_enabled_macro!(if_test, test, $);
476 if_test!(panic!("This should panic"));
477 }
478
479 #[test]
480 fn generate_feature_enabled_macro_works() {
481 generate_feature_enabled_macro!(if_not_test, not(test), $);
482 if_not_test!(panic!("This should not panic"));
483 }
484}