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
#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
#[cfg(feature = "std")]
use std::ffi::{CStr, CString, NulError};
#[cfg(feature = "std")]
use std::os::raw::c_char;

use crate::{WriteBytes, WriteStr, WriteStrAsBytes};

macro_rules! with_docs {
    ( docs: { $( $doc:expr ),* $(,)? }, item: { $item:item } ) => {
        $( #[doc = $doc] )*
        $item
    };
}

macro_rules! define_write_fn {
    (
        $name:ident,
        ($($params:ty),*),
        $writes:literal,
        $into_write_fns:literal
    ) => {
        with_docs!(
            docs: {
                ::core::concat!(
                    "A wrapper for write functions `for<R> FnMut(",
                    ::core::stringify!($($params),*),
                    ") -> R`."
                ),
                "",
                ::core::concat!(
                    "It implements ",
                    $writes,
                    " and can be used in conjunction with ",
                    $into_write_fns,
                    " to simplify type inference."
                ),
                "",
                "[`IntoWriteFn`]: trait.IntoWriteFn.html",
                "[`IntoTryWriteFn`]: trait.IntoTryWriteFn.html"
            },
            item: {
                #[derive(Clone, Copy, Debug, Eq, PartialEq)]
                pub struct $name<F, R>(F)
                where
                    F: FnMut($($params),*) -> R;
            }
        );

        impl<F, R> $name<F, R>
        where
            F: FnMut($($params),*) -> R,
        {
            with_docs!(
                docs: {
                    ::core::concat!(
                        "Creates a new `",
                        ::core::stringify!($name),
                        "` containing the given closure or function."
                    )
                },
                item: {
                    pub fn new(closure: F) -> Self {
                        Self(closure)
                    }
                }
            );
        }
    };
}

macro_rules! define_write_str_fn {
    ($name:ident, ($($params:ty),*), $buf:ident => ($($args:tt)*)) => {
        define_write_fn!(
            $name,
            ($($params),*),
            "[`WriteStr`] trait",
            "[`IntoWriteFn`] and [`IntoTryWriteFn`] traits"
        );

        impl<F, R> WriteStr for $name<F, R>
        where
            F: FnMut($($params),*) -> R,
        {
            type Output = R;

            fn write_str(&mut self, $buf: &str) -> Self::Output {
                self.0($($args)*)
            }
        }
    };
}

macro_rules! define_write_bytes_fn {
    ($name:ident, ($($params:ty),*), $buf:ident => ($($args:tt)*)) => {
        define_write_fn!(
            $name,
            ($($params),*),
            "[`WriteBytes`] and [`WriteStr`] traits",
            "[`IntoWriteFn`] and [`IntoTryWriteFn`] traits"
        );

        impl<F, R> WriteBytes for $name<F, R>
        where
            F: FnMut($($params),*) -> R,
        {
            type Output = R;

            fn write_bytes(&mut self, $buf: &[u8]) -> Self::Output {
                self.0($($args)*)
            }
        }

        impl<F, R> WriteStrAsBytes for $name<F, R> where F: FnMut($($params),*) -> R {}
    };
}

#[cfg(feature = "std")]
macro_rules! define_write_cstr_fn {
    ($name:ident, ($($params:ty),*), $cstr:ident => ($($args:tt)*)) => {
        define_write_fn!(
            $name,
            ($($params),*),
            "[`WriteBytes`] and [`WriteStr`] traits",
            "[`IntoWriteFn`] trait"
        );

        impl<F, R> WriteBytes for $name<F, R>
        where
            F: FnMut($($params),*) -> R,
        {
            type Output = R;

            fn write_bytes(&mut self, buf: &[u8]) -> Self::Output {
                let $cstr = match CString::new(buf.as_ref()) {
                    Ok(ok) => ok,
                    Err(err) => panic!(
                        "nul byte found in provided data at position: {}, buffer: {:?}",
                        err.nul_position(),
                        buf
                    ),
                };
                self.0($($args)*)
            }
        }

        impl<F, R> WriteStrAsBytes for $name<F, R> where F: FnMut($($params),*) -> R {}
    };
}

#[cfg(feature = "std")]
macro_rules! define_try_write_cstr_fn {
    ($name:ident, ($($params:ty),*), $cstr:ident => ($($args:tt)*)) => {
        define_write_fn!(
            $name,
            ($($params),*),
            "[`WriteBytes`] and [`WriteStr`] traits",
            "[`IntoTryWriteFn`] trait"
        );

        impl<F, R> WriteBytes for $name<F, R>
        where
            F: FnMut($($params),*) -> R,
        {
            type Output = Result<R, NulError>;

            fn write_bytes(&mut self, buf: &[u8]) -> Self::Output {
                CString::new(buf.as_ref()).map(|$cstr| self.0($($args)*))
            }
        }

        impl<F, R> WriteStrAsBytes for $name<F, R>
        where
            F: FnMut($($params),*) -> R {}
    };
}

define_write_bytes_fn!(WritePtrLenFn, (*const u8, usize), buf => (buf.as_ptr(), buf.len()));
define_write_bytes_fn!(WriteLenPtrFn, (usize, *const u8), buf => (buf.len(), buf.as_ptr()));
define_write_bytes_fn!(WriteBytesFn, (&[u8]), buf => (buf));

define_write_str_fn!(WriteStrFn, (&str), buf => (buf));
#[cfg(feature = "alloc")]
define_write_str_fn!(WriteStringFn, (String), buf => (buf.to_string()));

#[cfg(feature = "std")]
define_write_cstr_fn!(WriteCStrFn, (&CStr), cstr => (cstr.as_c_str()));
#[cfg(feature = "std")]
define_write_cstr_fn!(WriteCStringFn, (CString), cstr => (cstr));
#[cfg(feature = "std")]
define_write_cstr_fn!(WriteCCharPtrFn, (*const c_char), cstr => (cstr.as_ptr()));

#[cfg(feature = "std")]
define_try_write_cstr_fn!(TryWriteCStrFn, (&CStr), cstr => (cstr.as_c_str()));
#[cfg(feature = "std")]
define_try_write_cstr_fn!(TryWriteCStringFn, (CString), cstr => (cstr));
#[cfg(feature = "std")]
define_try_write_cstr_fn!(TryWriteCCharPtrFn, (*const c_char), cstr => (cstr.as_ptr()));