# Feature request for declarative macro: make tt to match TraitName for any struct implemented TraitName

**URL:** <https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750>\
**Category:** language design\
**Created:** [May 21, 2021, 2:15pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750 "2021-05-21T14:15:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank](https://internals.rust-lang.org/u/Frank)\
**Post date:** [May 21, 2021, 2:15pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750/1 "2021-05-21T14:15:11Z")

</div>

For implementing a great declarative macro, it need tt to match a TraitName. For example, the following my\_great\_macro! have to call set\_value! marcro:

macro\_rules! my\_great\_macro {

```
......
set_value!(name, $t);//$t is a tt!
......

```

}

macro\_rules! set\_u32\_value {

```
($name:expr, u32) => { //it's good to match when tt==u32
    ...
};
($name:expr, bool) => { //it's good to match when tt==bool
    ...
};
($name:expr, TraitName) => { //however, it cannot match TraitName!
    ...
};
($name:expr, StructName) => { //even StructName, it's good to match when tt==StructName
    ...
};

($($else:tt)*) => {};

```

}

It is good to match the tt pattern for u32, bool, and even StructName. But, it cannot match the pattern for any struct which impl TraitName.

Is it possible to implement this feature for a future version of rust language?

---

<div class="post-metadata">

**Author:** ![ibraheemdev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ibraheemdev/32/13406_2.png) [@ibraheemdev](https://internals.rust-lang.org/u/ibraheemdev)\
**Post date:** [May 21, 2021, 2:25pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750/2 "2021-05-21T14:25:47Z")

</div>

Are `TraitName` and ` StructName` just placeholders here? Because I can't see why one would match and not the other...

Also, I think you are looking for they `$ty` matcher.

---

<div class="post-metadata">

**Author:** ![Frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank](https://internals.rust-lang.org/u/Frank)\
**Post date:** [May 21, 2021, 2:32pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750/3 "2021-05-21T14:32:46Z")

</div>

> [@ibraheemdev](#):
>
> Are `TraitName` and ` StructName` just placeholders here? Because I can't see why one would match and not the other

No, $ty is not what I need.

Well, do you notice 'struct which impl TraitName' I mentioned? TraitName is not the actual placeholders，but StructName - struct implemented TraitName.

Well, my mistake. I have modified the title to fit what it really mean.

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [May 21, 2021, 2:55pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750/4 "2021-05-21T14:55:36Z")

</div>

> [@Frank](#):
>
> But, it cannot match the pattern for any struct which impl TraitName.

This is impossible: macros don't have access to information about what types implement what traits, they run at an earlier phase of the compilation process.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 19, 2021, 2:56pm UTC](https://internals.rust-lang.org/t/feature-request-for-declarative-macro-make-tt-to-match-traitname-for-any-struct-implemented-traitname/14750/5 "2021-08-19T14:56:03Z")

</div>

Closed on August 19, 2021, 2:56pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
