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 methods = Vec::new();
437    if let Some(v) = cache.impls.get(&did) {
438        let mut used_links = FxHashSet::default();
439        let mut id_map = IdMap::new();
440
441        {
442            let used_links_bor = &mut used_links;
443            for impl_ in v.iter().map(|i| i.inner_impl()).filter(|i| i.trait_.is_none()) {
444                assoc_consts.extend(get_associated_constants(impl_, used_links_bor));
445                assoc_types.extend(get_associated_types(impl_, used_links_bor));
446                methods.extend(get_methods(impl_, false, used_links_bor, false, cx.tcx()));
447            }
448            // We want links' order to be reproducible so we don't use unstable sort.
449            assoc_consts.sort();
450            assoc_types.sort();
451            methods.sort();
452        }
453
454        let mut blocks = vec![
455            LinkBlock::new(
456                Link::new("implementations", "Associated Constants"),
457                "associatedconstant",
458                assoc_consts,
459            ),
460            LinkBlock::new(
461                Link::new("implementations", "Associated Types"),
462                "associatedtype",
463                assoc_types,
464            ),
465            LinkBlock::new(Link::new("implementations", "Methods"), "method", methods),
466        ];
467
468        if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
469            if let Some(impl_) =
470                v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
471            {
472                let mut derefs = DefIdSet::default();
473                derefs.insert(did);
474                sidebar_deref_methods(
475                    cx,
476                    &mut blocks,
477                    impl_,
478                    v,
479                    &mut derefs,
480                    &mut used_links,
481                    deref_id_map,
482                );
483            }
484
485            let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
486                v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_auto());
487            let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
488                concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_blanket());
489
490            sidebar_render_assoc_items(
491                cx,
492                &mut id_map,
493                concrete,
494                synthetic,
495                blanket_impl,
496                &mut blocks,
497            );
498        }
499
500        links.append(&mut blocks);
501    }
502}
503
504fn sidebar_deref_methods<'a>(
505    cx: &'a Context<'_>,
506    out: &mut Vec<LinkBlock<'a>>,
507    impl_: &Impl,
508    v: &[Impl],
509    derefs: &mut DefIdSet,
510    used_links: &mut FxHashSet<String>,
511    deref_id_map: &'a DefIdMap<String>,
512) {
513    let c = cx.cache();
514
515    debug!("found Deref: {impl_:?}");
516    if let Some((target, real_target)) =
517        impl_.inner_impl().items.iter().find_map(|item| match item.kind {
518            clean::AssocTypeItem(box ref t, _) => Some(match *t {
519                clean::TypeAlias { item_type: Some(ref type_), .. } => (type_, &t.type_),
520                _ => (&t.type_, &t.type_),
521            }),
522            _ => None,
523        })
524    {
525        debug!("found target, real_target: {target:?} {real_target:?}");
526        if let Some(did) = target.def_id(c) &&
527            let Some(type_did) = impl_.inner_impl().for_.def_id(c) &&
528            // `impl Deref<Target = S> for S`
529            (did == type_did || !derefs.insert(did))
530        {
531            // Avoid infinite cycles
532            return;
533        }
534        let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
535        let inner_impl = target
536            .def_id(c)
537            .or_else(|| {
538                target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned())
539            })
540            .and_then(|did| c.impls.get(&did));
541        if let Some(impls) = inner_impl {
542            debug!("found inner_impl: {impls:?}");
543            let mut ret = impls
544                .iter()
545                .filter(|i| {
546                    i.inner_impl().trait_.is_none()
547                        && real_target.is_doc_subtype_of(&i.inner_impl().for_, c)
548                })
549                .flat_map(|i| get_methods(i.inner_impl(), true, used_links, deref_mut, cx.tcx()))
550                .collect::<Vec<_>>();
551            if !ret.is_empty() {
552                let id = if let Some(target_def_id) = real_target.def_id(c) {
553                    Cow::Borrowed(
554                        deref_id_map
555                            .get(&target_def_id)
556                            .expect("Deref section without derived id")
557                            .as_str(),
558                    )
559                } else {
560                    Cow::Borrowed("deref-methods")
561                };
562                let title = format!(
563                    "Methods from {:#}<Target={:#}>",
564                    print_path(impl_.inner_impl().trait_.as_ref().unwrap(), cx),
565                    print_type(real_target, cx),
566                );
567                // We want links' order to be reproducible so we don't use unstable sort.
568                ret.sort();
569                out.push(LinkBlock::new(Link::new(id, title), "deref-methods", ret));
570            }
571        }
572
573        // Recurse into any further impls that might exist for `target`
574        if let Some(target_did) = target.def_id(c)
575            && let Some(target_impls) = c.impls.get(&target_did)
576            && let Some(target_deref_impl) = target_impls.iter().find(|i| {
577                i.inner_impl()
578                    .trait_
579                    .as_ref()
580                    .map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait())
581                    .unwrap_or(false)
582            })
583        {
584            sidebar_deref_methods(
585                cx,
586                out,
587                target_deref_impl,
588                target_impls,
589                derefs,
590                used_links,
591                deref_id_map,
592            );
593        }
594    }
595}
596
597fn sidebar_enum<'a>(
598    cx: &'a Context<'_>,
599    it: &'a clean::Item,
600    e: &'a clean::Enum,
601    items: &mut Vec<LinkBlock<'a>>,
602    deref_id_map: &'a DefIdMap<String>,
603) {
604    let mut variants = e
605        .non_stripped_variants()
606        .filter_map(|v| v.name)
607        .map(|name| Link::new(format!("variant.{name}"), name.to_string()))
608        .collect::<Vec<_>>();
609    variants.sort_unstable();
610
611    items.push(LinkBlock::new(Link::new("variants", "Variants"), "variant", variants));
612    sidebar_assoc_items(cx, it, items, deref_id_map);
613}
614
615pub(crate) fn sidebar_module_like(
616    item_sections_in_use: FxHashSet<ItemSection>,
617    ids: &mut IdMap,
618    module_like: ModuleLike,
619) -> LinkBlock<'static> {
620    let item_sections: Vec<Link<'_>> = ItemSection::ALL
621        .iter()
622        .copied()
623        .filter(|sec| item_sections_in_use.contains(sec))
624        .map(|sec| Link::new(ids.derive(sec.id()), sec.name()))
625        .collect();
626    let header = if let Some(first_section) = item_sections.first() {
627        Link::new(
628            first_section.href.clone(),
629            if module_like.is_crate() { "Crate Items" } else { "Module Items" },
630        )
631    } else {
632        Link::empty()
633    };
634    LinkBlock::new(header, "", item_sections)
635}
636
637fn sidebar_module(
638    items: &[clean::Item],
639    ids: &mut IdMap,
640    module_like: ModuleLike,
641) -> LinkBlock<'static> {
642    let item_sections_in_use: FxHashSet<_> = items
643        .iter()
644        .filter(|it| {
645            !it.is_stripped()
646                && it
647                    .name
648                    .or_else(|| {
649                        if let clean::ImportItem(ref i) = it.kind
650                            && let clean::ImportKind::Simple(s) = i.kind
651                        {
652                            Some(s)
653                        } else {
654                            None
655                        }
656                    })
657                    .is_some()
658        })
659        .map(|it| item_ty_to_section(it.type_()))
660        .collect();
661
662    sidebar_module_like(item_sections_in_use, ids, module_like)
663}
664
665fn sidebar_foreign_type<'a>(
666    cx: &'a Context<'_>,
667    it: &'a clean::Item,
668    items: &mut Vec<LinkBlock<'a>>,
669    deref_id_map: &'a DefIdMap<String>,
670) {
671    sidebar_assoc_items(cx, it, items, deref_id_map);
672}
673
674/// Renders the trait implementations for this type
675fn sidebar_render_assoc_items(
676    cx: &Context<'_>,
677    id_map: &mut IdMap,
678    concrete: Vec<&Impl>,
679    synthetic: Vec<&Impl>,
680    blanket_impl: Vec<&Impl>,
681    items: &mut Vec<LinkBlock<'_>>,
682) {
683    let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
684        let mut links = FxHashSet::default();
685
686        let mut ret = impls
687            .iter()
688            .filter_map(|it| {
689                let trait_ = it.inner_impl().trait_.as_ref()?;
690                let encoded = id_map.derive(super::get_id_for_impl(cx.tcx(), it.impl_item.item_id));
691
692                let prefix = match it.inner_impl().polarity {
693                    ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
694                    ty::ImplPolarity::Negative => "!",
695                };
696                let generated = Link::new(encoded, format!("{prefix}{:#}", print_path(trait_, cx)));
697                if links.insert(generated.clone()) { Some(generated) } else { None }
698            })
699            .collect::<Vec<Link<'static>>>();
700        ret.sort();
701        ret
702    };
703
704    let concrete = format_impls(concrete, id_map);
705    let synthetic = format_impls(synthetic, id_map);
706    let blanket = format_impls(blanket_impl, id_map);
707    items.extend([
708        LinkBlock::new(
709            Link::new("trait-implementations", "Trait Implementations"),
710            "trait-implementation",
711            concrete,
712        ),
713        LinkBlock::new(
714            Link::new("synthetic-implementations", "Auto Trait Implementations"),
715            "synthetic-implementation",
716            synthetic,
717        ),
718        LinkBlock::new(
719            Link::new("blanket-implementations", "Blanket Implementations"),
720            "blanket-implementation",
721            blanket,
722        ),
723    ]);
724}
725
726fn get_next_url(used_links: &mut FxHashSet<String>, url: String) -> String {
727    if used_links.insert(url.clone()) {
728        return url;
729    }
730    let mut add = 1;
731    while !used_links.insert(format!("{url}-{add}")) {
732        add += 1;
733    }
734    format!("{url}-{add}")
735}
736
737fn get_methods<'a>(
738    i: &'a clean::Impl,
739    for_deref: bool,
740    used_links: &mut FxHashSet<String>,
741    deref_mut: bool,
742    tcx: TyCtxt<'_>,
743) -> Vec<Link<'a>> {
744    i.items
745        .iter()
746        .filter_map(|item| {
747            if let Some(ref name) = item.name
748                && item.is_method()
749                && (!for_deref || super::should_render_item(item, deref_mut, tcx))
750            {
751                Some(Link::new(
752                    get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::Method)),
753                    name.as_str(),
754                ))
755            } else {
756                None
757            }
758        })
759        .collect()
760}
761
762fn get_associated_constants<'a>(
763    i: &'a clean::Impl,
764    used_links: &mut FxHashSet<String>,
765) -> Vec<Link<'a>> {
766    i.items
767        .iter()
768        .filter_map(|item| {
769            if let Some(ref name) = item.name
770                && item.is_associated_const()
771            {
772                Some(Link::new(
773                    get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::AssocConst)),
774                    name.as_str(),
775                ))
776            } else {
777                None
778            }
779        })
780        .collect()
781}
782
783fn get_associated_types<'a>(
784    i: &'a clean::Impl,
785    used_links: &mut FxHashSet<String>,
786) -> Vec<Link<'a>> {
787    i.items
788        .iter()
789        .filter_map(|item| {
790            if let Some(ref name) = item.name
791                && item.is_associated_type()
792            {
793                Some(Link::new(
794                    get_next_url(used_links, format!("{typ}.{name}", typ = ItemType::AssocType)),
795                    name.as_str(),
796                ))
797            } else {
798                None
799            }
800        })
801        .collect()
802}