rust_chain/
print.rs

1use crate::structs::*;
2use crate::vmapi;
3use crate::name::{ Name };
4
5///
6pub fn prints(s: &str) {
7    vmapi::print::prints_l(s.as_bytes().as_ptr(), s.len() as u32);
8}
9
10///
11pub fn printi(value: i64) {
12    vmapi::print::printi(value);
13}
14
15///
16pub fn printui(value: u64) {
17    vmapi::print::printui(value);
18}
19
20///
21pub fn printi128(value: i128) {
22    vmapi::print::printi128(value);
23}
24
25///
26pub fn printui128(value: u128) {
27    vmapi::print::printui128(value);
28}
29
30///
31pub fn printsf(value: f32) {
32    vmapi::print::printsf(value);
33}
34
35///
36pub fn printdf(value: f64) {
37    vmapi::print::printdf(value);
38}
39
40///
41pub fn printqf(value: &Float128) {
42    vmapi::print::printqf(value);
43}
44
45///
46pub fn printn(name: Name) {
47    vmapi::print::printn(name.value());
48}
49
50///
51pub fn printhex(data: &[u8]) {
52    vmapi::print::printhex(data.as_ptr(), data.len() as u32);
53}
54
55///
56pub trait Printable {
57    ///
58    fn print(&self);
59}
60
61impl Printable for Name {
62    ///
63    fn print(&self) {
64        printn(*self);
65    }
66}
67
68impl Printable for bool {
69    ///
70    fn print(&self) {
71        if *self {
72            prints("true");
73        } else {
74            prints("false");
75        }
76    }
77}
78
79impl Printable for i8 {
80    ///
81    fn print(&self) {
82        printi(*self as i64);
83    }
84}
85
86impl Printable for u8 {
87    ///
88    fn print(&self) {
89        printi(*self as i64);
90    }
91}
92
93impl Printable for i16 {
94    ///
95    fn print(&self) {
96        printi(*self as i64);
97    }
98}
99
100impl Printable for u16 {
101    ///
102    fn print(&self) {
103        printi(*self as i64);
104    }
105}
106
107impl Printable for i32 {
108    ///
109    fn print(&self) {
110        printi(*self as i64);
111    }
112}
113
114impl Printable for u32 {
115    ///
116    fn print(&self) {
117        printui(*self as u64);
118    }
119}
120
121impl Printable for usize {
122    ///
123    fn print(&self) {
124        printui(*self as u64);
125    }
126}
127
128impl Printable for i64 {
129    ///
130    fn print(&self) {
131        printi(*self);
132    }
133}
134
135impl Printable for u64 {
136    ///
137    fn print(&self) {
138        printui(*self);
139    }
140}
141
142impl Printable for i128 {
143    ///
144    fn print(&self) {
145        printi128(*self);
146    }
147}
148
149impl Printable for u128 {
150    ///
151    fn print(&self) {
152        printui128(*self);
153    }
154}
155
156impl Printable for [u8] {
157    ///
158    fn print(&self) {
159        printhex(self);
160    }
161}
162
163impl Printable for str {
164    ///
165    fn print(&self) {
166        prints(self);
167    }
168}
169
170impl Printable for f32 {
171    ///
172    fn print(&self) {
173        printsf(*self);
174    }
175}
176
177impl Printable for f64 {
178    ///
179    fn print(&self) {
180        printdf(*self);
181    }
182}
183
184impl Printable for Float128 {
185    ///
186    fn print(&self) {
187        printqf(self);
188    }
189}
190
191// #[macro_export]
192// macro_rules! eosio_print {
193//     ( $( $x:expr ),* ) => {
194//         {
195//             $(
196//                 $x.print();
197//                 prints(" ");
198//             )*
199//         }
200//     };
201// }
202
203///
204#[macro_export]
205macro_rules! eosio_print {
206    ($last:expr) => {
207        $last.print();
208    };
209    ($head:expr, $($tail:expr), +) => {
210        $head.print();
211        $crate::print::prints(" ");
212        eosio_print!($($tail),+);
213    };
214}
215
216///
217#[macro_export]
218macro_rules! chain_println {
219    ( $last:expr ) => {
220        $last.print();
221        $crate::print::prints("\n");
222    };
223    ( $head:expr, $($tail:expr), + ) => {
224        $head.print();
225        $crate::print::prints(" ");
226        chain_println!($($tail),+);
227    };
228}