printf_wrap/
larger_of.rs

1// Copyright (c) 2021-2023 The Pennsylvania State University and the project contributors.
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Implementations of [`LargerOfOp`].
5
6use crate::LargerOfOp;
7
8/// Implementations of [`LargerOfOp`]`<$smaller>*`, `LargerOfOp<$same>*`,
9/// and `LargerOfOp<$larger>*` for `$implementor*`.
10///
11/// The macro assumes that:
12///
13/// * Types in `$smaller` are smaller than each of the `$implementor`s.
14/// * Types in `$same` are the same size as each of the `$implementor`s.
15/// * Types in `$larger` are larger than each of the `$implementor`s.
16macro_rules! loo_impl {
17
18    // '@ 0' and '@ 1' generate all impls for a single $implementor.
19    ( @ 0 $implementor:ty : ( $($smaller:ty)* ) ( $($same:ty)* ) ( $($larger:ty)* ) ) => {
20        $(
21            impl LargerOfOp<$smaller> for $implementor {
22                type Output = $implementor;
23            }
24        )*
25
26        $(
27            impl LargerOfOp<$same> for $implementor {
28                type Output = $implementor;
29            }
30        )*
31
32        $(
33            impl LargerOfOp<$larger> for $implementor {
34                type Output = $larger;
35            }
36        )*
37    };
38    ( @ 1 $attr:meta $implementor:ty : ( $($smaller:ty)* ) ( $($same:ty)* ) ( $($larger:ty)* ) ) => {
39        $(
40            #[$attr]
41            impl LargerOfOp<$smaller> for $implementor {
42                type Output = $implementor;
43            }
44        )*
45
46        $(
47            #[$attr]
48            impl LargerOfOp<$same> for $implementor {
49                type Output = $implementor;
50            }
51        )*
52
53        $(
54            #[$attr]
55            impl LargerOfOp<$larger> for $implementor {
56                type Output = $larger;
57            }
58        )*
59    };
60
61    // '@ 2' and '@ 3' iterate over each $implementor.
62    // This intermediate step is needed as macros don't directly
63    // support nested iteration well.
64    ( @ 2 $($implementor:ty)* : $smaller:tt $same:tt $larger:tt ) => {
65        $( loo_impl! { @ 0 $implementor : $smaller $same $larger } )*
66    };
67    ( @ 3 $attr:meta $($implementor:ty)* : $smaller:tt $same:tt $larger:tt ) => {
68        $( loo_impl! { @ 1 $attr $implementor : $smaller $same $larger } )*
69    };
70
71    // The actual branches users of the macro will use:
72    ( $($implementor:ty)* : $($smaller:ty)* : $($same:ty)* : $($larger:ty)* ) => {
73        loo_impl! { @ 2 $($implementor)* : ( $($smaller)* ) ( $($same)* ) ( $($larger)* ) }
74    };
75    ( @ $attr:meta $($implementor:ty)* : $($smaller:ty)* : $($same:ty)* : $($larger:ty)* ) => {
76        loo_impl! { @ 3 $attr $($implementor)* : ( $($smaller)* ) ( $($same)* ) ( $($larger)* ) }
77    };
78}
79
80loo_impl! { u8 i8     : : u8 i8 : u16 i16 u32 i32 u64 i64 u128 i128 }
81loo_impl! { u16 i16   : u8 i8 : u16 i16 : u32 i32 u64 i64 u128 i128 }
82loo_impl! { u32 i32   : u8 i8 u16 i16 : u32 i32 : u64 i64 u128 i128 }
83loo_impl! { u64 i64   : u8 i8 u16 i16 u32 i32 : u64 i64 : u128 i128 }
84loo_impl! { u128 i128 : u8 i8 u16 i16 u32 i32 u64 i64 : u128 i128 : }
85
86loo_impl! { usize isize : : usize isize : }
87
88loo_impl! {
89    @ cfg(target_pointer_width = "8")
90    usize isize : : u8 i8 : u16 i16 u32 i32 u64 i64 u128 i128
91}
92loo_impl! {
93    @ cfg(target_pointer_width = "8")
94    u8 i8 : : usize isize :
95}
96loo_impl! {
97    @ cfg(target_pointer_width = "8")
98    u16 i16 u32 i32 u64 i64 u128 i128 : usize isize : :
99}
100
101loo_impl! {
102    @ cfg(target_pointer_width = "16")
103    usize isize : u8 i8 : u16 i16 : u32 i32 u64 i64 u128 i128
104}
105loo_impl! {
106    @ cfg(target_pointer_width = "16")
107    u8 i8 : : : usize isize
108}
109loo_impl! {
110    @ cfg(target_pointer_width = "16")
111    u16 i16 : : usize isize :
112}
113loo_impl! {
114    @ cfg(target_pointer_width = "16")
115    u32 i32 u64 i64 u128 i128 : usize isize : :
116}
117
118loo_impl! {
119    @ cfg(target_pointer_width = "32")
120    usize isize : u8 i8 u16 i16 : u32 i32 : u64 i64 u128 i128
121}
122loo_impl! {
123    @ cfg(target_pointer_width = "32")
124    u8 i8 u16 i16 : : : usize isize
125}
126loo_impl! {
127    @ cfg(target_pointer_width = "32")
128     u32 i32 : : usize isize :
129}
130loo_impl! {
131    @ cfg(target_pointer_width = "32")
132    u64 i64 u128 i128 : usize isize : :
133}
134
135loo_impl! {
136    @ cfg(target_pointer_width = "64")
137    usize isize : u8 i8 u16 i16 u32 i32 : u64 i64 : u128 i128
138}
139loo_impl! {
140    @ cfg(target_pointer_width = "64")
141    u8 i8 u16 i16 u32 i32 : : : usize isize
142}
143loo_impl! {
144    @ cfg(target_pointer_width = "64")
145    u64 i64 : : usize isize :
146}
147loo_impl! {
148    @ cfg(target_pointer_width = "64")
149    u128 i128 : usize isize : :
150}
151
152loo_impl! {
153    @ cfg(target_pointer_width = "128")
154    usize isize : u8 i8 u16 i16 u32 i32 u64 i64 : u128 i128 :
155}
156loo_impl! {
157    @ cfg(target_pointer_width = "128")
158    u8 i8 u16 i16 u32 i32 u64 i64 : : : usize isize
159}
160loo_impl! {
161    @ cfg(target_pointer_width = "128")
162    u128 i128 : : usize isize :
163}