fvm_std/
lib.rs

1//! fvm-std is a crate that supply base tool for developer to write contract with RUST more convenient
2//!
3//! ### Types
4//! Here some simple types that we provide for developers to write contract easier.
5//! #### H256
6//! `H256` is a 32-length bytes, in order to express block hash. There are methods implement
7//!
8//! ```ignore
9//! fn as_ref(&self) -> &H256
10//! pub const fn new(val: [u8; 32]) -> Self
11//! pub fn to_hex_string(&self) -> String
12//! ```
13//!
14//! #### H160
15//! `H160` is a 20-length bytes, in order to express address, there are methods implement
16//!
17//! ```ignore
18//! fn as_ref(&self) -> &H160
19//! pub const fn new(val: [u8; 20]) -> Self
20//! ```
21//!
22//! #### Address
23//!
24//! `Address` is a alias for `H160`, also a method implement
25//!
26//! ```ignore
27//! pub fn to_hex_string(&self) -> String
28//! ```
29//!
30//! ***Notice: `to_hex_string` is different to `to_string`, the latter will only print part of the content, if it is too long***
31//!
32//! #### Log level
33//!
34//! level for developer to use with event log. more details see in section [log](#Log)
35//!
36//! ```rust
37//! // CRITICAL ERROR WARNING NOTICE INFO DEBUG
38//! pub enum LogLevel {
39//!     CRITICAL,
40//!     ERROR,
41//!     WARNING,
42//!     NOTICE,
43//!     INFO,
44//!     DEBUG,
45//! }
46//! ```
47//!
48//! ### Log
49//!
50//! we have supplied several tool macros method for developer to print log in contract, include
51//! `critical!()`, `error!()`, `warning!()`, `notice!()`, `info!()`, `debug!()`
52//!
53//! #### Demo
54//!
55//! ```ignore
56//! pub fn add(&mut self) -> u64 {
57//!     info!("info {}", "hello");
58//!     warning!("warning {}", "hello");
59//!     notice!("notice {}", "hello");
60//!     1
61//! }
62//! ```
63//!
64//! ### Event
65//!
66//! supply event for developer used in contract.
67//!
68//! ```ignore
69//! #[derive(Debug, Serialize)]
70//! pub struct Event<T> where T: Serialize {
71//!     address: Address,
72//!     data: T,
73//!     topics: Vec<H256>,
74//! }
75//! ```
76//!
77//! **demo**
78//!
79//! ```ignore
80//! #[derive(Debug, Serialize)]
81//! struct Temp {
82//!     amount: u64,
83//! }
84//!
85//!
86//! #[storage]
87//! pub struct SetHash {
88//!     map: HyperMap<String, String>,
89//! }
90//!
91//! #[contract]
92//! impl SetHash {
93//!     fn new() -> Self {
94//!         Self { map: HyperMap::new() }
95//!     }
96//!
97//!     pub fn set_hash(&mut self, key: String, value: String) {
98//!         let temp = Temp { amount: 1 };
99//!         let ev = Event::new(temp, "set_hash".to_string(), vec!["test".to_string()]);
100//!         ev.emit();
101//!         self.map.insert(key, value);
102//!     }
103//!
104//!     pub fn get_hash(&mut self, key: String) -> &String {
105//!         self.map.get(&key).unwrap()
106//!     }
107//! }
108//!
109//! ```
110
111#![cfg_attr(not(any(feature = "std", test)), no_std)]
112// #![feature(proc_macro_hygiene)]
113// 在no-std中更为稳定的抛出panic信息
114// #![feature(panic_info_message)]
115
116
117extern crate alloc;
118
119///The prelude module provides common data types in the contract, and introduces some functions in the rust core library.
120pub mod prelude {
121    pub use crate::types::{Address, H256};
122    pub use alloc::boxed::Box;
123    pub use alloc::str;
124    pub use alloc::string::{self, String, ToString};
125    pub use alloc::vec::Vec;
126    pub use alloc::{format, vec};
127    pub use core::cmp;
128    pub use core::prelude::v1::*;
129}
130
131
132///The database module provides the interface to save the data in the contract to the chain and query the data from the chain.
133///The runtime module provides an interface to interact with the chain in the contract
134#[allow(dead_code)]
135pub mod runtime;
136
137///The types module provides common data types such as address, U128, hash, etc.
138pub mod types;
139
140/// The event module provides method to throw event in the contract.
141pub mod event;
142
143
144///The abi module provides serialization and deserialization methods for different data types in the contract
145#[cfg(feature = "advance")]
146pub mod advance;
147
148
149#[cfg(any(not(feature = "advance"), doc))]
150pub mod collections;
151
152#[cfg(any(not(feature = "advance"), doc))]
153pub mod normal;
154
155#[cfg(any(not(feature = "advance"), doc))]
156pub mod spread;
157
158mod utils;
159mod macros;
160