From b0a826b864b0958204b185faf566768e01d94c71 Mon Sep 17 00:00:00 2001 From: Joey Beauvais-Feisthauer Date: Tue, 13 Aug 2024 23:19:23 -0400 Subject: [PATCH] Simplify macro in `fp::prime` I just learned this trick to capture a `tt` and reparse it as other types of ASTs and it's definitely a good one to know --- ext/crates/fp/src/prime/mod.rs | 5 ++++- ext/crates/fp/src/prime/primes_2.rs | 2 +- ext/crates/fp/src/prime/primes_generic.rs | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ext/crates/fp/src/prime/mod.rs b/ext/crates/fp/src/prime/mod.rs index b1151b00b..fcc0eb1c0 100644 --- a/ext/crates/fp/src/prime/mod.rs +++ b/ext/crates/fp/src/prime/mod.rs @@ -232,8 +232,11 @@ macro_rules! impl_prime_ops { } macro_rules! impl_try_from { + ($p:tt) => { + impl_try_from!(@ $p, $p); + }; // We need the type both as a type and as an expression. - ($pn:ty, $pn_ex:expr) => { + (@ $pn:ty, $pn_ex:expr) => { impl std::convert::TryFrom for $pn { type Error = PrimeError; diff --git a/ext/crates/fp/src/prime/primes_2.rs b/ext/crates/fp/src/prime/primes_2.rs index eb3212450..bef3a4cd2 100644 --- a/ext/crates/fp/src/prime/primes_2.rs +++ b/ext/crates/fp/src/prime/primes_2.rs @@ -4,7 +4,7 @@ use super::*; def_prime_static!(P2, 2); impl_prime_ops!(P2); -impl_try_from!(P2, P2); +impl_try_from!(P2); pub type ValidPrime = P2; diff --git a/ext/crates/fp/src/prime/primes_generic.rs b/ext/crates/fp/src/prime/primes_generic.rs index b04e469c1..814a4bf48 100644 --- a/ext/crates/fp/src/prime/primes_generic.rs +++ b/ext/crates/fp/src/prime/primes_generic.rs @@ -12,10 +12,10 @@ impl_prime_ops!(P3); impl_prime_ops!(P5); impl_prime_ops!(P7); -impl_try_from!(P2, P2); -impl_try_from!(P3, P3); -impl_try_from!(P5, P5); -impl_try_from!(P7, P7); +impl_try_from!(P2); +impl_try_from!(P3); +impl_try_from!(P5); +impl_try_from!(P7); pub(crate) mod fp { use super::{P2, P3, P5, P7};