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
// Copyright (c) 2021-2022 The Pennsylvania State University and the project contributors.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Implementations of [`LargerOfOp`].

use crate::LargerOfOp;

/// Implementations of [`LargerOfOp`]`<$smaller>*`, `LargerOfOp<$same>*`,
/// and `LargerOfOp<$larger>*` for `$implementor*`.
///
/// The macro assumes that:
///
/// * Types in `$smaller` are smaller than each of the `$implementor`s.
/// * Types in `$same` are the same size as each of the `$implementor`s.
/// * Types in `$larger` are larger than each of the `$implementor`s.
macro_rules! loo_impl {

    // '@ 0' and '@ 1' generate all impls for a single $implementor.
    ( @ 0 $implementor:ty : ( $($smaller:ty)* ) ( $($same:ty)* ) ( $($larger:ty)* ) ) => {
        $(
            impl LargerOfOp<$smaller> for $implementor {
                type Output = $implementor;
            }
        )*

        $(
            impl LargerOfOp<$same> for $implementor {
                type Output = $implementor;
            }
        )*

        $(
            impl LargerOfOp<$larger> for $implementor {
                type Output = $larger;
            }
        )*
    };
    ( @ 1 $attr:meta $implementor:ty : ( $($smaller:ty)* ) ( $($same:ty)* ) ( $($larger:ty)* ) ) => {
        $(
            #[$attr]
            impl LargerOfOp<$smaller> for $implementor {
                type Output = $implementor;
            }
        )*

        $(
            #[$attr]
            impl LargerOfOp<$same> for $implementor {
                type Output = $implementor;
            }
        )*

        $(
            #[$attr]
            impl LargerOfOp<$larger> for $implementor {
                type Output = $larger;
            }
        )*
    };

    // '@ 2' and '@ 3' iterate over each $implementor.
    // This intermediate step is needed as macros don't directly
    // support nested iteration well.
    ( @ 2 $($implementor:ty)* : $smaller:tt $same:tt $larger:tt ) => {
        $( loo_impl! { @ 0 $implementor : $smaller $same $larger } )*
    };
    ( @ 3 $attr:meta $($implementor:ty)* : $smaller:tt $same:tt $larger:tt ) => {
        $( loo_impl! { @ 1 $attr $implementor : $smaller $same $larger } )*
    };

    // The actual branches users of the macro will use:
    ( $($implementor:ty)* : $($smaller:ty)* : $($same:ty)* : $($larger:ty)* ) => {
        loo_impl! { @ 2 $($implementor)* : ( $($smaller)* ) ( $($same)* ) ( $($larger)* ) }
    };
    ( @ $attr:meta $($implementor:ty)* : $($smaller:ty)* : $($same:ty)* : $($larger:ty)* ) => {
        loo_impl! { @ 3 $attr $($implementor)* : ( $($smaller)* ) ( $($same)* ) ( $($larger)* ) }
    };
}

loo_impl! { u8 i8     : : u8 i8 : u16 i16 u32 i32 u64 i64 u128 i128 }
loo_impl! { u16 i16   : u8 i8 : u16 i16 : u32 i32 u64 i64 u128 i128 }
loo_impl! { u32 i32   : u8 i8 u16 i16 : u32 i32 : u64 i64 u128 i128 }
loo_impl! { u64 i64   : u8 i8 u16 i16 u32 i32 : u64 i64 : u128 i128 }
loo_impl! { u128 i128 : u8 i8 u16 i16 u32 i32 u64 i64 : u128 i128 : }

loo_impl! { usize isize : : usize isize : }

loo_impl! {
    @ cfg(target_pointer_width = "16")
    usize isize : u8 i8 : u16 i16 : u32 i32 u64 i64 u128 i128
}
loo_impl! {
    @ cfg(target_pointer_width = "16")
    u8 i8 : : : usize isize
}
loo_impl! {
    @ cfg(target_pointer_width = "16")
    u16 i16 : : usize isize :
}
loo_impl! {
    @ cfg(target_pointer_width = "16")
    u32 i32 u64 i64 u128 i128 : usize isize : :
}

loo_impl! {
    @ cfg(target_pointer_width = "32")
    usize isize : u8 i8 u16 i16 : u32 i32 : u64 i64 u128 i128
}
loo_impl! {
    @ cfg(target_pointer_width = "32")
    u8 i8 u16 i16 : : : usize isize
}
loo_impl! {
    @ cfg(target_pointer_width = "32")
     u32 i32 : : usize isize :
}
loo_impl! {
    @ cfg(target_pointer_width = "32")
    u64 i64 u128 i128 : usize isize : :
}

loo_impl! {
    @ cfg(target_pointer_width = "64")
    usize isize : u8 i8 u16 i16 u32 i32 : u64 i64 : u128 i128
}
loo_impl! {
    @ cfg(target_pointer_width = "64")
    u8 i8 u16 i16 u32 i32 : : : usize isize
}
loo_impl! {
    @ cfg(target_pointer_width = "64")
    u64 i64 : : usize isize :
}
loo_impl! {
    @ cfg(target_pointer_width = "64")
    u128 i128 : usize isize : :
}