custom_print/
into_write_fn.rs

1#[cfg(feature = "alloc")]
2use alloc::string::String;
3#[cfg(feature = "std")]
4use std::ffi::{CStr, CString};
5#[cfg(feature = "std")]
6use std::os::raw::c_char;
7
8#[cfg(feature = "alloc")]
9use crate::WriteStringFn;
10use crate::{WriteBytesFn, WriteLenPtrFn, WritePtrLenFn, WriteStrFn};
11#[cfg(feature = "std")]
12use crate::{WriteCCharPtrFn, WriteCStrFn, WriteCStringFn};
13
14/// A trait used to inference type of write closure wrapper.
15///
16/// This trait used by [`FmtWriter`], [`ConcatWriter`] and [`IoWriter`].
17///
18/// Both `IntoWriteFn` and [`IntoTryWriteFn`] traits provides the same wrappers for
19/// closures with `*const u8`, `usize`, `&[u8]`, [`&str`] and [`String`] arguments.
20/// This variant uses panicking versions for
21/// closures with [`&CStr`], [`CString`], and [`*const c_char`] arguments,
22/// for a "fail fast" approach.
23///
24/// [`&str`]: https://doc.rust-lang.org/std/str/index.html
25/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
26/// [`&CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
27/// [`CString`]: https://doc.rust-lang.org/std/ffi/struct.CString.html
28/// [`*const c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html
29/// [`IntoTryWriteFn`]: trait.IntoTryWriteFn.html
30/// [`FmtWriter`]: struct.FmtWriter.html
31/// [`ConcatWriter`]: struct.ConcatWriter.html
32/// [`IoWriter`]: struct.IoWriter.html
33pub trait IntoWriteFn<Ts> {
34    /// The corresponding write function wrapper.
35    type WriteFn;
36
37    /// Returns the wrapped function.
38    fn into_write_fn(self) -> Self::WriteFn;
39}
40
41macro_rules! def {
42    ( ($F:tt, $R:tt), $func:ty, ($($ty:ty),*) ) => {
43        impl<$F, $R> IntoWriteFn<($($ty,)*)> for $F
44        where
45            $F: FnMut($($ty),*) -> $R,
46        {
47            type WriteFn = $func;
48            fn into_write_fn(self) -> Self::WriteFn {
49                Self::WriteFn::new(self)
50            }
51        }
52
53    }
54}
55
56def!((F, R), WritePtrLenFn<F, R>, (*const u8, usize));
57def!((F, R), WriteLenPtrFn<F, R>, (usize, *const u8));
58def!((F, R), WriteBytesFn<F, R>, (&[u8]));
59
60def!((F, R), WriteStrFn<F, R>, (&str));
61#[cfg(feature = "alloc")]
62def!((F, R), WriteStringFn<F, R>, (String));
63
64#[cfg(feature = "std")]
65def!((F, R), WriteCStrFn<F, R>, (&CStr));
66#[cfg(feature = "std")]
67def!((F, R), WriteCStringFn<F, R>, (CString));
68#[cfg(feature = "std")]
69def!((F, R), WriteCCharPtrFn<F, R>, (*const c_char));