Tool for finding struct that implements a set of traits

I'd posted this question on SO 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.

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

1 Like

Take a look at 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.

1 Like

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.

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?

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.