Skip to main content

rustdoc/html/render/
sidebar.rs

1use std::borrow::Cow;
2use std::cmp::Ordering;
3use std::fmt;
4
5use askama::Template;
6use rustc_data_structures::fx::FxHashSet;
7use rustc_hir::def::CtorKind;
8use rustc_hir::def_id::{DefIdMap, DefIdSet};
9use rustc_middle::ty::{self, TyCtxt};
10use tracing::debug;
11
12use super::{Context, ItemSection, item_ty_to_section};
13use crate::clean;
14use crate::formats::Impl;
15use crate::formats::item_type::ItemType;
16use crate::html::format::{print_path, print_type};
17use crate::html::markdown::{IdMap, MarkdownWithToc};
18use crate::html::render::print_item::compare_names;
19
20#[derive(Clone, Copy)]
21pub(crate) enum ModuleLike {
22    Module,
23    Crate,
24}
25
26impl ModuleLike {
27    pub(crate) fn is_crate(self) -> bool {
28        matches!(self, ModuleLike::Crate)
29    }
30}
31impl<'a> From<&'a clean::Item> for ModuleLike {
32    fn from(it: &'a clean::Item) -> ModuleLike {
33        if it.is_crate() { ModuleLike::Crate } else { ModuleLike::Module }
34    }
35}
36
37#[derive(Template)]
38#[template(path = "sidebar.html")]
39pub(super) struct Sidebar<'a> {
40    pub(super) title_prefix: &'static str,
41    pub(super) title: &'a str,
42    pub(super) is_crate: bool,
43    pub(super) parent_is_crate: bool,
44    pub(super) is_mod: bool,
45    pub(super) blocks: Vec<LinkBlock<'a>>,
46    pub(super) path: String,
47}
48
49impl Sidebar<'_> {
50    /// Only create a `<section>` if there are any blocks
51    /// which should actually be rendered.
52    pub fn should_render_blocks(&self) -> bool {
53        self.blocks.iter().any(LinkBlock::should_render)
54    }
55}
56
57/// A sidebar section such as 'Methods'.
58pub(crate) struct LinkBlock<'a> {
59    /// The name of this section, e.g. 'Methods'
60    /// as well as the link to it, e.g. `#implementations`.
61    /// Will be rendered inside an `<h3>` tag
62    heading: Link<'a>,
63    class: &'static str,
64    links: Vec<Link<'a>>,
65    /// Render the heading even if there are no links
66    force_render: bool,
67}
68
69impl<'a> LinkBlock<'a> {
70    pub fn new(heading: Link<'a>, class: &'static str, links: Vec<Link<'a>>) -> Self {
71        Self { heading, links, class, force_render: false }
72    }
73
74    pub fn forced(heading: Link<'a>, class: &'static str) -> Self {
75        Self { heading, links: vec![], class, force_render: true }
76    }
77
78    pub fn should_render(&self) -> bool {
79        self.force_render || !self.links.is_empty()
80    }
81}
82
83/// A link to an item. Content should not be escaped.
84#[derive(PartialEq, Eq, Hash, Clone)]
85pub(crate) struct Link<'a> {
86    /// The content for the anchor tag and title attr
87    name: Cow<'a, str>,
88    /// The content for the anchor tag (if different from name)
89    name_html: Option<Cow<'a, str>>,
90    /// The id of an anchor within the page (without a `#` prefix)
91    href: Cow<'a, str>,
92    /// Nested list of links (used only in top-toc)
93    children: Vec<Link<'a>>,
94}
95
96impl Ord for Link<'_> {
97    fn cmp(&self, other: &Self) -> Ordering {
98        match compare_names(&self.name, &other.name) {
99            Ordering::Equal => {}
100            result => return result,
101        }
102        (&self.name_html, &self.href, &self.children).cmp(&(
103            &other.name_html,
104            &other.href,
105            &other.children,
106        ))
107    }
108}
109
110impl PartialOrd for Link<'_> {
111    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
112        Some(self.cmp(other))
113    }
114}
115
116impl<'a> Link<'a> {
117    pub fn new(href: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>) -> Self {
118        Self { href: href.into(), name: name.into(), children: vec![], name_html: None }
119    }
120    pub fn empty() -> Link<'static> {
121        Link::new("", "")
122    }
123}
124
125pub(crate) mod filters {
126    use std::fmt::{self, Display};
127
128    use askama::filters::Safe;
129
130    use crate::html::escape::EscapeBodyTextWithWbr;
131
132    #[askama::filter_fn]
133    pub(crate) fn wrapped(
134        v: impl Display,
135        _: &dyn askama::Values,
136    ) -> askama::Result<Safe<impl Display>> {
137        let string = v.to_string();
138        Ok(Safe(fmt::from_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f))))
139    }
140}
141
142pub(super) fn print_sidebar(
143    cx: &Context<'_>,
144    it: &clean::Item,
145    mut buffer: impl fmt::Write,
146) -> fmt::Result {
147    let mut ids = IdMap::new();
148    let mut blocks: Vec<LinkBlock<'_>> = docblock_toc(cx, it, &mut ids).into_iter().collect();
149    let deref_id_map = cx.deref_id_map.borrow();
150    match it.kind {
151        clean::StructItem(ref s) => sidebar_struct(cx, it, s, &mut blocks, &deref_id_map),
152        clean::TraitItem(ref t) => sidebar_trait(cx, it, t, &mut blocks, &deref_id_map),
153        clean::PrimitiveItem(_) => sidebar_primitive(cx, it, &mut blocks, &deref_id_map),
154        clean::UnionItem(ref u) => sidebar_union(cx, it, u, &mut blocks, &deref_id_map),
155        clean::EnumItem(ref e) => sidebar_enum(cx, it, e, &mut blocks, &deref_id_map),
156        clean::TypeAliasItem(ref t) => sidebar_type_alias(cx, it, t, &mut blocks, &deref_id_map),
157        clean::ModuleItem(ref m) => {
158            blocks.push(sidebar_module(&m.items, &mut ids, ModuleLike::from(it)))
159        }
160        clean::ForeignTypeItem => sidebar_foreign_type(cx, it, &mut blocks, &deref_id_map),
161        _ => {}
162    }
163    // The sidebar is designed to display sibling functions, modules and
164    // other miscellaneous information. since there are lots of sibling
165    // items (and that causes quadratic growth in large modules),
166    // we refactor common parts into a shared JavaScript file per module.
167    // still, we don't move everything into JS because we want to preserve
168    // as much HTML as possible in order to allow non-JS-enabled browsers
169    // to navigate the documentation (though slightly inefficiently).
170    //
171    // crate title is displayed as part of logo lockup
172    let (title_prefix, title) = if !blocks.is_empty() && !it.is_crate() {
173        (
174            match it.kind {
175                clean::ModuleItem(..) => "Module ",
176                _ => "",
177            },
178            it.name.as_ref().unwrap().as_str(),
179        )
180    } else {
181        ("", "")
182    };
183    // need to show parent path header if:
184    //   - it's a child module, instead of the crate root
185    //   - there's a sidebar section for the item itself
186    //
187    // otherwise, the parent path header is redundant with the big crate
188    // branding area at the top of the sidebar
189    let sidebar_path =
190        if it.is_mod() { &cx.current[..cx.current.len() - 1] } else { &cx.current[..] };
191    let path: String = if sidebar_path.len() > 1 || !title.is_empty() {
192        let path = sidebar_path.iter().map(|s| s.as_str()).intersperse("::").collect();
193        if sidebar_path.len() == 1 { format!("crate {path}") } else { path }
194    } else {
195        "".into()
196    };
197    let sidebar = Sidebar {
198        title_prefix,
199        title,
200        is_mod: it.is_mod(),
201        is_crate: it.is_crate(),
202        parent_is_crate: sidebar_path.len() == 1,
203        blocks,
204        path,
205    };
206    sidebar.render_into(&mut buffer)?;
207    Ok(())
208}
209
210fn get_struct_fields_name<'a>(fields: &'a [clean::Item]) -> Vec<Link<'a>> {
211    let mut fields = fields
212        .iter()
213        .filter(|f| matches!(f.kind, clean::StructFieldItem(..)))
214        .filter_map(|f| {
215            f.name.as_ref().map(|name| Link::new(format!("structfield.{name}"), name.as_str()))
216        })
217        .collect::<Vec<Link<'a>>>();
218    fields.sort();
219    fields
220}
221
222fn docblock_toc<'a>(
223    cx: &'a Context<'_>,
224    it: &'a clean::Item,
225    ids: &mut IdMap,
226) -> Option<LinkBlock<'a>> {
227    let (toc, _) = MarkdownWithToc {
228        content: &it.doc_value(),
229        links: &it.links(cx),
230        ids,
231        error_codes: cx.shared.codes,
232        edition: cx.shared.edition(),
233        playground: &cx.shared.playground,
234    }
235    .into_parts();
236    let links: Vec<Link<'_>> = toc
237        .entries
238        .into_iter()
239        .map(|entry| {
240            Link {
241                name_html: if entry.html == entry.name { None } else { Some(entry.html.into()) },
242                name: entry.name.into(),
243                href: entry.id.into(),
244                children: entry
245                    .children
246                    .entries
247                    .into_iter()
248                    .map(|entry| Link {
249                        name_html: if entry.html == entry.name {
250                            None
251                        } else {
252                            Some(entry.html.into())
253                        },
254                        name: entry.name.into(),
255                        href: entry.id.into(),
256                        // Only a single level of nesting is shown here.
257                        // Going the full six could break the layout,
258                        // so we have to cut it off somewhere.
259                        children: vec![],
260                    })
261                    .collect(),
262            }
263        })
264        .collect();
265    if links.is_empty() {
266        None
267    } else {
268        Some(LinkBlock::new(Link::new("", "Sections"), "top-toc", links))
269    }
270}
271
272fn sidebar_struct<'a>(
273    cx: &'a Context<'_>,
274    it: &'a clean::Item,
275    s: &'a clean::Struct,
276    items: &mut Vec<LinkBlock<'a>>,
277    deref_id_map: &'a DefIdMap<String>,
278) {
279    let fields = get_struct_fields_name(&s.fields);
280    let field_name = match s.ctor_kind {
281        Some(CtorKind::Fn) => Some("Tuple Fields"),
282        None => Some("Fields"),
283        _ => None,
284    };
285    if let Some(name) = field_name {
286        items.push(LinkBlock::new(Link::new("fields", name), "structfield", fields));
287    }
288    sidebar_assoc_items(cx, it, items, deref_id_map);
289}
290
291fn sidebar_trait<'a>(
292    cx: &'a Context<'_>,
293    it: &'a clean::Item,
294    t: &'a clean::Trait,
295    blocks: &mut Vec<LinkBlock<'a>>,
296    deref_id_map: &'a DefIdMap<String>,
297) {
298    fn filter_items<'a>(
299        items: &'a [clean::Item],
300        filt: impl Fn(&clean::Item) -> bool,
301        ty: &str,
302    ) -> Vec<Link<'a>> {
303        let mut res = items
304            .iter()
305            .filter_map(|m: &clean::Item| match m.name {
306                Some(ref name) if filt(m) => Some(Link::new(format!("{ty}.{name}"), name.as_str())),
307                _ => None,
308            })
309            .collect::<Vec<Link<'a>>>();
310        res.sort();
311        res
312    }
313
314    let req_assoc = filter_items(&t.items, |m| m.is_required_associated_type(), "associatedtype");
315    let prov_assoc = filter_items(&t.items, |m| m.is_associated_type(), "associatedtype");
316    let req_assoc_const =
317        filter_items(&t.items, |m| m.is_required_associated_const(), "associatedconstant");
318    let prov_assoc_const =
319        filter_items(&t.items, |m| m.is_associated_const(), "associatedconstant");
320    let req_method = filter_items(&t.items, |m| m.is_ty_method(), "tymethod");
321    let prov_method = filter_items(&t.items, |m| m.is_method(), "method");
322    let mut foreign_impls = vec![];
323    if let Some(implementors) = cx.cache().implementors.get(&it.item_id.expect_def_id()) {
324        foreign_impls.extend(
325            implementors
326                .iter()
327                .filter(|i| !i.is_on_local_type(cx))
328                .filter_map(|i| super::extract_for_impl_name(&i.impl_item, cx))
329                .map(|(name, id)| Link::new(id, name)),
330        );
331        foreign_impls.sort();
332    }
333
334    blocks.extend(
335        [
336            ("required-associated-consts", "Required Associated Constants", req_assoc_const),
337            ("provided-associated-consts", "Provided Associated Constants", prov_assoc_const),
338            ("required-associated-types", "Required Associated Types", req_assoc),
339            ("provided-associated-types", "Provided Associated Types", prov_assoc),
340            ("required-methods", "Required Methods", req_method),
341            ("provided-methods", "Provided Methods", prov_method),
342            ("foreign-impls", "Implementations on Foreign Types", foreign_impls),
343        ]
344        .into_iter()
345        .map(|(id, title, items)| LinkBlock::new(Link::new(id, title), "", items)),
346    );
347    sidebar_assoc_items(cx, it, blocks, deref_id_map);
348
349    if !t.is_dyn_compatible(cx.tcx()) {
350        blocks.push(LinkBlock::forced(
351            Link::new("dyn-compatibility", "Dyn Compatibility"),
352            "dyn-compatibility-note",
353        ));
354    }
355
356    blocks.push(LinkBlock::forced(Link::new("implementors", "Implementors"), "impl"));
357    if t.is_auto(cx.tcx()) {
358        blocks.push(LinkBlock::forced(
359            Link::new("synthetic-implementors", "Auto Implementors"),
360            "impl-auto",
361        ));
362    }
363}
364
365fn sidebar_primitive<'a>(
366    cx: &'a Context<'_>,
367    it: &'a clean::Item,
368    items: &mut Vec<LinkBlock<'a>>,
369    deref_id_map: &'a DefIdMap<String>,
370) {
371    if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
372        sidebar_assoc_items(cx, it, items, deref_id_map);
373    } else {
374        let (concrete, synthetic, blanket_impl) =
375            super::get_filtered_impls_for_reference(&cx.shared, it);
376
377        sidebar_render_assoc_items(cx, &mut IdMap::new(), concrete, synthetic, blanket_impl, items);
378    }
379}
380
381fn sidebar_type_alias<'a>(
382    cx: &'a Context<'_>,
383    it: &'a clean::Item,
384    t: &'a clean::TypeAlias,
385    items: &mut Vec<LinkBlock<'a>>,
386    deref_id_map: &'a DefIdMap<String>,
387) {
388    if let Some(inner_type) = &t.inner_type {
389        items.push(LinkBlock::forced(Link::new("aliased-type", "Aliased Type"), "type"));
390        match inner_type {
391            clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive: _ } => {
392                let mut variants = variants
393                    .iter()
394                    .filter(|i| !i.is_stripped())
395                    .filter_map(|v| v.name)
396                    .map(|name| Link::new(format!("variant.{name}"), name.to_string()))
397                    .collect::<Vec<_>>();
398                variants.sort_unstable();
399
400                items.push(LinkBlock::new(Link::new("variants", "Variants"), "variant", variants));
401            }
402            clean::TypeAliasInnerType::Union { fields }
403            | clean::TypeAliasInnerType::Struct { ctor_kind: _, fields } => {
404                let fields = get_struct_fields_name(fields);
405                items.push(LinkBlock::new(Link::new("fields", "Fields"), "field", fields));
406            }
407        }
408    }
409    sidebar_assoc_items(cx, it, items, deref_id_map);
410}
411
412fn sidebar_union<'a>(
413    cx: &'a Context<'_>,
414    it: &'a clean::Item,
415    u: &'a clean::Union,
416    items: &mut Vec<LinkBlock<'a>>,
417    deref_id_map: &'a DefIdMap<String>,
418) {
419    let fields = get_struct_fields_name(&u.fields);
420    items.push(LinkBlock::new(Link::new("fields", "Fields"), "structfield", fields));
421    sidebar_assoc_items(cx, it, items, deref_id_map);
422}
423
424/// Adds trait implementations into the blocks of links
425fn sidebar_assoc_items<'a>(
426    cx: &'a Context<'_>,
427    it: &'a clean::Item,
428    links: &mut Vec<LinkBlock<'a>>,
429    deref_id_map: &'a DefIdMap<String>,
430) {
431    let did = it.item_id.expect_def_id();
432    let cache = cx.cache();
433
434    let mut assoc_consts = Vec::new();
435    let mut assoc_types = Vec::new();
436    let mut assoc_fns = Vec::new();
437    let mut methods = Vec::new();
438    if let Some(v) = cache.impls.get(&did) {
439        let mut used_links = FxHashSet::default();
440        let mut id_map = IdMap::new();
441
442        {
443            let used_links_bor = &mut used_links;
444            for impl_ in v.iter().map(|i| i.inner_impl()).filter(|i| i.trait_.is_none()) {
445                assoc_consts.extend(get_associated_constants(impl_, used_links_bor));
446                assoc_types.extend(get_associated_types(impl_, used_links_bor));
447                methods.extend(get_methods(
448                    impl_,
449                    GetMethodsMode::AlsoCollectAssocFns { assoc_fns: &mut assoc_fns },
450                    used_links_bor,
451                    cx.tcx(),
452                ));
453            }
454            // We want links' order to be reproducible so we don't use unstable sort.
455            assoc_consts.sort();
456            assoc_types.sort();
457            methods.sort();
458        }
459
460        let mut blocks = vec![
461            LinkBlock::new(
462                Link::new("implementations", "Associated Constants"),
463                "associatedconstant",
464                assoc_consts,
465            ),
466            LinkBlock::new(
467                Link::new("implementations", "Associated Types"),
468                "associatedtype",
469                assoc_types,
470            ),
471            LinkBlock::new(
472                Link::new("implementations", "Associated Functions"),
473                "method",
474                assoc_fns,
475            ),
476            LinkBlock::new(Link::new("implementations", "Methods"), "method", methods),
477        ];
478
479        if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
480            if let Some(impl_) = v.iter().find(|i| {
481                i.trait_did() == cx.tcx().lang_items().deref_trait() && !i.is_negative_trait_impl()
482            }) {
483                let mut derefs = DefIdSet::default();
484                derefs.insert(did);
485                sidebar_deref_methods(
486                    cx,
487                    &mut blocks,
488                    impl_,
489                    v,
490                    &mut derefs,
491                    &mut used_links,
492                    deref_id_map,
493                );
494            }
495
496            let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
497                v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_auto());
498            let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
499                concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_blanket());
500
501            sidebar_render_assoc_items(
502                cx,
503                &mut id_map,
504                concrete,
505                synthetic,
506                blanket_impl,
507                &mut blocks,
508            );
509        }
510
511        links.append(&mut blocks);
512    }
513}
514
515fn sidebar_deref_methods<'a>(
516    cx: &'a Context<'_>,
517    out: &mut Vec<LinkBlock<'a>>,
518    impl_: &Impl,
519    v: &[Impl],
520    derefs: &mut DefIdSet,
521    used_links: &mut FxHashSet<String>,
522    deref_id_map: &'a DefIdMap<String>,
523) {
524    let c = cx.cache();
525
526    debug!("found Deref: {impl_:?}");
527    if let Some((target, real_target)) =
528        impl_.inner_impl().items.iter().find_map(|item| match item.kind {
529            clean::AssocTypeItem(box ref t, _) => Some(match *t {
530                clean::TypeAlias { item_type: Some(ref type_), .. } => (type_, &t.type_),
531                _ => (&t.type_, &t.type_),
532            }),
533            _ => None,
534        })
535    {
536        debug!("found target, real_target: {target:?} {real_target:?}");
537        if let Some(did) = target.def_id(c) &&
538            let Some(type_did) = impl_.inner_impl().for_.def_id(c) &&
539            // `impl Deref<Target = S> for S`
540            (did == type_did || !derefs.insert(did))
541        {
542            // Avoid infinite cycles
543            return;
544        }
545        let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
546        let inner_impl = target
547            .def_id(c)
548            .or_else(|| {
549                target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned())
550            })
551            .and_then(|did| c.impls.get(&did));
552        if let Some(impls) = inner_impl {
553            debug!("found inner_impl: {impls:?}");
554            let mut ret = impls
555                .iter()
556                .filter(|i| {
557                    i.inner_impl().trait_.is_none()
558                        && real_target.is_doc_subtype_of(&i.inner_impl().for_, c)
559                })
560                .flat_map(|i| {
561                    get_methods(
562                        i.inner_impl(),
563                        GetMethodsMode::Deref { deref_mut },
564                        used_links,
565                        cx.tcx(),
566                    )
567                    .collect::<Vec<_>>()
568                })
569                .collect::<Vec<_>>();
570            if !ret.is_empty() {
571                let id = if let Some(target_def_id) = real_target.def_id(c) {
572                    Cow::Borrowed(
573                        deref_id_map
574                            .get(&target_def_id)
575                            .expect("Deref section without derived id")
576                            .as_str(),
577                    )
578                } else {
579                    Cow::Borrowed("deref-methods")
580                };
581                let title = format!(
582                    "Methods from {:#}<Target={:#}>",
583                    print_path(impl_.inner_impl().trait_.as_ref().unwrap(), cx),
584                    print_type(real_target, cx),
585                );
586                // We want links' order to be reproducible so we don't use unstable sort.
587                ret.sort();
588                out.push(LinkBlock::new(Link::new(id, title), "deref-methods", ret));
589            }
590        }
591
592        // Recurse into any further impls that might exist for `target`
593        if let Some(target_did) = target.def_id(c)
594            && let Some(target_impls) = c.impls.get(&target_did)
595            && let Some(target_deref_impl) = target_impls.iter().find(|i| {
596                i.inner_impl()
597                    .trait_
598                    .as_ref()
599                    .map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait())
600                    .unwrap_or(false)
601                    && !i.is_negative_trait_impl()
602            })
603        {
604            sidebar_deref_methods(
605                cx,
606                out,
607                target_deref_impl,
608                target_impls,
609                derefs,
610                used_links,
611                deref_id_map,
612            );
613        }
614    }
615}
616
617fn sidebar_enum<'a>(
618    cx: &'a Context<'_>,
619    it: &'a clean::Item,
620    e: &'a clean::Enum,
621    items: &mut Vec<LinkBlock<'a>>,
622    deref_id_map: &'a DefIdMap<String>,
623) {
624    let mut variants = e
625        .non_stripped_variants()
626        .filter_map(|v| v.name)
627        .map(|name| Link::new(format!("variant.{name}"), name.to_string()))
628        .collect::<Vec<_>>();
629    variants.sort_unstable();
630
631    items.push(LinkBlock::new(Link::new("variants", "Variants"), "variant", variants));
632    sidebar_assoc_items(cx, it, items, deref_id_map);
633}
634
635pub(crate) fn sidebar_module_like(
636    item_sections_in_use: FxHashSet<ItemSection>,
637    ids: &mut IdMap,
638    module_like: ModuleLike,
639) -> LinkBlock<'static> {
640    let item_sections: Vec<Link<'_>> = ItemSection::ALL
641        .iter()
642        .copied()
643        .filter(|sec| item_sections_in_use.contains(sec))
644        .map(|sec| Link::new(ids.derive(sec.id()), sec.name()))
645        .collect();
646    let header = if let Some(first_section) = item_sections.first() {
647        Link::new(
648            first_section.href.clone(),
649            if module_like.is_crate() { "Crate Items" } else { "Module Items" },
650        )
651    } else {
652        Link::empty()
653    };
654    LinkBlock::new(header, "", item_sections)
655}
656
657fn sidebar_module(
658    items: &[clean::Item],
659    ids: &mut IdMap,
660    module_like: ModuleLike,
661) -> LinkBlock<'static> {
662    let item_sections_in_use: FxHashSet<_> = items
663        .iter()
664        .filter(|it| {
665            !it.is_stripped()
666                && it
667                    .name
668                    .or_else(|| {
669                        if let clean::ImportItem(ref i) = it.kind
670                            && let clean::ImportKind::Simple(s) = i.kind
671                        {
672                            Some(s)
673                        } else {
674                            None
675                        }
676                    })
677                    .is_some()
678        })
679        .map(|it| item_ty_to_section(it.type_()))
680        .collect();
681
682    sidebar_module_like(item_sections_in_use, ids, module_like)
683}
684
685fn sidebar_foreign_type<'a>(
686    cx: &'a Context<'_>,
687    it: &'a clean::Item,
688    items: &mut Vec<LinkBlock<'a>>,
689    deref_id_map: &'a DefIdMap<String>,
690) {
691    sidebar_assoc_items(cx, it, items, deref_id_map);
692}
693
694/// Renders the trait implementations for this type
695fn sidebar_render_assoc_items(
696    cx: &Context<'_>,
697    id_map: &mut IdMap,
698    concrete: Vec<&Impl>,
699    synthetic: Vec<&Impl>,
700    blanket_impl: Vec<&Impl>,
701    items: &mut Vec<LinkBlock<'_>>,
702) {
703    let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
704        let mut links = FxHashSet::default();
705
706        let mut ret = impls
707            .iter()
708            .filter_map(|it| {
709                let trait_ = it.inner_impl().trait_.as_ref()?;
710                let encoded = id_map.derive(super::get_id_for_impl(cx.tcx(), it.impl_item.item_id));
711
712                let prefix = match it.inner_impl().polarity {
713                    ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
714                    ty::ImplPolarity::Negative => "!",
715                };
716                let generated = Link::new(encoded, format!("{prefix}{:#}", print_path(trait_, cx)));
717                if links.insert(generated.clone()) { Some(generated) } else { None }
718            })
719            .collect::<Vec<Link<'static>>>();
720        ret.sort();
721        ret
722    };
723
724    let concrete = format_impls(concrete, id_map);
725    let synthetic = format_impls(synthetic, id_map);
726    let blanket = format_impls(blanket_impl, id_map);
727    items.extend([
728        LinkBlock::new(
729            Link::new("trait-implementations", "Trait Implementations"),
730            "trait-implementation",
731            concrete,
732        ),
733        LinkBlock::new(
734            Link::new("synthetic-implementations", "Auto Trait Implementations"),
735            "synthetic-implementation",
736            synthetic,
737        ),
738        LinkBlock::new(
739            Link::new("blanket-implementations", "Blanket Implementations"),
740            "blanket-implementation",
741            blanket,
742        ),
743    ]);
744}
745
746fn get_next_url(used_links: &mut FxHashSet<String>, url: String) -> String {
747    if used_links.insert(url.clone()) {
748        return url;
749    }
750    let mut add = 1;
751    while !used_links.insert(format!("{url}-{add}")) {
752        add += 1;
753    }
754    format!("{url}-{add}")
755}
756
757enum GetMethodsMode<'r, 'l> {
758    Deref { deref_mut: bool },
759    AlsoCollectAssocFns { assoc_fns: &'r mut Vec<Link<'l>> },
760}
761
762fn get_methods<'a>(
763    i: &'a clean::Impl,
764    mut mode: GetMethodsMode<'_, 'a>,
765    used_links: &mut FxHashSet<String>,
766    tcx: TyCtxt<'_>,
767) -> impl Iterator<Item = Link<'a>> {
768    i.items.iter().filter_map(move |item| {
769        if let Some(ref name) = item.name
770            && item.is_method()
771        {
772            let mut build_link = || {
773                Link::new(
774                    get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::Method)),
775                    name.as_str(),
776                )
777            };
778            match &mut mode {
779                &mut GetMethodsMode::Deref { deref_mut } => {
780                    if super::should_render_item(item, deref_mut, tcx) {
781                        Some(build_link())
782                    } else {
783                        None
784                    }
785                }
786                GetMethodsMode::AlsoCollectAssocFns { assoc_fns } => {
787                    if item.has_self_param() {
788                        Some(build_link())
789                    } else {
790                        assoc_fns.push(build_link());
791                        None
792                    }
793                }
794            }
795        } else {
796            None
797        }
798    })
799}
800
801fn get_associated_constants<'a>(
802    i: &'a clean::Impl,
803    used_links: &mut FxHashSet<String>,
804) -> impl Iterator<Item = Link<'a>> {
805    i.items.iter().filter_map(|item| {
806        if let Some(ref name) = item.name
807            && item.is_associated_const()
808        {
809            Some(Link::new(
810                get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::AssocConst)),
811                name.as_str(),
812            ))
813        } else {
814            None
815        }
816    })
817}
818
819fn get_associated_types<'a>(
820    i: &'a clean::Impl,
821    used_links: &mut FxHashSet<String>,
822) -> impl Iterator<Item = Link<'a>> {
823    i.items.iter().filter_map(|item| {
824        if let Some(ref name) = item.name
825            && item.is_associated_type()
826        {
827            Some(Link::new(
828                get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::AssocType)),
829                name.as_str(),
830            ))
831        } else {
832            None
833        }
834    })
835}