# Tool for finding struct that implements a set of traits

**URL:** <https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398>\
**Category:** tools and infrastructure\
**Created:** [September 17, 2022, 1:23pm UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398 "2022-09-17T13:23:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![twitu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/twitu/32/9846_2.png) [@twitu](https://internals.rust-lang.org/u/twitu)\
**Post date:** [September 17, 2022, 1:23pm UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/1 "2022-09-17T13:23:48Z")

</div>

I'd posted this [question on SO](https://stackoverflow.com/q/73752344/7588896) and got directed here. I'll reshare it here to foster some discussion.

* * *

I've come across a case where I have a generic that needs to satisfy two traits.

```rust
pub struct FileReader<R: Read + Seek> { /* private fields */ }

```

These are standard traits and I can find their implementors individually. Then I can see which implementors are common and use one of them.

However this made me consider if there's a way to query - cargo docs, the compiler or something to find possible structs that implement a given set of traits. This might be useful when generics have many trait conditions and/or a trait has many implementors.

Cargo docs already has a search bar for queries but it's for searching with names or with function types. Not one level up queries like these.

Anyone one else who thinks this is a cool and would want to use it?

This looks feasible to me. Are there any seriously challenges in implementing something like this?

* * *

To extend the discussion, suppose there was a way to find this. I'm not sure where it would fit -

1. Cargo docs sounds like a good place because it's where the documentation lives where I'd typically learn about what traits a particular generic type needs. So getting candidate implementors right there would be great.

2. It could also be a cli command like to query a set of candidates implementors for a set of traits in the context of project.

3. Finally it could also be an extension for something like rust analyzer i.e. a language server

---

<div class="post-metadata">

**Author:** ![y86-dev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/y86-dev/32/9867_2.png) [@y86-dev](https://internals.rust-lang.org/u/y86-dev)\
**Post date:** [September 17, 2022, 1:39pm UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/2 "2022-09-17T13:39:14Z")

</div>

Take a look at [chalk](https://github.com/rust-lang/chalk). I think this would do exactly what you want. I personally do not know the status of it, but I also would really like to see this.

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [September 17, 2022, 11:50pm UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/3 "2022-09-17T23:50:12Z")

</div>

Chalk alone is not enough as it does not parse Rust code or expand macros etc.. But building that on top of rust-analyzer (or rustc) should be feasible.

---

<div class="post-metadata">

**Author:** ![y86-dev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/y86-dev/32/9867_2.png) [@y86-dev](https://internals.rust-lang.org/u/y86-dev)\
**Post date:** [September 18, 2022, 10:59am UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/4 "2022-09-18T10:59:21Z")

</div>

I am not familiar enough with chalk, but iirc there are plans to at some point create a REPL (maybe integrated into cargo?) that would be able to answer exactly these kinds of questions: `list all T: Read + Seek`, right?

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [September 18, 2022, 11:38am UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/5 "2022-09-18T11:38:52Z")

</div>

There is such REPL, but it doesn't parse Rust code and you can't feed Rust source code into it. It uses some Rust-like syntax, and also doesn't include macro expansion, name resolution etc.

---

<div class="post-metadata">

**Author:** ![twitu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/twitu/32/9846_2.png) [@twitu](https://internals.rust-lang.org/u/twitu)\
**Post date:** [October 14, 2022, 2:49am UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/6 "2022-10-14T02:49:43Z")

</div>

I just noticed that rust-analyzer has [integrated with chalk](https://rust-analyzer.github.io/blog/2021/12/30/2021-recap.html#chalk-integration). I'll create an issue there, to discuss if it's possible to query for and auto-suggest a trait resolution.

---

<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:** [January 12, 2023, 2:50am UTC](https://internals.rust-lang.org/t/tool-for-finding-struct-that-implements-a-set-of-traits/17398/7 "2023-01-12T02:50:30Z")

</div>

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