Skip to content

Commit d6ee48c

Browse files
committed
feat(either_of): Add Either! convenience macro
1 parent 1446c31 commit d6ee48c

File tree

1 file changed

+114
-15
lines changed

1 file changed

+114
-15
lines changed

either_of/src/lib.rs

Lines changed: 114 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -942,44 +942,119 @@ macro_rules! either {
942942
};
943943
}
944944

945+
/// Convenience macro for refering to `Either` types by listing their variants.
946+
///
947+
/// # Example
948+
///
949+
/// ```no_run
950+
/// use either_of::Either;
951+
///
952+
/// let _: either_of::EitherOf3<u8, i8, u32> = <Either!(u8, i8, u32)>::A(0);
953+
/// ```
954+
/// A single type parameter equates to its value:
955+
/// ```no_run
956+
/// # use either_of::Either;
957+
/// let a: Either!(i32) = 0;
958+
/// let _: i32 = a;
959+
/// ```
960+
#[macro_export]
961+
macro_rules! Either { // TODO: add `() => {!}` branch when the "never" type gets stabilized
962+
($A:ty$(,)?) => {
963+
$A
964+
};
965+
($A:ty, $B:ty$(,)?) => {
966+
$crate::Either<$A, $B>
967+
};
968+
($A:ty, $B:ty, $C:ty$(,)?) => {
969+
$crate::EitherOf3<$A, $B, $C>
970+
};
971+
($A:ty, $B:ty, $C:ty, $D:ty$(,)?) => {
972+
$crate::EitherOf4<$A, $B, $C, $D>
973+
};
974+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty$(,)?) => {
975+
$crate::EitherOf5<$A, $B, $C, $D, $E>
976+
};
977+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty$(,)?) => {
978+
$crate::EitherOf6<$A, $B, $C, $D, $E, $F>
979+
};
980+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty$(,)?) => {
981+
$crate::EitherOf7<$A, $B, $C, $D, $E, $F, $G>
982+
};
983+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty$(,)?) => {
984+
$crate::EitherOf8<$A, $B, $C, $D, $E, $F, $G, $H>
985+
};
986+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty$(,)?) => {
987+
$crate::EitherOf9<$A, $B, $C, $D, $E, $F, $G, $H, $I>
988+
};
989+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty$(,)?) => {
990+
$crate::EitherOf10<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J>
991+
};
992+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty$(,)?) => {
993+
$crate::EitherOf11<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K>
994+
};
995+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty, $L:ty$(,)?) => {
996+
$crate::EitherOf12<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L>
997+
};
998+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty, $L:ty, $M:ty$(,)?) => {
999+
$crate::EitherOf13<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M>
1000+
};
1001+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty, $L:ty, $M:ty, $N:ty$(,)?) => {
1002+
$crate::EitherOf14<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N>
1003+
};
1004+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty, $L:ty, $M:ty, $N:ty, $O:ty$(,)?) => {
1005+
$crate::EitherOf15<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, $O>
1006+
};
1007+
($A:ty, $B:ty, $C:ty, $D:ty, $E:ty, $F:ty, $G:ty, $H:ty, $I:ty, $J:ty, $K:ty, $L:ty, $M:ty, $N:ty, $O:ty, $P:ty$(,)?) => {
1008+
$crate::EitherOf16<$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, $O, $P>
1009+
};
1010+
}
1011+
9451012
#[cfg(test)]
9461013
mod tests {
9471014
use super::*;
9481015

9491016
// compile time test
9501017
#[test]
1018+
#[expect(clippy::type_complexity)]
9511019
fn either_macro() {
952-
let _: Either<&str, f64> = either!(12,
1020+
let a: f64 = 0.0;
1021+
let _: Either!(f64) = a;
1022+
let a: Either<&str, f64> = either!(12,
9531023
12 => "12",
9541024
_ => 0.0f64,
9551025
);
956-
let _: EitherOf3<&str, f64, i32> = either!(12,
1026+
let _: Either!(&str, f64) = a;
1027+
let a: EitherOf3<&str, f64, i32> = either!(12,
9571028
12 => "12",
9581029
13 => 0.0f64,
9591030
_ => 12i32,
9601031
);
961-
let _: EitherOf4<&str, f64, char, i32> = either!(12,
1032+
let _: Either!(&str, f64, i32) = a;
1033+
let a: EitherOf4<&str, f64, char, i32> = either!(12,
9621034
12 => "12",
9631035
13 => 0.0f64,
9641036
14 => ' ',
9651037
_ => 12i32,
9661038
);
967-
let _: EitherOf5<&str, f64, char, f32, i32> = either!(12,
1039+
let _: Either!(&str, f64, char, i32) = a;
1040+
let a: EitherOf5<&str, f64, char, f32, i32> = either!(12,
9681041
12 => "12",
9691042
13 => 0.0f64,
9701043
14 => ' ',
9711044
15 => 0.0f32,
9721045
_ => 12i32,
9731046
);
974-
let _: EitherOf6<&str, f64, char, f32, u8, i32> = either!(12,
1047+
let _: Either!(&str, f64, char, f32, i32) = a;
1048+
let a: EitherOf6<&str, f64, char, f32, u8, i32> = either!(12,
9751049
12 => "12",
9761050
13 => 0.0f64,
9771051
14 => ' ',
9781052
15 => 0.0f32,
9791053
16 => 24u8,
9801054
_ => 12i32,
9811055
);
982-
let _: EitherOf7<&str, f64, char, f32, u8, i8, i32> = either!(12,
1056+
let _: Either!(&str, f64, char, f32, u8, i32) = a;
1057+
let a: EitherOf7<&str, f64, char, f32, u8, i8, i32> = either!(12,
9831058
12 => "12",
9841059
13 => 0.0f64,
9851060
14 => ' ',
@@ -988,7 +1063,8 @@ mod tests {
9881063
17 => 2i8,
9891064
_ => 12i32,
9901065
);
991-
let _: EitherOf8<&str, f64, char, f32, u8, i8, u16, i32> = either!(12,
1066+
let _: Either!(&str, f64, char, f32, u8, i8, i32) = a;
1067+
let a: EitherOf8<&str, f64, char, f32, u8, i8, u16, i32> = either!(12,
9921068
12 => "12",
9931069
13 => 0.0f64,
9941070
14 => ' ',
@@ -998,7 +1074,8 @@ mod tests {
9981074
18 => 42u16,
9991075
_ => 12i32,
10001076
);
1001-
let _: EitherOf9<&str, f64, char, f32, u8, i8, u16, i16, i32> = either!(12,
1077+
let _: Either!(&str, f64, char, f32, u8, i8, u16, i32) = a;
1078+
let a: EitherOf9<&str, f64, char, f32, u8, i8, u16, i16, i32> = either!(12,
10021079
12 => "12",
10031080
13 => 0.0f64,
10041081
14 => ' ',
@@ -1009,7 +1086,8 @@ mod tests {
10091086
19 => 64i16,
10101087
_ => 12i32,
10111088
);
1012-
let _: EitherOf10<&str, f64, char, f32, u8, i8, u16, i16, u32, i32> = either!(12,
1089+
let _: Either!(&str, f64, char, f32, u8, i8, u16, i16, i32) = a;
1090+
let a: EitherOf10<&str, f64, char, f32, u8, i8, u16, i16, u32, i32> = either!(12,
10131091
12 => "12",
10141092
13 => 0.0f64,
10151093
14 => ' ',
@@ -1021,7 +1099,8 @@ mod tests {
10211099
20 => 84u32,
10221100
_ => 12i32,
10231101
);
1024-
let _: EitherOf11<
1102+
let _: Either!(&str, f64, char, f32, u8, i8, u16, i16, u32, i32) = a;
1103+
let a: EitherOf11<
10251104
&str,
10261105
f64,
10271106
char,
@@ -1046,7 +1125,9 @@ mod tests {
10461125
21 => 12i32,
10471126
_ => 13u64,
10481127
);
1049-
let _: EitherOf12<
1128+
let _: Either!(&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64,) =
1129+
a;
1130+
let a: EitherOf12<
10501131
&str,
10511132
f64,
10521133
char,
@@ -1073,7 +1154,10 @@ mod tests {
10731154
22 => 13u64,
10741155
_ => 14i64,
10751156
);
1076-
let _: EitherOf13<
1157+
let _: Either!(
1158+
&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64, i64
1159+
) = a;
1160+
let a: EitherOf13<
10771161
&str,
10781162
f64,
10791163
char,
@@ -1102,7 +1186,10 @@ mod tests {
11021186
23 => 14i64,
11031187
_ => 15u128,
11041188
);
1105-
let _: EitherOf14<
1189+
let _: Either!(
1190+
&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64, i64, u128,
1191+
) = a;
1192+
let a: EitherOf14<
11061193
&str,
11071194
f64,
11081195
char,
@@ -1133,7 +1220,11 @@ mod tests {
11331220
24 => 15u128,
11341221
_ => 16i128,
11351222
);
1136-
let _: EitherOf15<
1223+
let _: Either!(
1224+
&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64, i64, u128,
1225+
i128,
1226+
) = a;
1227+
let a: EitherOf15<
11371228
&str,
11381229
f64,
11391230
char,
@@ -1166,7 +1257,11 @@ mod tests {
11661257
25 => 16i128,
11671258
_ => [1u8],
11681259
);
1169-
let _: EitherOf16<
1260+
let _: Either!(
1261+
&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64, i64, u128,
1262+
i128, [u8; 1],
1263+
) = a;
1264+
let a: EitherOf16<
11701265
&str,
11711266
f64,
11721267
char,
@@ -1201,6 +1296,10 @@ mod tests {
12011296
26 => [1u8],
12021297
_ => [1i8],
12031298
);
1299+
let _: Either!(
1300+
&str, f64, char, f32, u8, i8, u16, i16, u32, i32, u64, i64, u128,
1301+
i128, [u8; 1], [i8; 1],
1302+
) = a;
12041303
}
12051304

12061305
#[test]

0 commit comments

Comments
 (0)