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
#![cfg_attr(rustfmt, rustfmt::skip)]
#![allow(unused)]
#![warn(unused_must_use)]

use super::*;

pub(in crate) use extension_traits::*;
mod extension_traits;

pub(in crate) use macros::*;
mod macros;

pub(in crate) use mb_file_expanded::*;
mod mb_file_expanded;

pub(in crate) use trait_impl_shenanigans::*;
mod trait_impl_shenanigans;

pub(in crate)
trait MySplit {
    type Ret;
    fn my_split (self: &'_ Self)
      -> Self::Ret
    ;
}

impl MySplit for Generics {
    type Ret = (TokenStream2, Vec<WherePredicate>);

    fn my_split (self: &'_ Generics)
      -> Self::Ret
    {
        let cap = self.params.iter().len();
        let mut lts = Vec::with_capacity(cap);
        let mut tys = Vec::with_capacity(cap);
        let mut predicates =
            self.split_for_impl()
                .2
                .map_or(vec![], |wc| wc.predicates.iter().cloned().collect())
        ;
        self.params
            .iter()
            .cloned()
            .for_each(|it| match it {
                | GenericParam::Type(mut ty) => {
                    let ty_param = &ty.ident;
                    ::core::mem::take(&mut ty.bounds)
                        .into_iter()
                        .for_each(|bound: TypeParamBound| {
                            predicates.push(parse_quote! {
                                #ty_param : #bound
                            });
                        })
                    ;
                    tys.push(ty);
                },
                | GenericParam::Lifetime(mut lt) => {
                    let lt_param = &lt.lifetime;
                    ::core::mem::take(&mut lt.bounds)
                        .into_iter()
                        .for_each(|bound: Lifetime| {
                            predicates.push(parse_quote! {
                                #lt_param : #bound
                            });
                        })
                    ;
                    lts.push(lt);
                },
                | GenericParam::Const(_) => {
                    unimplemented!("const generics")
                },
            })
        ;
        (
            quote!(
                #(#lts ,)*
                #(#tys),*
            ),

            predicates
        )
    }
}

#[cfg(any())] /* Comment to enable (requires `cargo add bat`) */
pub(in crate)
fn pretty_print_tokenstream (
    code: &'_ TokenStream,
    fname: &'_ str,
)
{
    fn try_format (input: &'_ str)
      -> Option<String>
    {Some({
        let mut child =
            ::std::process::Command::new("rustfmt")
                .stdin(::std::process::Stdio::piped())
                .stdout(::std::process::Stdio::piped())
                .stderr(::std::process::Stdio::piped())
                .spawn()
                .ok()?
        ;
        match child.stdin.take().unwrap() { ref mut stdin => {
            ::std::io::Write::write_all(stdin, input.as_bytes()).ok()?;
        }}
        let mut stdout = String::new();
        ::std::io::Read::read_to_string(
            &mut child.stdout.take().unwrap(),
            &mut stdout,
        ).ok()?;
        if child.wait().ok()?.success().not() { return None; }
        stdout
    })}

    if  ::std::env::var("SAFER_FFI_DEBUG_FILTER")
            .ok()
            .map_or(true, |ref filter| fname.contains(filter))
    {
        if let Some(ref formatted) = try_format(&code.to_string()) {
            // It's formatted, now let's try to also colorize it:
            if  ::bat::PrettyPrinter::new()
                    .input_from_bytes(formatted.as_ref())
                    .language("rust")
                    .true_color(false)
                    .print()
                    .is_err()
            {
                // Fallback to non-colorized-but-formatted output.
                println!("{}", formatted);
            }
        } else {
            // Fallback to raw output.
            println!("{}", code);
        }
    }
}

pub(in crate)
struct RemapNonStaticLifetimesTo<'__> {
    pub(in crate)
    new_lt_name: &'__ str,
}

impl ::syn::visit_mut::VisitMut
    for RemapNonStaticLifetimesTo<'_>
{
    fn visit_lifetime_mut (
        self: &'_ mut Self,
        lifetime: &'_ mut Lifetime,
    )
    {
        if lifetime.ident != "static" {
            lifetime.ident = Ident::new(
                self.new_lt_name,
                lifetime.ident.span(),
            );
        }
    }

    fn visit_type_reference_mut (
        self: &'_ mut Self,
        ty_ref: &'_ mut TypeReference,
    )
    {
        // 1 – sub-recurse
        visit_mut::visit_type_reference_mut(self, ty_ref);
        // 2 – handle the implicitly elided case.
        if ty_ref.lifetime.is_none() {
            ty_ref.lifetime = Some(Lifetime::new(
                &["'", self.new_lt_name].concat(),
                ty_ref.and_token.span,
            ));
        }
    }

    fn visit_parenthesized_generic_arguments_mut (
        self: &'_ mut Self,
        _: &'_ mut ParenthesizedGenericArguments,
    )
    {
        // Elided lifetimes in `fn(…)` or `Fn…(…)` are higher order:
        /* do not subrecurse */
    }
}

pub(in crate)
fn compile_warning (
    span: &dyn ToTokens,
    message: &str,
) -> TokenStream2
{
    let mut spans = span.to_token_stream().into_iter().map(|tt| tt.span());
    let fst = spans.next().unwrap_or_else(Span::call_site);
    let lst = spans.fold(fst, |_, cur| cur);
    let ref message = ["\n", message].concat();
    let warning = Ident::new("warning", fst);
    quote_spanned!(lst=>
        #[allow(nonstandard_style, clippy::all)]
        const _: () = {
            #[allow(nonstandard_style)]
            struct safer_ffi_ {
                #[deprecated(note = #message)]
                #warning: ()
            }
            //                     fst    lst
            let _ = safer_ffi_ { #warning: () };
            //                   ^^^^^^^^^^^^
        };
    )
}

pub(in crate)
fn extract_docs (
    attrs: &'_ [Attribute]
) -> Result<Vec<Expr>>
{
    attrs.iter().filter_map(|attr| {
        attr.path
            .is_ident("doc")
            .then(|| Parser::parse2(
                |input: ParseStream<'_>| Ok(
                    if input.peek(Token![=]) {
                        let _: Token![=] = input.parse::<Token![=]>().unwrap();
                        let doc_str: Expr = input.parse()?;
                        let _: Option<Token![,]> = input.parse()?;
                        Some(doc_str)
                    } else {
                        let _ = input.parse::<TokenStream2>();
                        None
                    }
                ),
                attr.tokens.clone(),
            )
            .transpose())
            .flatten()
        })
        .collect()
}

pub(crate)
struct LazyQuote(
    pub(crate) fn() -> TokenStream2,
    pub(crate) ::core::cell::RefCell<Option<TokenStream2>>,
);

impl ::quote::ToTokens for LazyQuote {
    fn to_tokens (self: &'_ LazyQuote, tokens: &'_ mut TokenStream2)
    {
        self.1
            .borrow_mut()
            .get_or_insert_with(self.0)
            .to_tokens(tokens)
    }
}

pub(in crate)
fn parenthesized<T> (
    input: ParseStream<'_>,
    scope: impl FnOnce(token::Paren, ParseStream<'_>) -> Result<T>,
) -> Result<T>
{
    let contents;
    scope(parenthesized!(contents in input), &contents)
}