custom_print/
into_try_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;
10#[cfg(feature = "std")]
11use crate::{TryWriteCCharPtrFn, TryWriteCStrFn, TryWriteCStringFn};
12use crate::{WriteBytesFn, WriteLenPtrFn, WritePtrLenFn, WriteStrFn};
13
14/// A trait used to inference type of fallible write closure wrapper.
15///
16/// This trait used by [`FmtTryWriter`], [`ConcatTryWriter`] and [`IoTryWriter`].
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 non-panicking versions for
21/// closures with [`&CStr`], [`CString`], and [`*const c_char`] arguments.
22///
23/// [`&str`]: https://doc.rust-lang.org/std/str/index.html
24/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
25/// [`&CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
26/// [`CString`]: https://doc.rust-lang.org/std/ffi/struct.CString.html
27/// [`*const c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html
28/// [`IntoWriteFn`]: trait.IntoWriteFn.html
29/// [`FmtTryWriter`]: struct.FmtTryWriter.html
30/// [`ConcatTryWriter`]: struct.ConcatTryWriter.html
31/// [`IoTryWriter`]: struct.IoTryWriter.html
32pub trait IntoTryWriteFn<Ts> {
33    /// The corresponding fallible write function wrapper.
34    type TryWriteFn;
35
36    /// Returns the wrapped function.
37    fn into_try_write_fn(self) -> Self::TryWriteFn;
38}
39
40macro_rules! def {
41    ( ($F:tt, $R:tt), $func:ty, ($($ty:ty),*) ) => {
42        impl<$F, $R> IntoTryWriteFn<($($ty,)*)> for $F
43        where
44            $F: FnMut($($ty),*) -> $R,
45        {
46            type TryWriteFn = $func;
47            fn into_try_write_fn(self) -> Self::TryWriteFn {
48                Self::TryWriteFn::new(self)
49            }
50        }
51
52    }
53}
54
55def!((F, R), WritePtrLenFn<F, R>, (*const u8, usize));
56def!((F, R), WriteLenPtrFn<F, R>, (usize, *const u8));
57def!((F, R), WriteBytesFn<F, R>, (&[u8]));
58
59def!((F, R), WriteStrFn<F, R>, (&str));
60#[cfg(feature = "alloc")]
61def!((F, R), WriteStringFn<F, R>, (String));
62
63#[cfg(feature = "std")]
64def!((F, R), TryWriteCStrFn<F, R>, (&CStr));
65#[cfg(feature = "std")]
66def!((F, R), TryWriteCStringFn<F, R>, (CString));
67#[cfg(feature = "std")]
68def!((F, R), TryWriteCCharPtrFn<F, R>, (*const c_char));