sp_std/
lib.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//! Lowest-abstraction level for the Substrate runtime: just exports useful primitives from std
19//! or client/alloc to be used with any code that depends on the runtime.
20
21#![cfg_attr(not(feature = "std"), no_std)]
22#![cfg_attr(
23	feature = "std",
24	doc = "Substrate runtime standard library as compiled when linked with Rust's standard library."
25)]
26#![cfg_attr(
27	not(feature = "std"),
28	doc = "Substrate's runtime standard library as compiled without Rust's standard library."
29)]
30
31/// Initialize a key-value collection from array.
32///
33/// Creates a vector of given pairs and calls `collect` on the iterator from it.
34/// Can be used to create a `HashMap`.
35#[macro_export]
36macro_rules! map {
37	($( $name:expr => $value:expr ),* $(,)? ) => (
38		vec![ $( ( $name, $value ) ),* ].into_iter().collect()
39	)
40}
41
42/// Feature gate some code that should only be run when `std` feature is enabled.
43///
44/// # Example
45///
46/// ```
47/// use sp_std::if_std;
48///
49/// if_std! {
50///     // This code is only being compiled and executed when the `std` feature is enabled.
51///     println!("Hello native world");
52/// }
53/// ```
54#[cfg(feature = "std")]
55#[macro_export]
56macro_rules! if_std {
57	( $( $code:tt )* ) => {
58		$( $code )*
59	}
60}
61
62#[cfg(not(feature = "std"))]
63#[macro_export]
64macro_rules! if_std {
65	( $( $code:tt )* ) => {};
66}
67
68#[cfg(feature = "std")]
69include!("../with_std.rs");
70
71#[cfg(not(feature = "std"))]
72include!("../without_std.rs");
73
74/// A target for `core::write!` macro - constructs a string in memory.
75#[derive(Default)]
76pub struct Writer(vec::Vec<u8>);
77
78impl fmt::Write for Writer {
79	fn write_str(&mut self, s: &str) -> fmt::Result {
80		self.0.extend(s.as_bytes());
81		Ok(())
82	}
83}
84
85impl Writer {
86	/// Access the content of this `Writer` e.g. for printout
87	pub fn inner(&self) -> &vec::Vec<u8> {
88		&self.0
89	}
90
91	/// Convert into the content of this `Writer`
92	pub fn into_inner(self) -> vec::Vec<u8> {
93		self.0
94	}
95}
96
97/// Prelude of common useful imports.
98///
99/// This should include only things which are in the normal std prelude.
100pub mod prelude {
101	pub use crate::{
102		borrow::ToOwned,
103		boxed::Box,
104		clone::Clone,
105		cmp::{Eq, PartialEq, Reverse},
106		iter::IntoIterator,
107		vec::Vec,
108	};
109
110	// Re-export `vec!` macro here, but not in `std` mode, since
111	// std's prelude already brings `vec!` into the scope.
112	#[cfg(not(feature = "std"))]
113	pub use crate::vec;
114}