summaryrefslogtreecommitdiffstats
path: root/src/librustdoc/html/render/sidebar.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /src/librustdoc/html/render/sidebar.rs
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/librustdoc/html/render/sidebar.rs')
-rw-r--r--src/librustdoc/html/render/sidebar.rs105
1 files changed, 68 insertions, 37 deletions
diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs
index 76f63c6f6..ba4aaaff5 100644
--- a/src/librustdoc/html/render/sidebar.rs
+++ b/src/librustdoc/html/render/sidebar.rs
@@ -19,7 +19,7 @@ pub(super) struct Sidebar<'a> {
pub(super) title_prefix: &'static str,
pub(super) title: &'a str,
pub(super) is_crate: bool,
- pub(super) version: &'a str,
+ pub(super) is_mod: bool,
pub(super) blocks: Vec<LinkBlock<'a>>,
pub(super) path: String,
}
@@ -38,18 +38,19 @@ pub(crate) struct LinkBlock<'a> {
/// as well as the link to it, e.g. `#implementations`.
/// Will be rendered inside an `<h3>` tag
heading: Link<'a>,
+ class: &'static str,
links: Vec<Link<'a>>,
/// Render the heading even if there are no links
force_render: bool,
}
impl<'a> LinkBlock<'a> {
- pub fn new(heading: Link<'a>, links: Vec<Link<'a>>) -> Self {
- Self { heading, links, force_render: false }
+ pub fn new(heading: Link<'a>, class: &'static str, links: Vec<Link<'a>>) -> Self {
+ Self { heading, links, class, force_render: false }
}
- pub fn forced(heading: Link<'a>) -> Self {
- Self { heading, links: vec![], force_render: true }
+ pub fn forced(heading: Link<'a>, class: &'static str) -> Self {
+ Self { heading, links: vec![], class, force_render: true }
}
pub fn should_render(&self) -> bool {
@@ -99,12 +100,12 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
|| it.is_primitive()
|| it.is_union()
|| it.is_enum()
- || it.is_mod()
+ // crate title is displayed as part of logo lockup
+ || (it.is_mod() && !it.is_crate())
|| it.is_type_alias()
{
(
match *it.kind {
- clean::ModuleItem(..) if it.is_crate() => "Crate ",
clean::ModuleItem(..) => "Module ",
_ => "",
},
@@ -113,14 +114,22 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
} else {
("", "")
};
- let version =
- if it.is_crate() { cx.cache().crate_version.as_deref().unwrap_or_default() } else { "" };
- let path: String = if !it.is_mod() {
- cx.current.iter().map(|s| s.as_str()).intersperse("::").collect()
+ // need to show parent path header if:
+ // - it's a child module, instead of the crate root
+ // - there's a sidebar section for the item itself
+ //
+ // otherwise, the parent path header is redundant with the big crate
+ // branding area at the top of the sidebar
+ let sidebar_path =
+ if it.is_mod() { &cx.current[..cx.current.len() - 1] } else { &cx.current[..] };
+ let path: String = if sidebar_path.len() > 1 || !title.is_empty() {
+ let path = sidebar_path.iter().map(|s| s.as_str()).intersperse("::").collect();
+ if sidebar_path.len() == 1 { format!("crate {path}") } else { path }
} else {
"".into()
};
- let sidebar = Sidebar { title_prefix, title, is_crate: it.is_crate(), version, blocks, path };
+ let sidebar =
+ Sidebar { title_prefix, title, is_mod: it.is_mod(), is_crate: it.is_crate(), blocks, path };
sidebar.render_into(buffer).unwrap();
}
@@ -149,7 +158,7 @@ fn sidebar_struct<'a>(
};
let mut items = vec![];
if let Some(name) = field_name {
- items.push(LinkBlock::new(Link::new("fields", name), fields));
+ items.push(LinkBlock::new(Link::new("fields", name), "structfield", fields));
}
sidebar_assoc_items(cx, it, &mut items);
items
@@ -206,12 +215,23 @@ fn sidebar_trait<'a>(
("foreign-impls", "Implementations on Foreign Types", foreign_impls),
]
.into_iter()
- .map(|(id, title, items)| LinkBlock::new(Link::new(id, title), items))
+ .map(|(id, title, items)| LinkBlock::new(Link::new(id, title), "", items))
.collect();
sidebar_assoc_items(cx, it, &mut blocks);
- blocks.push(LinkBlock::forced(Link::new("implementors", "Implementors")));
+
+ if !t.is_object_safe(cx.tcx()) {
+ blocks.push(LinkBlock::forced(
+ Link::new("object-safety", "Object Safety"),
+ "object-safety-note",
+ ));
+ }
+
+ blocks.push(LinkBlock::forced(Link::new("implementors", "Implementors"), "impl"));
if t.is_auto(cx.tcx()) {
- blocks.push(LinkBlock::forced(Link::new("synthetic-implementors", "Auto Implementors")));
+ blocks.push(LinkBlock::forced(
+ Link::new("synthetic-implementors", "Auto Implementors"),
+ "impl-auto",
+ ));
}
blocks
}
@@ -237,7 +257,7 @@ fn sidebar_type_alias<'a>(
) -> Vec<LinkBlock<'a>> {
let mut items = vec![];
if let Some(inner_type) = &t.inner_type {
- items.push(LinkBlock::forced(Link::new("aliased-type", "Aliased type")));
+ items.push(LinkBlock::forced(Link::new("aliased-type", "Aliased type"), "type"));
match inner_type {
clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive: _ } => {
let mut variants = variants
@@ -248,12 +268,12 @@ fn sidebar_type_alias<'a>(
.collect::<Vec<_>>();
variants.sort_unstable();
- items.push(LinkBlock::new(Link::new("variants", "Variants"), variants));
+ items.push(LinkBlock::new(Link::new("variants", "Variants"), "variant", variants));
}
clean::TypeAliasInnerType::Union { fields }
| clean::TypeAliasInnerType::Struct { ctor_kind: _, fields } => {
let fields = get_struct_fields_name(fields);
- items.push(LinkBlock::new(Link::new("fields", "Fields"), fields));
+ items.push(LinkBlock::new(Link::new("fields", "Fields"), "field", fields));
}
}
}
@@ -267,7 +287,7 @@ fn sidebar_union<'a>(
u: &'a clean::Union,
) -> Vec<LinkBlock<'a>> {
let fields = get_struct_fields_name(&u.fields);
- let mut items = vec![LinkBlock::new(Link::new("fields", "Fields"), fields)];
+ let mut items = vec![LinkBlock::new(Link::new("fields", "Fields"), "structfield", fields)];
sidebar_assoc_items(cx, it, &mut items);
items
}
@@ -279,12 +299,11 @@ fn sidebar_assoc_items<'a>(
links: &mut Vec<LinkBlock<'a>>,
) {
let did = it.item_id.expect_def_id();
- let v = cx.shared.all_impls_for_item(it, it.item_id.expect_def_id());
- let v = v.as_slice();
+ let cache = cx.cache();
let mut assoc_consts = Vec::new();
let mut methods = Vec::new();
- if !v.is_empty() {
+ if let Some(v) = cache.impls.get(&did) {
let mut used_links = FxHashSet::default();
let mut id_map = IdMap::new();
@@ -320,7 +339,7 @@ fn sidebar_assoc_items<'a>(
cx,
&mut deref_methods,
impl_,
- v.iter().copied(),
+ v,
&mut derefs,
&mut used_links,
);
@@ -333,12 +352,16 @@ fn sidebar_assoc_items<'a>(
sidebar_render_assoc_items(cx, &mut id_map, concrete, synthetic, blanket_impl)
} else {
- std::array::from_fn(|_| LinkBlock::new(Link::empty(), vec![]))
+ std::array::from_fn(|_| LinkBlock::new(Link::empty(), "", vec![]))
};
let mut blocks = vec![
- LinkBlock::new(Link::new("implementations", "Associated Constants"), assoc_consts),
- LinkBlock::new(Link::new("implementations", "Methods"), methods),
+ LinkBlock::new(
+ Link::new("implementations", "Associated Constants"),
+ "associatedconstant",
+ assoc_consts,
+ ),
+ LinkBlock::new(Link::new("implementations", "Methods"), "method", methods),
];
blocks.append(&mut deref_methods);
blocks.extend([concrete, synthetic, blanket]);
@@ -350,7 +373,7 @@ fn sidebar_deref_methods<'a>(
cx: &'a Context<'_>,
out: &mut Vec<LinkBlock<'a>>,
impl_: &Impl,
- v: impl Iterator<Item = &'a Impl>,
+ v: &[Impl],
derefs: &mut DefIdSet,
used_links: &mut FxHashSet<String>,
) {
@@ -375,7 +398,7 @@ fn sidebar_deref_methods<'a>(
// Avoid infinite cycles
return;
}
- let deref_mut = { v }.any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
+ let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
let inner_impl = target
.def_id(c)
.or_else(|| {
@@ -407,7 +430,7 @@ fn sidebar_deref_methods<'a>(
);
// We want links' order to be reproducible so we don't use unstable sort.
ret.sort();
- out.push(LinkBlock::new(Link::new(id, title), ret));
+ out.push(LinkBlock::new(Link::new(id, title), "deref-methods", ret));
}
}
@@ -426,7 +449,7 @@ fn sidebar_deref_methods<'a>(
cx,
out,
target_deref_impl,
- target_impls.iter(),
+ target_impls,
derefs,
used_links,
);
@@ -446,7 +469,7 @@ fn sidebar_enum<'a>(
.collect::<Vec<_>>();
variants.sort_unstable();
- let mut items = vec![LinkBlock::new(Link::new("variants", "Variants"), variants)];
+ let mut items = vec![LinkBlock::new(Link::new("variants", "Variants"), "variant", variants)];
sidebar_assoc_items(cx, it, &mut items);
items
}
@@ -460,7 +483,7 @@ pub(crate) fn sidebar_module_like(
.filter(|sec| item_sections_in_use.contains(sec))
.map(|sec| Link::new(sec.id(), sec.name()))
.collect();
- LinkBlock::new(Link::empty(), item_sections)
+ LinkBlock::new(Link::empty(), "", item_sections)
}
fn sidebar_module(items: &[clean::Item]) -> LinkBlock<'static> {
@@ -503,8 +526,7 @@ fn sidebar_render_assoc_items(
.iter()
.filter_map(|it| {
let trait_ = it.inner_impl().trait_.as_ref()?;
- let encoded =
- id_map.derive(super::get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx));
+ let encoded = id_map.derive(super::get_id_for_impl(cx.tcx(), it.impl_item.item_id));
let prefix = match it.inner_impl().polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
@@ -522,12 +544,21 @@ fn sidebar_render_assoc_items(
let synthetic = format_impls(synthetic, id_map);
let blanket = format_impls(blanket_impl, id_map);
[
- LinkBlock::new(Link::new("trait-implementations", "Trait Implementations"), concrete),
+ LinkBlock::new(
+ Link::new("trait-implementations", "Trait Implementations"),
+ "trait-implementation",
+ concrete,
+ ),
LinkBlock::new(
Link::new("synthetic-implementations", "Auto Trait Implementations"),
+ "synthetic-implementation",
synthetic,
),
- LinkBlock::new(Link::new("blanket-implementations", "Blanket Implementations"), blanket),
+ LinkBlock::new(
+ Link::new("blanket-implementations", "Blanket Implementations"),
+ "blanket-implementation",
+ blanket,
+ ),
]
}