tetcore_std/
lib.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 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 Tetcore 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
23#![cfg_attr(feature = "std",
24   doc = "Tetcore runtime standard library as compiled when linked with Rust's standard library.")]
25#![cfg_attr(not(feature = "std"),
26   doc = "Tetcore's runtime standard library as compiled without Rust's standard library.")]
27
28#[macro_export]
29macro_rules! map {
30	($( $name:expr => $value:expr ),*) => (
31		vec![ $( ( $name, $value ) ),* ].into_iter().collect()
32	)
33}
34
35/// Feature gate some code that should only be run when `std` feature is enabled.
36///
37/// # Example
38///
39/// ```
40/// use tetcore_std::if_std;
41///
42/// if_std! {
43///     // This code is only being compiled and executed when the `std` feature is enabled.
44///     println!("Hello native world");
45/// }
46/// ```
47#[cfg(feature = "std")]
48#[macro_export]
49macro_rules! if_std {
50	( $( $code:tt )* ) => {
51		$( $code )*
52	}
53}
54
55#[cfg(not(feature = "std"))]
56#[macro_export]
57macro_rules! if_std {
58	( $( $code:tt )* ) => {}
59}
60
61#[cfg(feature = "std")]
62include!("../with_std.rs");
63
64#[cfg(not(feature = "std"))]
65include!("../without_std.rs");
66
67
68/// A target for `core::write!` macro - constructs a string in memory.
69#[derive(Default)]
70pub struct Writer(vec::Vec<u8>);
71
72impl fmt::Write for Writer {
73	fn write_str(&mut self, s: &str) -> fmt::Result {
74		self.0.extend(s.as_bytes());
75		Ok(())
76	}
77}
78
79impl Writer {
80	/// Access the content of this `Writer` e.g. for printout
81	pub fn inner(&self) -> &vec::Vec<u8> {
82		&self.0
83	}
84
85	/// Convert into the content of this `Writer`
86	pub fn into_inner(self) -> vec::Vec<u8> {
87		self.0
88	}
89}
90
91/// Prelude of common useful imports.
92///
93/// This should include only things which are in the normal std prelude.
94pub mod prelude {
95	pub use crate::vec::Vec;
96	pub use crate::boxed::Box;
97	pub use crate::cmp::{Eq, PartialEq, Reverse};
98	pub use crate::clone::Clone;
99
100	// Re-export `vec!` macro here, but not in `std` mode, since
101	// std's prelude already brings `vec!` into the scope.
102	#[cfg(not(feature = "std"))]
103	pub use crate::vec;
104}