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

rustdoc: allow searching by trait bounds #84824

Open
pickfire opened this issue May 2, 2021 · 4 comments
Open

rustdoc: allow searching by trait bounds #84824

pickfire opened this issue May 2, 2021 · 4 comments
Labels
A-rustdoc-search Area: Rustdoc's search feature C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@pickfire
Copy link
Contributor

pickfire commented May 2, 2021

https://docs.rs/hyper/0.14.7/hyper/client/conn/struct.Builder.html

image

Some libraries have trait bounds more complicated and more that one items, some of it could be automatically-derived or some could be specifically implemented for a certain structs/enums. Other libraries such as diesel also make use of trait bounds extensively.

pub fn handshake<T, B>(
    &self,
    io: T
) -> impl Future<Output = Result<(SendRequest<B>, Connection<T, B>)>> where
    T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
    B: HttpBody + 'static,
    B::Data: Send,
    B::Error: Into<Box<dyn StdError + Send + Sync>>, 

If the user wants to find out which type can they used within the trait bound. According to the context above that they want to find out what can they use for T, they had to click on AsyncRead, capture all the implementations in mind and then go back and click on AsyncWrite and see which types appear again since trait bound is additive, it needs to satisfy all the traits.

If the user implemented a struct/enum that implemented all the specified trait bound then they know which is it, but if the user is using a third party library, chances are they will easily get lost or take some time to figure out what can T be without implementing it themselves. Without examples of the function, it would be require some time to go through and understand all the traits and their respective implementations.

Maybe we could have a way for user to know what could possibly be the types that they can use for T without going through the whole codebase? Behaving like goto implementation of language server, except now it can show what types satisfies all the specified trait bounds?

Of course the solution may not need to be perfect. I can think it may not work well with auto-traits since it could be hard to do that. Two solutions I can think of, we can have a implementations section within the search tab that shows all the types that satisfies the trait bounds specified, like AsyncRead + AsyncWrite (but not sure if we can also do it for Unpin or Send, maybe we couldn't cover lifetime at all). Another thing I can think of is that we can maybe provide some information when one clicks/hover on io or T then it will show the types that implement it, similar to the auto-traits section within the doc page.

Not sure if it is feasible, just an idea. Maybe we even had to have another search index if we can't reuse the existing one and not sure if it's worth it, or maybe we could make it lazily loaded only when they reached the tab. One issue I see is that types being shown may not be able to show standard library types and that may be an issue.

@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-search Area: Rustdoc's search feature labels May 2, 2021
@jsha
Copy link
Contributor

jsha commented Jun 3, 2021

I definitely see the utility of this, but I wonder if it might not be better solved by the documentation itself. For instance, right now the documentation for handshake says:

Constructs a connection with the configured options and IO.

Instead it could say:

Constructs a connection with the configured options and IO. The io parameter will usually be a tokio::net::TcpStream.

The advantage of this approach is that it can work across crate boundaries, even when the crates are not built together. Right now, crates on docs.rs aren't aware of each other when built.

@pickfire
Copy link
Contributor Author

pickfire commented Jun 3, 2021

The advantage of this approach is that it can work across crate boundaries, even when the crates are not built together. Right now, crates on docs.rs aren't aware of each other when built.

Yeah, what you said is good but in reality I have never seen any crates that have docs that do this. Documentation is good only if there are good documentation writers (rust have lots of good documentation but I see most low-level stuff documentation is bad), in any case we always have the trait bounds so giving the user the ability will always benefit the users compared to just documentation because we can't force how the documentation is written.

jsha added a commit to jsha/hyper that referenced this issue Jun 3, 2021
Per rust-lang/rust#84824, it can be hard to figure out
from the trait bounds what types meet those bounds.
@jsha
Copy link
Contributor

jsha commented Jun 3, 2021

Here's an idea: we could look to see what types are used to satisfy T in a crate's tests, doctests, and internal code. Then we could offer a hover tip on T that would say something like "example types that satisfy T: tokio::net::TcpStream, hyper::test::TestStream". That would solve the "crates aren't aware of each other at build time" problem. Which I realize now is not really accurate - a crate is aware of its own dependencies.

@jyn514
Copy link
Member

jyn514 commented Jun 22, 2021

Here's an idea: we could look to see what types are used to satisfy T in a crate's tests, doctests, and internal code. Then we could offer a hover tip on T that would say something like "example types that satisfy T: tokio::net::TcpStream, hyper::test::TestStream". That would solve the "crates aren't aware of each other at build time" problem. Which I realize now is not really accurate - a crate is aware of its own dependencies.

See rust-lang/rfcs#3123 for a more fleshed out idea around this.

@camelid camelid added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-search Area: Rustdoc's search feature C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants