1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! The `custom-print` crate helps you to define `print`, `println` and `dbg` macros
//! in wasm and customize them for other targets without any dependencies.
//!
//! # About
//!
//! This crate helps you to define `print`-like macros, `dbg` and `panic_hook`
//! on `wasm32-unknown-unknown` target without `wasm-bindgen` dependency.
//! Also, it can be used on another targets to override default std `write`-like macros,
//! add `try_` macros variants, or to specify panic hook function.
//! It works on stable Rust,
//! supports `no-alloc` and `no-std` environments and has no dependencies.
//!
//! In most cases it is suggested to use macros
//! [`define_macros`], [`define_macro`] or [`define_init_panic_hook`].
//! These macros define macros or functions with the specified names that use
//! [`FmtWriter`], [`FmtTryWriter`], [`ConcatWriter`], [`ConcatTryWriter`],
//! [`IoWriter`] or [`IoTryWriter`] with the specified closure, unsafe function or extern function.
//!
//! # Usage
//!
//! First, add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! custom-print = "1.0.0"
//! ```
//!
//! This crate depends on the standard library by default.
//! To use this crate in a `#![no_std]` context but with heap-allocations enabled,
//! use `default-features = false` in your `Cargo.toml` as shown below:
//!
//! ```toml
//! [dependencies.custom-print]
//! version = "1.0.0"
//! default-features = false
//! features = ["alloc"]
//! ```
//!
//! # Examples
//!
//! An example with an extern functions that takes a UTF-8 chars pointer and byte length
//! with no `std` prelude:
#![cfg_attr(feature = "alloc", doc = " ```rust")]
#![cfg_attr(not(feature = "alloc"), doc = " ```rust,compile_fail")]
//! #![no_std]
//! extern crate std;
//!
//! # pub mod ffi {
//! #     #[no_mangle] pub extern "C" fn console_log(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_warn(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_error(_: *const u8, _: usize) {}
//! # }
//! #
//! custom_print::define_macros!({ print, println },
//!     concat, extern "C" fn console_log(_: *const u8, _: usize));
//! custom_print::define_macros!({ eprint, eprintln, dbg },
//!     concat, extern "C" fn console_warn(_: *const u8, _: usize));
//! custom_print::define_init_panic_hook!(
//!     concat, extern "C" fn console_error(_: *const u8, _: usize));
//!
//! fn main() {
//!     init_panic_hook();
//!     println!("println");
//!     print!("print");
//!     eprintln!("eprintln");
//!     eprint!("eprint");
//!     dbg!("dbg");
//! }
//! ```
//!
//! An example with a closure that takes an [`str`] reference
//! in `no_std` and `no_alloc` context:
#![cfg_attr(feature = "std", doc = " ```rust")]
#![cfg_attr(not(feature = "std"), doc = " ```rust,compile_fail")]
//! #![no_std]
//! custom_print::define_macros!({ print, println }, fmt, |_value: &str| { /* ... */ });
//!
//! # fn main() { // avoid clippy::needless_doctest_main false positive
//! fn main() {
//!     println!("println");
//!     print!("print");
//! }
//! # main();
//! # }
//! ```
//!
//! An example with a function that takes a [`c_char`] pointer and overriding
//! [`std::print`] and [`std::println`] functions:
#![cfg_attr(feature = "std", doc = " ```rust")]
#![cfg_attr(not(feature = "std"), doc = " ```rust,compile_fail")]
//! fn write(_value: *const std::os::raw::c_char) { /* ... */ }
//!
//! custom_print::define_macros!({ cprint, cprintln }, concat, crate::write);
//! macro_rules! print { ($($args:tt)*) => { cprint!($($args)*); } }
//! macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } }
//!
//! fn main() {
//!     println!("println");
//!     print!("print");
//! }
//! ```
//!
//! Macro generation macros support specifying custom attributes, so you can
//! re-export the generated macros and use them in other crates:
//! ```rust
//! // foo crate:
//!
//! custom_print::define_macros!(
//!     #[macro_export] { print, println, cprint, cprintln },
//!     fmt, |_value: &str| { /* ... */ }
//! );
//!
//! # fn main() {
//! fn func() {
//!     cprintln!("cprintln");
//!     cprint!("cprint");
//! }
//!
//! // bar crate:
//!
//! fn main() {
//! #   /*
//!     foo::println!("println");
//!     foo::print!("print");
//! #   */
//! }
//! # func();
//! # main();
//! # }
//! ```
//!
//! You can find more usage examples in the tests and examples repository folders.
//!
//! # Macro expansion
//!
//! The example with [`define_macros`] and [`define_init_panic_hook`] with extern functions:
#![cfg_attr(feature = "alloc", doc = " ```rust")]
#![cfg_attr(not(feature = "alloc"), doc = " ```rust,compile_fail")]
//! #![no_std]
//! extern crate std;
//!
//! # pub mod ffi {
//! #     #[no_mangle] pub extern "C" fn console_log(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_warn(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_error(_: *const u8, _: usize) {}
//! # }
//! #
//! custom_print::define_macros!({ print, println, try_println },
//!     concat, extern "C" fn console_log(_: *const u8, _: usize));
//! custom_print::define_macros!({ eprint, eprintln, dbg },
//!     concat, extern "C" fn console_warn(_: *const u8, _: usize));
//! custom_print::define_init_panic_hook!(
//!     concat, extern "C" fn console_error(_: *const u8, _: usize));
//!
//! fn main() {
//!     init_panic_hook();
//!     println!("Greetings from println");
//!     let _ = try_println!("Greetings from try_println");
//!     eprintln!("Greetings from eprintln");
//!     let _ = dbg!("Greetings from dbg");
//! }
//! ```
//! partially expands to:
#![cfg_attr(feature = "alloc", doc = " ```rust")]
#![cfg_attr(not(feature = "alloc"), doc = " ```rust,compile_fail")]
//! # pub mod ffi {
//! #     #[no_mangle] pub extern "C" fn console_log(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_warn(_: *const u8, _: usize) {}
//! #     #[no_mangle] pub extern "C" fn console_error(_: *const u8, _: usize) {}
//! # }
//! #
//! fn init_panic_hook() {
//!     fn panic_hook(info: &::std::panic::PanicInfo<'_>) {
//!         ::core::writeln!(
//!             ::custom_print::ConcatWriter::from_closure({
//!                 extern "C" { fn console_error(_: *const u8, _: usize); }
//!                 |arg1: *const u8, arg2: usize| unsafe { console_error(arg1, arg2) }
//!             }),
//!             "{}",
//!             info
//!         ).expect("failed writing panic info");
//!     }
//!     ::std::panic::set_hook(::std::boxed::Box::new(panic_hook))
//! }
//!
//! fn main() {
//!     init_panic_hook();
//!
//!     ::core::writeln!(
//!         ::custom_print::ConcatWriter::from_closure({
//!             extern "C" { fn console_log(_: *const u8, _: usize); }
//!             |arg1: *const u8, arg2: usize| unsafe { console_log(arg1, arg2) }
//!         }),
//!         "Greetings from println"
//!     ).expect("failed writing");
//!
//!     let _ = ::core::writeln!(
//!         ::custom_print::ConcatTryWriter::from_closure({
//!             extern "C" { fn console_log(_: *const u8, _: usize); }
//!             |arg1: *const u8, arg2: usize| unsafe { console_log(arg1, arg2) }
//!         }),
//!         "Greetings from try_println"
//!     );
//!
//!     ::core::writeln!(
//!         ::custom_print::ConcatWriter::from_closure({
//!             extern "C" { fn console_warn(_: *const u8, _: usize); }
//!             |arg1: *const u8, arg2: usize| unsafe { console_warn(arg1, arg2) }
//!         }),
//!         "Greetings from eprintln"
//!     ).expect("failed writing");
//!
//!     let _ = ::custom_print::dbgwrite!(
//!         ::core::writeln,
//!         ::custom_print::ConcatWriter::from_closure({
//!             extern "C" { fn console_error(_: *const u8, _: usize); }
//!             |arg1: *const u8, arg2: usize| unsafe { console_error(arg1, arg2) }
//!         }),
//!         expect,
//!         ":?",
//!         "Greetings from dbg"
//!     );
//! }
//! ```
//!
//! # Feature Flags
//!
//! - `alloc` (implied by `std` so enabled by default):
//!   Enables [`WriteStringFn`] and [`ConcatWriter`] types.
//! - `std` (enabled by default):
//!   Enables [`IoWriter`], `{Try}Write{CStr|CString|CCharPtr}Fn`,
//!   [`define_panic_hook`] and [`define_init_panic_hook`].
//!
//! # Similar crates
//!
//! - [`web-log`]
//!   provides `print`, `println`, `eprint`, `eprintln`,
//!   requires wasm-bindgen.
//! - [`wasm-rs-dbg`]
//!   provides `dbg`,
//!   requires web-sys.
//! - [`console_log`]
//!   provides logging with `trace`, `debug`, `warn`, `error` etc.,
//!   requires log and web-sys.
//! - [`console_error_panic_hook`]
//!   provides panic_hook and panic hook set functions,
//!   requires wasm-bindgen.
//!
//! # Troubleshooting
//!
//! ## Macro name is ambiguous
//!
//! Errors like
//! `` `println` is ambiguous (macro-expanded name vs less macro-expanded name
//! from outer scope during import/macro resolution)``
//! occur because of the inability to overwrite standard rust macros
//! in [textual scope] with macro-expanded macros.
//!
//! Use can use proxy macros to replace `std` macros in [textual scope]:
//! ```rust
//! custom_print::define_macro!(cprintln, once: write_fn);
//! macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } }
//! ```
//!
//! Alternatively, use can override macro in the [path-based scope]:
//! ```rust
//! custom_print::define_macro!(cprintln, once: write_fn);
//! use cprintln as println;
//! ```
//!
//! See [`define_macro`] for more details.
//!
//! ## Println, dbg and others do nothing in submodules
//!
//! It looks like you have overridden `print`-like macros in the [path-based scope],
//! but you have not overridden them in submodules.
//! Use proxy macros as it shown [above](#macro_name_is_ambiguous)
//! or do not forget to override it in submodules.
//! ```rust
//! custom_print::define_macro!(cprintln, once: write_fn);
//! use cprintln as println;
//! mod submodule {
//!     use cprintln as println;
//! }
//! ```
//!
//! You can always use [`cargo expand`] to find out where the problem is.
//!
//! ## The trait bound `[closure]: IntoWriteFn<_>` is not satisfied
//!
//! Errors like:
//! ``the trait bound `...: IntoWriteFn<_>` is not satisfied`` or
//! ``the trait bound `...: IntoTryWriteFn<_>` is not satisfied``,
//! with `note: required by ``...Writer::<F1>::from_closure` ``
//! errors occur because of the inability to determine
//! the appropriate type of wrapper for the closure.
//!
//! Specify closure arguments if you haven't already,
//! or use helper closure that takes acceptable arguments (`&str`, `&[u8]`, etc.)
//! and convert them to the arguments your function requires.
//!
//! # License
//!
//! Licensed under either of
//!
//! - Apache License, Version 2.0
//!   ([LICENSE-APACHE](https://github.com/zheland/custom-print/blob/master/LICENSE-APACHE) or
//!   [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0))
//! - MIT license
//!   ([LICENSE-MIT](https://github.com/zheland/custom-print/blob/master/LICENSE-MIT) or
//!   [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT))
//!
//! at your option.
//!
//! ## Contribution
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in the work by you, as defined in the Apache-2.0 license,
//! shall be dual licensed as above, without any
//! additional terms or conditions.
//!
//! [`str`]: https://doc.rust-lang.org/std/str/index.html
//! [`c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html
//! [`std::print`]: https://doc.rust-lang.org/std/macro.print.html
//! [`std::println`]: https://doc.rust-lang.org/std/macro.println.html
//! [`define_macro`]: macro.define_macro.html
//! [`define_macros`]: macro.define_macros.html
//! [`define_panic_hook`]: macro.define_panic_hook.html
//! [`define_init_panic_hook`]: macro.define_init_panic_hook.html
//! [`WriteStringFn`]: struct.WriteStringFn.html
//! [`FmtWriter`]: struct.FmtWriter.html
//! [`FmtTryWriter`]: struct.FmtTryWriter.html
//! [`ConcatWriter`]: struct.ConcatWriter.html
//! [`ConcatTryWriter`]: struct.ConcatTryWriter.html
//! [`IoWriter`]: struct.IoWriter.html
//! [`IoTryWriter`]: struct.IoTryWriter.html
//! [`web-log`]: https://crates.io/crates/web-log
//! [`wasm-rs-dbg`]: https://crates.io/crates/wasm-rs-dbg
//! [`console_log`]: https://crates.io/crates/console_log
//! [`console_error_panic_hook`]: https://crates.io/crates/console_error_panic_hook
//! [`cargo expand`]: https://crates.io/crates/cargo-expand
//! [textual scope]: https://doc.rust-lang.org/reference/macros-by-example.html#scoping-exporting-and-importing
//! [path-based scope]: https://doc.rust-lang.org/reference/macros-by-example.html#scoping-exporting-and-importing

#![warn(
    clippy::all,
    rust_2018_idioms,
    missing_copy_implementations,
    missing_debug_implementations,
    single_use_lifetimes,
    missing_docs,
    trivial_casts,
    unused_import_braces,
    unused_qualifications,
    unused_results
)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "alloc")]
mod concat_try_writer;
#[cfg(feature = "alloc")]
mod concat_writer;
mod flush;
mod flush_fn;
mod fmt_try_writer;
mod fmt_writer;
mod into_try_write_fn;
mod into_write_fn;
#[cfg(feature = "std")]
mod io_try_writer;
#[cfg(feature = "std")]
mod io_writer;
mod macros;
mod never_error;
mod write_bytes;
mod write_fns;
mod write_str;

#[cfg(feature = "alloc")]
pub use concat_try_writer::{ConcatTryWriter, IntoConcatWriteResult};
#[cfg(feature = "alloc")]
pub use concat_writer::{ConcatWriter, ExpectConcatWriteResult};
pub use flush::Flush;
pub use flush_fn::FlushFn;
pub use fmt_try_writer::{FmtTryWriter, IntoFmtWriteResult};
pub use fmt_writer::{ExpectFmtWriteResult, FmtWriter};
pub use into_try_write_fn::IntoTryWriteFn;
pub use into_write_fn::IntoWriteFn;
#[cfg(feature = "std")]
pub use io_try_writer::{IntoIoFlushResult, IntoIoWriteResult, IoTryWriter};
#[cfg(feature = "std")]
pub use io_writer::{ExpectIoFlushResult, ExpectIoWriteResult, IoWriter};
pub use never_error::NeverError;
pub use write_bytes::WriteBytes;
#[cfg(feature = "alloc")]
pub use write_fns::WriteStringFn;
#[cfg(feature = "std")]
pub use write_fns::{
    TryWriteCCharPtrFn, TryWriteCStrFn, TryWriteCStringFn, WriteCCharPtrFn, WriteCStrFn,
    WriteCStringFn,
};
pub use write_fns::{WriteBytesFn, WriteLenPtrFn, WritePtrLenFn, WriteStrFn};
pub use write_str::{WriteStr, WriteStrAsBytes};