Skip to content

Commit

Permalink
Simplify macro in fp::prime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoeyBF committed Aug 14, 2024
1 parent bd831d5 commit b0a826b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ext/crates/fp/src/prime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> for $pn {
type Error = PrimeError;

Expand Down
2 changes: 1 addition & 1 deletion ext/crates/fp/src/prime/primes_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions ext/crates/fp/src/prime/primes_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit b0a826b

Please sign in to comment.