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
//! Crate not intended for direct use.
//! Use https:://docs.rs/lending-iterator instead.
// Templated by `cargo-generate` using https://github.com/danielhenrymantilla/proc-macro-template
#![allow(nonstandard_style, unused_imports)]

use ::core::{
    mem,
    ops::Not as _,
};
use ::proc_macro::{
    TokenStream,
};
use ::proc_macro2::{
    Span,
    TokenStream as TokenStream2,
    TokenTree as TT,
};
use ::quote::{
    format_ident,
    quote,
    quote_spanned,
    ToTokens,
};
use ::syn::{*,
    parse::{Parse, Parser, ParseStream},
    punctuated::Punctuated,
    Result, // Explicitly shadow it
    spanned::Spanned,
};

///
#[proc_macro] pub
fn HKT (
    input: TokenStream,
) -> TokenStream
{
    HKT_impl(input.into())
    //  .map(|ret| { println!("{}", ret); ret })
        .unwrap_or_else(|err| {
            let mut errors =
                err .into_iter()
                    .map(|err| Error::new(
                        err.span(),
                        format_args!("`lending_iterator::HKT!`: {}", err),
                    ))
            ;
            let mut err = errors.next().unwrap();
            errors.for_each(|cur| err.combine(cur));
            err.to_compile_error()
        })
        .into()
}

fn hkt_lifetime(span: Span) -> Lifetime {
    Lifetime::new("'ඞ", span)
}

fn HKT_impl (
    input: TokenStream2,
) -> Result<TokenStream2>
{
    use ::syn::visit_mut;

    let mut input: Type = parse2(input)?;
    visit_mut::VisitMut::visit_type_mut(
        &mut {
            struct ReplaceLifetimeVisitor;
            impl visit_mut::VisitMut for ReplaceLifetimeVisitor {
                fn visit_lifetime_mut (
                    self: &'_ mut Self,
                    lifetime: &'_ mut Lifetime,
                )
                {
                    if lifetime.ident == "_" {
                        *lifetime = hkt_lifetime(lifetime.span());
                    }
                }

                fn visit_type_reference_mut (
                    self: &'_ mut Self,
                    ty_ref: &'_ mut TypeReference,
                )
                {
                    visit_mut::visit_type_reference_mut(self, ty_ref);
                    let and_token = &ty_ref.and_token;
                    ty_ref.lifetime.get_or_insert_with(|| {
                        hkt_lifetime(and_token.span())
                    });
                }

                // Stop uneliding when encountering `fn()` pointer signatures or
                // `Fn` trait bounds.
                fn visit_parenthesized_generic_arguments_mut (
                    self: &'_ mut Self,
                    _: &'_ mut ParenthesizedGenericArguments,
                )
                {
                    /* do not subrecurse: stop visiting! */
                }
                fn visit_type_bare_fn_mut (
                    self: &'_ mut Self,
                    _: &'_ mut TypeBareFn,
                )
                {
                    /* do not subrecurse: stop visiting! */
                }
            }
            ReplaceLifetimeVisitor
        },
        &mut input,
    );
    Ok(input.into_token_stream())
}