Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo list trait implementors #14852

Open
walksanatora opened this issue Nov 22, 2024 · 6 comments
Open

Cargo list trait implementors #14852

walksanatora opened this issue Nov 22, 2024 · 6 comments
Labels
A-new-subcommand Area: new subcommand C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@walksanatora
Copy link

Problem

I am writing a program and would like a way to see every struct that implements a trait (eg: serde::Serialize)
I know that at somepoint during compilation the full type information is avaliable because rust is able to give output like

     = help: the following other types implement trait `_::_serde::Serialize`:
               &'a T
               &'a mut T
               ()
               (T,)
               (T0, T1)
               (T0, T1, T2)
               (T0, T1, T2, T3)
               (T0, T1, T2, T3, T4)
             and 150 others

when using types that dont implement a trait.

Proposed Solution

if I had to guess this would probally be best relegated to some kind of nightly -Z flag probally smth like -Z dump-impls probally producing either a debug output dump of the crates. or just a simple tabulated tree of crates and traits and impls

Notes

No response

@walksanatora walksanatora added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Nov 22, 2024
@walksanatora walksanatora changed the title Cargo list implementations Cargo list trait implementors Nov 22, 2024
@walksanatora
Copy link
Author

this may also work as a sub-command of cargo report eg: cargo report trait-implementations

@epage
Copy link
Contributor

epage commented Nov 22, 2024

Another way to consider this is that cargo doc is able to collect this information. On top of that, there is experimental json output for it. Someone could experiment with specific queries or a general purpose set of queries for types and traits based on that. This would likely be the best way forward for experimenting and evaluating the idea before inclusion into Cargo in some form.

@epage epage added A-new-subcommand Area: new subcommand S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. and removed S-triage Status: This issue is waiting on initial triage. labels Nov 22, 2024
@walksanatora
Copy link
Author

odd is that when I ran cargo +nightly rustdoc -Z unstable-options --output-format=json it only included the trait implementation information for my crate. (as the unstable option of output-format is not on the base doc command yet)

@weihanglo
Copy link
Member

Guess it is because cargo rustdoc passes those trailing arguments to only one crate selected in CLI, which by default is your primary package.

@charmitro
Copy link
Contributor

I'm also interested in this feature.

I've been thinking about a potential cargo trait-query subcommand.

Maybe use rustdoc experimental JSON output to build a queryable index of trait implementations?

@walksanatora
Copy link
Author

so I ended up making smth stupid that parsed the cargo doc html to get all structs implementing a trait using scraper+ego-tree and clap
it is very slow sincce it needs to crunch through EVERY HTML file in the doc directory
https://github.com/walksanatora/trait_search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-new-subcommand Area: new subcommand C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

4 participants