-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Rustdoc label trait feature #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?
Rustdoc label trait feature #157058
Changes from all commits
19beb1c
c95ffaa
4843bad
fcfb089
6cddf7b
62b5091
60688d1
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,5 +1,7 @@ | ||
| 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; | ||
|
|
@@ -37,7 +39,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, label_traits_for_item}; | ||
| use crate::html::url_parts_builder::UrlPartsBuilder; | ||
|
|
||
| const ITEM_TABLE_OPEN: &str = "<dl class=\"item-table\">"; | ||
|
|
@@ -50,6 +52,15 @@ struct PathComponent { | |
| name: Symbol, | ||
| } | ||
|
|
||
| struct LabelTraitVars { | ||
| name: String, | ||
| full_path: String, | ||
| /// Relative URL to the trait page, or empty when not linkable. | ||
| href: String, | ||
|
Comment on lines
+58
to
+59
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. should it be an option?
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. I think we should always link to the trait, so I'd say no.
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 would make us not be able to do so ? It would be quite far-fetched to add label_trait to doc_hidden but... Maybe other cases like... visibility? not sure. |
||
| /// Pre-rendered `style="..."` attribute. | ||
| style_attr: String, | ||
| } | ||
|
|
||
| #[derive(Template)] | ||
| #[template(path = "print_item.html")] | ||
| struct ItemVars<'a> { | ||
|
|
@@ -58,6 +69,7 @@ struct ItemVars<'a> { | |
| item_type: &'a str, | ||
| path_components: Vec<PathComponent>, | ||
| stability_since_raw: &'a str, | ||
| impl_label_traits: Vec<LabelTraitVars>, | ||
| src_href: Option<&'a str>, | ||
| } | ||
|
|
||
|
|
@@ -111,6 +123,30 @@ 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 impl_label_traits: Vec<LabelTraitVars> = label_traits_for_item(item, cx) | ||
| .into_iter() | ||
| .map(|info| { | ||
| // Stable per-trait color from a hash of the DefId 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); | ||
| let v = h.finish(); | ||
| let style_attr = format!( | ||
| "style=\"background: rgb({}, {}, {})\"", | ||
| v as u8, | ||
| (v >> 8) as u8, | ||
| (v >> 16) as u8, | ||
| ); | ||
| LabelTraitVars { | ||
| name: info.name, | ||
| full_path: info.full_path, | ||
| href: info.href.unwrap_or_default(), | ||
| style_attr, | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| let path_components = if item.is_fake_item() { | ||
| vec![] | ||
| } else { | ||
|
|
@@ -134,6 +170,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, | ||
| impl_label_traits, | ||
| src_href: src_href.as_deref(), | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #![feature(doc_label_trait)] | ||
| #![crate_name = "foo"] | ||
|
|
||
| #[doc(label_trait)] | ||
| pub trait Labeled {} | ||
|
|
||
| #[doc(label_trait)] | ||
| pub trait AlsoLabeled {} | ||
|
|
||
| pub trait Plain {} | ||
|
|
||
| //@ has 'foo/struct.Tagged.html' | ||
| //@ has - '//a[@class="impl-label-trait-full-badge"][@href="trait.Labeled.html"][@title="foo::Labeled"]' 'Labeled' | ||
| // Badges are sorted by trait name, so `AlsoLabeled` precedes `Labeled`. | ||
| //@ has - '//div[@class="impl-label-trait-full-badge-container"]/a[1]' 'AlsoLabeled' | ||
| //@ has - '//div[@class="impl-label-trait-full-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="impl-label-trait-full-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.
should we have strong verification on which items those are applied?
View changes since the review