Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rlp = ["alloy-primitives/rlp", "dep:alloy-rlp"]
serde = ["alloy-primitives/serde"]
k256 = ["alloy-primitives/k256"]
eip712 = ["alloy-sol-types?/eip712-serde", "alloy-dyn-abi?/eip712"]
more-tuple-impls = ["alloy-sol-types/more-tuple-impls"]

postgres = ["std", "alloy-primitives/postgres"]
arbitrary = [
Expand Down
1 change: 1 addition & 0 deletions crates/sol-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ std = ["alloy-primitives/std", "alloy-json-abi?/std", "serde?/std"]
json = ["dep:alloy-json-abi", "alloy-sol-macro/json"]
eip712-serde = ["dep:serde", "alloy-primitives/serde"]
arbitrary = ["alloy-primitives/arbitrary"]
more-tuple-impls = []
60 changes: 60 additions & 0 deletions crates/sol-types/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
/// Calls the given macro with all the tuples.
#[rustfmt::skip]
#[cfg(not(feature = "more-tuple-impls"))] // 24
macro_rules! all_the_tuples {
(@double $mac:path) => {
$mac!( 1 (T1 U1));
$mac!( 2 (T1 U1), (T2 U2));
$mac!( 3 (T1 U1), (T2 U2), (T3 U3));
$mac!( 4 (T1 U1), (T2 U2), (T3 U3), (T4 U4));
$mac!( 5 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5));
$mac!( 6 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6));
$mac!( 7 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7));
$mac!( 8 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8));
$mac!( 9 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9));
$mac!(10 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10));
$mac!(11 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11));
$mac!(12 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12));
$mac!(13 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13));
$mac!(14 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14));
$mac!(15 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15));
$mac!(16 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16));
$mac!(17 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17));
$mac!(18 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18));
$mac!(19 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19));
$mac!(20 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19), (T20 U20));
$mac!(21 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19), (T20 U20), (T21 U21));
$mac!(22 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19), (T20 U20), (T21 U21), (T22 U22));
$mac!(23 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19), (T20 U20), (T21 U21), (T22 U22), (T23 U23));
$mac!(24 (T1 U1), (T2 U2), (T3 U3), (T4 U4), (T5 U5), (T6 U6), (T7 U7), (T8 U8), (T9 U9), (T10 U10), (T11 U11), (T12 U12), (T13 U13), (T14 U14), (T15 U15), (T16 U16), (T17 U17), (T18 U18), (T19 U19), (T20 U20), (T21 U21), (T22 U22), (T23 U23), (T24 U24));
};

($mac:path) => {
$mac!( 1 T1);
$mac!( 2 T1, T2);
$mac!( 3 T1, T2, T3);
$mac!( 4 T1, T2, T3, T4);
$mac!( 5 T1, T2, T3, T4, T5);
$mac!( 6 T1, T2, T3, T4, T5, T6);
$mac!( 7 T1, T2, T3, T4, T5, T6, T7);
$mac!( 8 T1, T2, T3, T4, T5, T6, T7, T8);
$mac!( 9 T1, T2, T3, T4, T5, T6, T7, T8, T9);
$mac!(10 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
$mac!(11 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
$mac!(12 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
$mac!(13 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
$mac!(14 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
$mac!(15 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
$mac!(16 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
$mac!(17 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17);
$mac!(18 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18);
$mac!(19 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19);
$mac!(20 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20);
$mac!(21 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21);
$mac!(22 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22);
$mac!(23 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23);
$mac!(24 T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24);
};
}

/// Calls the given macro with all the tuples.
#[rustfmt::skip]
#[cfg(feature = "more-tuple-impls")] // 60
macro_rules! all_the_tuples {
(@double $mac:path) => {
$mac!( 1 (T1 U1));
Expand Down
67 changes: 67 additions & 0 deletions scripts/all_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""
Generate Rust's all_the_tuples macro with configurable N.
"""


def generate_tuple_list(n, double=False):
"""Generate tuple list for a given n."""
if double:
return ", ".join(f"(T{i} U{i})" for i in range(1, n + 1))
else:
return ", ".join(f"T{i}" for i in range(1, n + 1))


def generate_macro(max_n):
"""Generate the complete all_the_tuples macro."""
lines = [
"/// Calls the given macro with all the tuples.",
"#[rustfmt::skip]",
"macro_rules! all_the_tuples {",
" (@double $mac:path) => {",
]

# Generate @double variant
for i in range(1, max_n + 1):
tuple_list = generate_tuple_list(i, double=True)
lines.append(f" $mac!({i:2} {tuple_list});")

lines.append(" };")
lines.append("")
lines.append(" ($mac:path) => {")

# Generate single variant
for i in range(1, max_n + 1):
tuple_list = generate_tuple_list(i, double=False)
lines.append(f" $mac!({i:2} {tuple_list});")

lines.append(" };")
lines.append("}")
lines.append("")

return "\n".join(lines)


def main():
import sys

if len(sys.argv) != 2:
print("Usage: python all_tuples.py <N>")
print("Example: python all_tuples.py 60")
sys.exit(1)

try:
n = int(sys.argv[1])
if n <= 0:
raise ValueError("N must be positive")
except ValueError as e:
print(f"Error: {e}")
print("N must be a positive integer")
sys.exit(1)

macro_code = generate_macro(n)
print(macro_code)


if __name__ == "__main__":
main()
Loading