-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Rustdoc label badge for notable traits #157058
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
9ffec13
c354aec
0126a8a
ebc813b
ef5716f
88c202c
977ed35
0886603
412812a
f478afa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| use std::borrow::Cow; | ||
| use std::cmp::Ordering; | ||
| use std::collections::hash_map::DefaultHasher; | ||
| use std::fmt::{self, Display, Write as _}; | ||
| use std::hash::{Hash, Hasher}; | ||
| use std::iter; | ||
|
|
||
| use askama::Template; | ||
|
|
@@ -38,7 +40,7 @@ use crate::html::format::{ | |
| }; | ||
| use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; | ||
| use crate::html::render::sidebar::filters; | ||
| use crate::html::render::{document_full, document_item_info}; | ||
| use crate::html::render::{document_full, document_item_info, notable_trait_badges}; | ||
| use crate::html::url_parts_builder::UrlPartsBuilder; | ||
|
|
||
| const ITEM_TABLE_OPEN: &str = "<dl class=\"item-table\">"; | ||
|
|
@@ -51,6 +53,15 @@ struct PathComponent { | |
| name: Symbol, | ||
| } | ||
|
|
||
| struct NotableTraitBadgeVars { | ||
| name: String, | ||
| full_path: String, | ||
| /// Relative URL to the trait page, or `None` when not linkable. | ||
| href: Option<String>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the trait is not linkable, we should not generate a badge for it (I mentioned that here).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it does, yes ; we should probably decide what to do about the notable trait visibility on function return type to align behaviors ? I'd say keep it consistent and then unify behavior on a follow up pr ? But no strong feeling, can do here, I just fear controversial discussions on tangantial changes :p
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Errata: notable traits may be surfaced when the deps are not built ; minimal impact but we should keep option for local builds then. |
||
| /// Index of the `.notable-trait-badge-{n}` color class. | ||
| color_index: u8, | ||
| } | ||
|
|
||
| #[derive(Template)] | ||
| #[template(path = "print_item.html")] | ||
| struct ItemVars<'a> { | ||
|
|
@@ -59,6 +70,7 @@ struct ItemVars<'a> { | |
| item_type: &'a str, | ||
| path_components: Vec<PathComponent>, | ||
| stability_since_raw: &'a str, | ||
| notable_trait_badges: Vec<NotableTraitBadgeVars>, | ||
| src_href: Option<&'a str>, | ||
| } | ||
|
|
||
|
|
@@ -112,6 +124,25 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Disp | |
| let src_href = | ||
| if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None }; | ||
|
|
||
| let notable_trait_badges: Vec<NotableTraitBadgeVars> = notable_trait_badges(item, cx) | ||
| .into_iter() | ||
| .map(|info| { | ||
| // Stable per-trait color from a hash of the trait path so the | ||
| // same trait gets the same badge color across pages. | ||
| // This won't be stable between releases though. | ||
| let mut h = DefaultHasher::new(); | ||
| info.full_path.hash(&mut h); | ||
| const BADGE_COLORS: u8 = 6; | ||
| let color_index = (h.finish() as u8) % BADGE_COLORS; | ||
| NotableTraitBadgeVars { | ||
| name: info.name, | ||
| full_path: info.full_path, | ||
| href: info.href, | ||
| color_index, | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| let path_components = if item.is_fake_item() { | ||
| vec![] | ||
| } else { | ||
|
|
@@ -135,6 +166,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Disp | |
| item_type: &item.type_().to_string(), | ||
| path_components, | ||
| stability_since_raw: &stability_since_raw, | ||
| notable_trait_badges, | ||
| src_href: src_href.as_deref(), | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #![feature(doc_notable_trait)] | ||
| #![crate_name = "foo"] | ||
|
|
||
| #[doc(notable_trait)] | ||
| pub trait Labeled {} | ||
|
|
||
| pub trait Bound {} | ||
|
|
||
| // A conditional impl: the badge is rendered unconditionally even though the | ||
| // impl only holds for `T: Bound`. | ||
| //@ has 'foo/struct.Wrapper.html' '//div[@class="notable-trait-badge-container"]/a[@href="trait.Labeled.html"]' 'Labeled' | ||
| pub struct Wrapper<T>(pub T); | ||
| impl<T: Bound> Labeled for Wrapper<T> {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #![feature(doc_notable_trait, negative_impls)] | ||
| #![crate_name = "foo"] | ||
|
|
||
| #[doc(notable_trait)] | ||
| pub trait Labeled {} | ||
|
|
||
| // A negative impl must not produce a badge. | ||
| //@ count 'foo/struct.Neg.html' '//div[@class="notable-trait-badge-container"]' 0 | ||
| pub struct Neg; | ||
| impl !Labeled for Neg {} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe doc hidden, private traits should be tested too? And unlinkable trait but not sure we can emulate an unbuilt doc dependency |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #![feature(doc_notable_trait)] | ||
| #![crate_name = "foo"] | ||
|
|
||
| #[doc(notable_trait)] | ||
| pub trait Base {} | ||
|
|
||
| pub trait Derived: Base {} | ||
|
|
||
| //@ has 'foo/struct.S.html' | ||
| // Implementing `Derived` requires implementing the notable supertrait `Base`, | ||
| // so its badge shows up. | ||
| //@ count - '//div[@class="notable-trait-badge-container"]/a' 1 | ||
| //@ has - '//div[@class="notable-trait-badge-container"]/a[@href="trait.Base.html"]' 'Base' | ||
| pub struct S; | ||
| impl Base for S {} | ||
| impl Derived for S {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #![feature(doc_notable_trait)] | ||
| #![crate_name = "foo"] | ||
|
|
||
| #[doc(notable_trait)] | ||
| pub trait Labeled {} | ||
|
|
||
| #[doc(notable_trait)] | ||
| pub trait AlsoLabeled {} | ||
|
|
||
| pub trait Plain {} | ||
|
|
||
| //@ has 'foo/struct.Tagged.html' | ||
| //@ has - '//div[@class="notable-trait-badge-container"]/a[@href="trait.Labeled.html"][@title="foo::Labeled"]' 'Labeled' | ||
| // Badges are sorted by trait name, so `AlsoLabeled` precedes `Labeled`. | ||
| //@ has - '//div[@class="notable-trait-badge-container"]/a[1]' 'AlsoLabeled' | ||
| //@ has - '//div[@class="notable-trait-badge-container"]/a[2]' 'Labeled' | ||
| pub struct Tagged; | ||
| impl Labeled for Tagged {} | ||
| impl AlsoLabeled for Tagged {} | ||
| impl Plain for Tagged {} | ||
|
|
||
| //@ has 'foo/struct.Untagged.html' | ||
| //@ count - '//div[@class="notable-trait-badge-container"]' 0 | ||
| pub struct Untagged; | ||
| impl Plain for Untagged {} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could use if let chains too
View changes since the review