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

use super::*;

type Result<T, E = Error> = ::core::result::Result<T, E>;

pub(in crate)
trait CollectVec : Sized + IntoIterator {
    fn vec (self: Self)
      -> Vec<Self::Item>
    {
        impl<I : IntoIterator> CollectVec for I {}
        self.into_iter().collect()
    }
}

pub(in crate)
trait VMap : Sized + IntoIterator {
    fn vmap<T> (
        self: Self,
        f: impl FnMut(Self::Item) -> T,
    ) -> Vec<T>
    {
        self.into_iter().map(f).collect()
    }

    fn try_vmap<T, E> (
        self: Self,
        f: impl FnMut(Self::Item) -> Result<T, E>
    ) -> Result<Vec<T>, E>
    {
        self.into_iter().map(f).collect()
    }
}

impl<I : ?Sized> VMap for I
where
    Self : Sized + IntoIterator,
{}

pub(in crate)
trait Extend_ {
    fn extend_<A, I> (
        &mut self,
        iterable: I,
    )
    where
        Self : Extend<A>,
        I : IntoIterator<Item = A>,
    {
        impl<T> Extend_ for T {}
        self.extend(iterable)
    }

    fn extend_one_<A> (
        &mut self,
        item: A,
    )
    where
        Self : Extend<A>,
    {
        self.extend([item])
    }
}

pub
trait Also : Sized {
    fn also (mut self, tweak: impl FnOnce(&mut Self))
      -> Self
    {
        impl<T> Also for T {}
        tweak(&mut self);
        self
    }
}

/// Allows to convert a `bool` or an `Option<T>` into a `#( … )*`-usable
/// interpolable (to mock the `$( … )?` from `macro_rules!` macros).
pub
trait Kleene<'r> {
    type Ret;
    fn kleenable (self: &'r Self)
      -> Self::Ret
    ;
}
impl<'r, T : 'r + ToTokens> Kleene<'r> for Option<T> {
    type Ret = &'r [T];
    fn kleenable (self: &'r Option<T>)
      -> &'r [T]
    {
        self.as_ref().map_or(&[], slice::from_ref)
    }
}
// `bool` can be viewed as a `Option<EmptyTs>`.
impl Kleene<'_> for bool {
    type Ret = &'static [EmptyTs];
    fn kleenable (self: &'_ bool)
      -> &'static [EmptyTs]
    {
        if let true = self {
            &[EmptyTs]
        } else {
            &[]
        }
    }
}
pub
struct EmptyTs;
impl ToTokens for EmptyTs {
    fn to_tokens (self: &'_ EmptyTs, _: &mut TokenStream2)
    {}
}