1use std::cmp::Ordering;
2use std::fmt::{self, Display, Write as _};
3use std::iter;
4
5use askama::Template;
6use rustc_abi::VariantIdx;
7use rustc_ast::join_path_syms;
8use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
9use rustc_hir as hir;
10use rustc_hir::def::CtorKind;
11use rustc_hir::def_id::DefId;
12use rustc_index::IndexVec;
13use rustc_middle::ty::{self, TyCtxt};
14use rustc_span::hygiene::MacroKind;
15use rustc_span::symbol::{Symbol, sym};
16use tracing::{debug, info};
17
18use super::type_layout::document_type_layout;
19use super::{
20 AssocItemLink, AssocItemRender, Context, ImplRenderingParameters, RenderMode,
21 collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
22 item_ty_to_section, notable_traits_button, notable_traits_json, render_all_impls,
23 render_assoc_item, render_assoc_items, render_attributes_in_code, render_impl,
24 render_repr_attributes_in_code, render_rightside, render_stability_since_raw,
25 render_stability_since_raw_with_extra, write_section_heading,
26};
27use crate::clean;
28use crate::config::ModuleSorting;
29use crate::display::{Joined as _, MaybeDisplay as _};
30use crate::formats::Impl;
31use crate::formats::item_type::ItemType;
32use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
33use crate::html::format::{
34 Ending, PrintWithSpace, print_abi_with_space, print_constness_with_space, print_where_clause,
35 visibility_print_with_space,
36};
37use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
38use crate::html::render::sidebar::filters;
39use crate::html::render::{document_full, document_item_info};
40use crate::html::url_parts_builder::UrlPartsBuilder;
41
42macro_rules! item_template {
61 (
62 $(#[$meta:meta])*
63 struct $name:ident<'a, 'cx> {
64 cx: &'a Context<'cx>,
65 it: &'a clean::Item,
66 $($field_name:ident: $field_ty:ty),*,
67 },
68 methods = [$($methods:tt),* $(,)?]
69 ) => {
70 #[derive(Template)]
71 $(#[$meta])*
72 struct $name<'a, 'cx> {
73 cx: &'a Context<'cx>,
74 it: &'a clean::Item,
75 $($field_name: $field_ty),*
76 }
77
78 impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for $name<'a, 'cx> {
79 fn item_and_cx(&self) -> (&'a clean::Item, &'a Context<'cx>) {
80 (&self.it, &self.cx)
81 }
82 }
83
84 impl<'a, 'cx: 'a> $name<'a, 'cx> {
85 item_template_methods!($($methods)*);
86 }
87 };
88}
89
90macro_rules! item_template_methods {
94 () => {};
95 (document $($rest:tt)*) => {
96 fn document(&self) -> impl fmt::Display {
97 let (item, cx) = self.item_and_cx();
98 document(cx, item, None, HeadingOffset::H2)
99 }
100 item_template_methods!($($rest)*);
101 };
102 (document_type_layout $($rest:tt)*) => {
103 fn document_type_layout(&self) -> impl fmt::Display {
104 let (item, cx) = self.item_and_cx();
105 let def_id = item.item_id.expect_def_id();
106 document_type_layout(cx, def_id)
107 }
108 item_template_methods!($($rest)*);
109 };
110 (render_assoc_items $($rest:tt)*) => {
111 fn render_assoc_items(&self) -> impl fmt::Display {
112 let (item, cx) = self.item_and_cx();
113 let def_id = item.item_id.expect_def_id();
114 render_assoc_items(cx, item, def_id, AssocItemRender::All)
115 }
116 item_template_methods!($($rest)*);
117 };
118 ($method:ident $($rest:tt)*) => {
119 compile_error!(concat!("unknown method: ", stringify!($method)));
120 };
121 ($token:tt $($rest:tt)*) => {
122 compile_error!(concat!("unexpected token: ", stringify!($token)));
123 };
124}
125
126const ITEM_TABLE_OPEN: &str = "<dl class=\"item-table\">";
127const REEXPORTS_TABLE_OPEN: &str = "<dl class=\"item-table reexports\">";
128const ITEM_TABLE_CLOSE: &str = "</dl>";
129
130struct PathComponent {
132 path: String,
133 name: Symbol,
134}
135
136#[derive(Template)]
137#[template(path = "print_item.html")]
138struct ItemVars<'a> {
139 typ: &'a str,
140 name: &'a str,
141 item_type: &'a str,
142 path_components: Vec<PathComponent>,
143 stability_since_raw: &'a str,
144 src_href: Option<&'a str>,
145}
146
147pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Display {
148 debug_assert!(!item.is_stripped());
149
150 fmt::from_fn(|buf| {
151 let typ = match item.kind {
152 clean::ModuleItem(_) => {
153 if item.is_crate() {
154 "Crate "
155 } else {
156 "Module "
157 }
158 }
159 clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
160 clean::TraitItem(..) => "Trait ",
161 clean::StructItem(..) => "Struct ",
162 clean::UnionItem(..) => "Union ",
163 clean::EnumItem(..) => "Enum ",
164 clean::TypeAliasItem(..) => "Type Alias ",
165 clean::MacroItem(..) => "Macro ",
166 clean::ProcMacroItem(ref mac) => match mac.kind {
167 MacroKind::Bang => "Macro ",
168 MacroKind::Attr => "Attribute Macro ",
169 MacroKind::Derive => "Derive Macro ",
170 },
171 clean::PrimitiveItem(..) => "Primitive Type ",
172 clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
173 clean::ConstantItem(..) => "Constant ",
174 clean::ForeignTypeItem => "Foreign Type ",
175 clean::KeywordItem => "Keyword ",
176 clean::TraitAliasItem(..) => "Trait Alias ",
177 _ => {
178 unreachable!();
180 }
181 };
182 let stability_since_raw =
183 render_stability_since_raw(item.stable_since(cx.tcx()), item.const_stability(cx.tcx()))
184 .maybe_display()
185 .to_string();
186
187 let src_href =
194 if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
195
196 let path_components = if item.is_primitive() || item.is_keyword() {
197 vec![]
198 } else {
199 let cur = &cx.current;
200 let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
201 cur.iter()
202 .enumerate()
203 .take(amt)
204 .map(|(i, component)| PathComponent {
205 path: "../".repeat(cur.len() - i - 1),
206 name: *component,
207 })
208 .collect()
209 };
210
211 let item_vars = ItemVars {
212 typ,
213 name: item.name.as_ref().unwrap().as_str(),
214 item_type: &item.type_().to_string(),
215 path_components,
216 stability_since_raw: &stability_since_raw,
217 src_href: src_href.as_deref(),
218 };
219
220 item_vars.render_into(buf).unwrap();
221
222 match &item.kind {
223 clean::ModuleItem(m) => {
224 write!(buf, "{}", item_module(cx, item, &m.items))
225 }
226 clean::FunctionItem(f) | clean::ForeignFunctionItem(f, _) => {
227 write!(buf, "{}", item_function(cx, item, f))
228 }
229 clean::TraitItem(t) => write!(buf, "{}", item_trait(cx, item, t)),
230 clean::StructItem(s) => {
231 write!(buf, "{}", item_struct(cx, item, s))
232 }
233 clean::UnionItem(s) => write!(buf, "{}", item_union(cx, item, s)),
234 clean::EnumItem(e) => write!(buf, "{}", item_enum(cx, item, e)),
235 clean::TypeAliasItem(t) => {
236 write!(buf, "{}", item_type_alias(cx, item, t))
237 }
238 clean::MacroItem(m) => write!(buf, "{}", item_macro(cx, item, m)),
239 clean::ProcMacroItem(m) => {
240 write!(buf, "{}", item_proc_macro(cx, item, m))
241 }
242 clean::PrimitiveItem(_) => write!(buf, "{}", item_primitive(cx, item)),
243 clean::StaticItem(i) => {
244 write!(buf, "{}", item_static(cx, item, i, None))
245 }
246 clean::ForeignStaticItem(i, safety) => {
247 write!(buf, "{}", item_static(cx, item, i, Some(*safety)))
248 }
249 clean::ConstantItem(ci) => {
250 write!(buf, "{}", item_constant(cx, item, &ci.generics, &ci.type_, &ci.kind))
251 }
252 clean::ForeignTypeItem => {
253 write!(buf, "{}", item_foreign_type(cx, item))
254 }
255 clean::KeywordItem => write!(buf, "{}", item_keyword(cx, item)),
256 clean::TraitAliasItem(ta) => {
257 write!(buf, "{}", item_trait_alias(cx, item, ta))
258 }
259 _ => {
260 unreachable!();
262 }
263 }?;
264
265 let mut types_with_notable_traits = cx.types_with_notable_traits.borrow_mut();
267 if !types_with_notable_traits.is_empty() {
268 write!(
269 buf,
270 r#"<script type="text/json" id="notable-traits-data">{}</script>"#,
271 notable_traits_json(types_with_notable_traits.iter(), cx),
272 )?;
273 types_with_notable_traits.clear();
274 }
275 Ok(())
276 })
277}
278
279fn should_hide_fields(n_fields: usize) -> bool {
281 n_fields > 12
282}
283
284fn toggle_open(mut w: impl fmt::Write, text: impl Display) {
285 write!(
286 w,
287 "<details class=\"toggle type-contents-toggle\">\
288 <summary class=\"hideme\">\
289 <span>Show {text}</span>\
290 </summary>",
291 )
292 .unwrap();
293}
294
295fn toggle_close(mut w: impl fmt::Write) {
296 w.write_str("</details>").unwrap();
297}
298
299trait ItemTemplate<'a, 'cx: 'a>: askama::Template + Display {
300 fn item_and_cx(&self) -> (&'a clean::Item, &'a Context<'cx>);
301}
302
303fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> impl fmt::Display {
304 fmt::from_fn(|w| {
305 write!(w, "{}", document(cx, item, None, HeadingOffset::H2))?;
306
307 let mut not_stripped_items =
308 items.iter().filter(|i| !i.is_stripped()).enumerate().collect::<Vec<_>>();
309
310 fn reorder(ty: ItemType) -> u8 {
312 match ty {
313 ItemType::ExternCrate => 0,
314 ItemType::Import => 1,
315 ItemType::Primitive => 2,
316 ItemType::Module => 3,
317 ItemType::Macro => 4,
318 ItemType::Struct => 5,
319 ItemType::Enum => 6,
320 ItemType::Constant => 7,
321 ItemType::Static => 8,
322 ItemType::Trait => 9,
323 ItemType::Function => 10,
324 ItemType::TypeAlias => 12,
325 ItemType::Union => 13,
326 _ => 14 + ty as u8,
327 }
328 }
329
330 fn cmp(i1: &clean::Item, i2: &clean::Item, tcx: TyCtxt<'_>) -> Ordering {
331 let rty1 = reorder(i1.type_());
332 let rty2 = reorder(i2.type_());
333 if rty1 != rty2 {
334 return rty1.cmp(&rty2);
335 }
336 let is_stable1 =
337 i1.stability(tcx).as_ref().map(|s| s.level.is_stable()).unwrap_or(true);
338 let is_stable2 =
339 i2.stability(tcx).as_ref().map(|s| s.level.is_stable()).unwrap_or(true);
340 if is_stable1 != is_stable2 {
341 return is_stable2.cmp(&is_stable1);
344 }
345 match (i1.name, i2.name) {
346 (Some(name1), Some(name2)) => compare_names(name1.as_str(), name2.as_str()),
347 (Some(_), None) => Ordering::Greater,
348 (None, Some(_)) => Ordering::Less,
349 (None, None) => Ordering::Equal,
350 }
351 }
352
353 let tcx = cx.tcx();
354
355 match cx.shared.module_sorting {
356 ModuleSorting::Alphabetical => {
357 not_stripped_items.sort_by(|(_, i1), (_, i2)| cmp(i1, i2, tcx));
358 }
359 ModuleSorting::DeclarationOrder => {}
360 }
361 not_stripped_items.dedup_by_key(|(idx, i)| {
381 (
382 i.item_id,
383 if i.name.is_some() { Some(full_path(cx, i)) } else { None },
384 i.type_(),
385 if i.is_import() { *idx } else { 0 },
386 )
387 });
388
389 debug!("{not_stripped_items:?}");
390 let mut last_section = None;
391
392 for (_, myitem) in ¬_stripped_items {
393 let my_section = item_ty_to_section(myitem.type_());
394 if Some(my_section) != last_section {
395 if last_section.is_some() {
396 w.write_str(ITEM_TABLE_CLOSE)?;
397 }
398 last_section = Some(my_section);
399 let section_id = my_section.id();
400 let tag =
401 if section_id == "reexports" { REEXPORTS_TABLE_OPEN } else { ITEM_TABLE_OPEN };
402 write!(
403 w,
404 "{}",
405 write_section_heading(my_section.name(), &cx.derive_id(section_id), None, tag)
406 )?;
407 }
408
409 match myitem.kind {
410 clean::ExternCrateItem { ref src } => {
411 use crate::html::format::print_anchor;
412
413 match *src {
414 Some(src) => {
415 write!(
416 w,
417 "<dt><code>{}extern crate {} as {};",
418 visibility_print_with_space(myitem, cx),
419 print_anchor(myitem.item_id.expect_def_id(), src, cx),
420 EscapeBodyTextWithWbr(myitem.name.unwrap().as_str())
421 )?;
422 }
423 None => {
424 write!(
425 w,
426 "<dt><code>{}extern crate {};",
427 visibility_print_with_space(myitem, cx),
428 print_anchor(
429 myitem.item_id.expect_def_id(),
430 myitem.name.unwrap(),
431 cx
432 )
433 )?;
434 }
435 }
436 w.write_str("</code></dt>")?;
437 }
438
439 clean::ImportItem(ref import) => {
440 let stab_tags = import.source.did.map_or_else(String::new, |import_def_id| {
441 print_extra_info_tags(tcx, myitem, item, Some(import_def_id)).to_string()
442 });
443
444 let id = match import.kind {
445 clean::ImportKind::Simple(s) => {
446 format!(" id=\"{}\"", cx.derive_id(format!("reexport.{s}")))
447 }
448 clean::ImportKind::Glob => String::new(),
449 };
450 write!(
451 w,
452 "<dt{id}>\
453 <code>"
454 )?;
455 render_attributes_in_code(w, myitem, "", cx);
456 write!(
457 w,
458 "{vis}{imp}</code>{stab_tags}\
459 </dt>",
460 vis = visibility_print_with_space(myitem, cx),
461 imp = import.print(cx)
462 )?;
463 }
464
465 _ => {
466 if myitem.name.is_none() {
467 continue;
468 }
469
470 let unsafety_flag = match myitem.kind {
471 clean::FunctionItem(_) | clean::ForeignFunctionItem(..)
472 if myitem.fn_header(tcx).unwrap().safety
473 == hir::HeaderSafety::Normal(hir::Safety::Unsafe) =>
474 {
475 "<sup title=\"unsafe function\">âš </sup>"
476 }
477 clean::ForeignStaticItem(_, hir::Safety::Unsafe) => {
478 "<sup title=\"unsafe static\">âš </sup>"
479 }
480 _ => "",
481 };
482
483 let visibility_and_hidden = match myitem.visibility(tcx) {
484 Some(ty::Visibility::Restricted(_)) => {
485 if myitem.is_doc_hidden() {
486 "<span title=\"Restricted Visibility\"> 🔒</span><span title=\"Hidden item\">👻</span> "
488 } else {
489 "<span title=\"Restricted Visibility\"> 🔒</span> "
490 }
491 }
492 _ if myitem.is_doc_hidden() => {
493 "<span title=\"Hidden item\"> 👻</span> "
494 }
495 _ => "",
496 };
497
498 let docs =
499 MarkdownSummaryLine(&myitem.doc_value(), &myitem.links(cx)).into_string();
500 let (docs_before, docs_after) =
501 if docs.is_empty() { ("", "") } else { ("<dd>", "</dd>") };
502 write!(
503 w,
504 "<dt>\
505 <a class=\"{class}\" href=\"{href}\" title=\"{title1} {title2}\">\
506 {name}\
507 </a>\
508 {visibility_and_hidden}\
509 {unsafety_flag}\
510 {stab_tags}\
511 </dt>\
512 {docs_before}{docs}{docs_after}",
513 name = EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()),
514 visibility_and_hidden = visibility_and_hidden,
515 stab_tags = print_extra_info_tags(tcx, myitem, item, None),
516 class = myitem.type_(),
517 unsafety_flag = unsafety_flag,
518 href = print_item_path(myitem.type_(), myitem.name.unwrap().as_str()),
519 title1 = myitem.type_(),
520 title2 = full_path(cx, myitem),
521 )?;
522 }
523 }
524 }
525
526 if last_section.is_some() {
527 w.write_str(ITEM_TABLE_CLOSE)?;
528 }
529 Ok(())
530 })
531}
532
533fn print_extra_info_tags(
536 tcx: TyCtxt<'_>,
537 item: &clean::Item,
538 parent: &clean::Item,
539 import_def_id: Option<DefId>,
540) -> impl Display {
541 fmt::from_fn(move |f| {
542 fn tag_html(class: &str, title: &str, contents: &str) -> impl Display {
543 fmt::from_fn(move |f| {
544 write!(
545 f,
546 r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
547 title = Escape(title),
548 )
549 })
550 }
551
552 let deprecation = import_def_id
554 .map_or_else(|| item.deprecation(tcx), |import_did| tcx.lookup_deprecation(import_did));
555 if let Some(depr) = deprecation {
556 let message = if depr.is_in_effect() { "Deprecated" } else { "Deprecation planned" };
557 write!(f, "{}", tag_html("deprecated", "", message))?;
558 }
559
560 let stability = import_def_id
563 .map_or_else(|| item.stability(tcx), |import_did| tcx.lookup_stability(import_did));
564 if stability.is_some_and(|s| s.is_unstable() && s.feature != sym::rustc_private) {
565 write!(f, "{}", tag_html("unstable", "", "Experimental"))?;
566 }
567
568 let cfg = match (&item.cfg, parent.cfg.as_ref()) {
569 (Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
570 (cfg, _) => cfg.as_deref().cloned(),
571 };
572
573 debug!(
574 "Portability name={name:?} {cfg:?} - {parent_cfg:?} = {cfg:?}",
575 name = item.name,
576 cfg = item.cfg,
577 parent_cfg = parent.cfg
578 );
579 if let Some(ref cfg) = cfg {
580 write!(
581 f,
582 "{}",
583 tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html())
584 )
585 } else {
586 Ok(())
587 }
588 })
589}
590
591fn item_function(cx: &Context<'_>, it: &clean::Item, f: &clean::Function) -> impl fmt::Display {
592 fmt::from_fn(|w| {
593 let tcx = cx.tcx();
594 let header = it.fn_header(tcx).expect("printing a function which isn't a function");
595 debug!(
596 "item_function/const: {:?} {:?} {:?} {:?}",
597 it.name,
598 &header.constness,
599 it.stable_since(tcx),
600 it.const_stability(tcx),
601 );
602 let constness = print_constness_with_space(
603 &header.constness,
604 it.stable_since(tcx),
605 it.const_stability(tcx),
606 );
607 let safety = header.safety.print_with_space();
608 let abi = print_abi_with_space(header.abi).to_string();
609 let asyncness = header.asyncness.print_with_space();
610 let visibility = visibility_print_with_space(it, cx).to_string();
611 let name = it.name.unwrap();
612
613 let generics_len = format!("{:#}", f.generics.print(cx)).len();
614 let header_len = "fn ".len()
615 + visibility.len()
616 + constness.len()
617 + asyncness.len()
618 + safety.len()
619 + abi.len()
620 + name.as_str().len()
621 + generics_len;
622
623 let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
624
625 wrap_item(w, |w| {
626 render_attributes_in_code(w, it, "", cx);
627 write!(
628 w,
629 "{vis}{constness}{asyncness}{safety}{abi}fn \
630 {name}{generics}{decl}{notable_traits}{where_clause}",
631 vis = visibility,
632 constness = constness,
633 asyncness = asyncness,
634 safety = safety,
635 abi = abi,
636 name = name,
637 generics = f.generics.print(cx),
638 where_clause =
639 print_where_clause(&f.generics, cx, 0, Ending::Newline).maybe_display(),
640 decl = f.decl.full_print(header_len, 0, cx),
641 )
642 })?;
643 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
644 })
645}
646
647fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt::Display {
648 fmt::from_fn(|w| {
649 let tcx = cx.tcx();
650 let bounds = print_bounds(&t.bounds, false, cx);
651 let required_types =
652 t.items.iter().filter(|m| m.is_required_associated_type()).collect::<Vec<_>>();
653 let provided_types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
654 let required_consts =
655 t.items.iter().filter(|m| m.is_required_associated_const()).collect::<Vec<_>>();
656 let provided_consts =
657 t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
658 let required_methods = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
659 let provided_methods = t.items.iter().filter(|m| m.is_method()).collect::<Vec<_>>();
660 let count_types = required_types.len() + provided_types.len();
661 let count_consts = required_consts.len() + provided_consts.len();
662 let count_methods = required_methods.len() + provided_methods.len();
663 let must_implement_one_of_functions = &tcx.trait_def(t.def_id).must_implement_one_of;
664
665 wrap_item(w, |mut w| {
667 render_attributes_in_code(&mut w, it, "", cx);
668 write!(
669 w,
670 "{vis}{safety}{is_auto}trait {name}{generics}{bounds}",
671 vis = visibility_print_with_space(it, cx),
672 safety = t.safety(tcx).print_with_space(),
673 is_auto = if t.is_auto(tcx) { "auto " } else { "" },
674 name = it.name.unwrap(),
675 generics = t.generics.print(cx),
676 )?;
677
678 if !t.generics.where_predicates.is_empty() {
679 write!(
680 w,
681 "{}",
682 print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display()
683 )?;
684 } else {
685 w.write_char(' ')?;
686 }
687
688 if t.items.is_empty() {
689 w.write_str("{ }")
690 } else {
691 w.write_str("{\n")?;
693 let mut toggle = false;
694
695 if should_hide_fields(count_types) {
697 toggle = true;
698 toggle_open(
699 &mut w,
700 format_args!(
701 "{} associated items",
702 count_types + count_consts + count_methods
703 ),
704 );
705 }
706 for types in [&required_types, &provided_types] {
707 for t in types {
708 writeln!(
709 w,
710 "{};",
711 render_assoc_item(
712 t,
713 AssocItemLink::Anchor(None),
714 ItemType::Trait,
715 cx,
716 RenderMode::Normal,
717 )
718 )?;
719 }
720 }
721 if !toggle && should_hide_fields(count_types + count_consts) {
726 toggle = true;
727 toggle_open(
728 &mut w,
729 format_args!(
730 "{count_consts} associated constant{plural_const} and \
731 {count_methods} method{plural_method}",
732 plural_const = pluralize(count_consts),
733 plural_method = pluralize(count_methods),
734 ),
735 );
736 }
737 if count_types != 0 && (count_consts != 0 || count_methods != 0) {
738 w.write_str("\n")?;
739 }
740 for consts in [&required_consts, &provided_consts] {
741 for c in consts {
742 writeln!(
743 w,
744 "{};",
745 render_assoc_item(
746 c,
747 AssocItemLink::Anchor(None),
748 ItemType::Trait,
749 cx,
750 RenderMode::Normal,
751 )
752 )?;
753 }
754 }
755 if !toggle && should_hide_fields(count_methods) {
756 toggle = true;
757 toggle_open(&mut w, format_args!("{count_methods} methods"));
758 }
759 if count_consts != 0 && count_methods != 0 {
760 w.write_str("\n")?;
761 }
762
763 if !required_methods.is_empty() {
764 writeln!(w, " // Required method{}", pluralize(required_methods.len()))?;
765 }
766 for (pos, m) in required_methods.iter().enumerate() {
767 writeln!(
768 w,
769 "{};",
770 render_assoc_item(
771 m,
772 AssocItemLink::Anchor(None),
773 ItemType::Trait,
774 cx,
775 RenderMode::Normal,
776 )
777 )?;
778
779 if pos < required_methods.len() - 1 {
780 w.write_str("<span class=\"item-spacer\"></span>")?;
781 }
782 }
783 if !required_methods.is_empty() && !provided_methods.is_empty() {
784 w.write_str("\n")?;
785 }
786
787 if !provided_methods.is_empty() {
788 writeln!(w, " // Provided method{}", pluralize(provided_methods.len()))?;
789 }
790 for (pos, m) in provided_methods.iter().enumerate() {
791 writeln!(
792 w,
793 "{} {{ ... }}",
794 render_assoc_item(
795 m,
796 AssocItemLink::Anchor(None),
797 ItemType::Trait,
798 cx,
799 RenderMode::Normal,
800 )
801 )?;
802
803 if pos < provided_methods.len() - 1 {
804 w.write_str("<span class=\"item-spacer\"></span>")?;
805 }
806 }
807 if toggle {
808 toggle_close(&mut w);
809 }
810 w.write_str("}")
811 }
812 })?;
813
814 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
816
817 fn trait_item(cx: &Context<'_>, m: &clean::Item, t: &clean::Item) -> impl fmt::Display {
818 fmt::from_fn(|w| {
819 let name = m.name.unwrap();
820 info!("Documenting {name} on {ty_name:?}", ty_name = t.name);
821 let item_type = m.type_();
822 let id = cx.derive_id(format!("{item_type}.{name}"));
823
824 let content = document_full(m, cx, HeadingOffset::H5).to_string();
825
826 let toggled = !content.is_empty();
827 if toggled {
828 let method_toggle_class =
829 if item_type.is_method() { " method-toggle" } else { "" };
830 write!(w, "<details class=\"toggle{method_toggle_class}\" open><summary>")?;
831 }
832 write!(
833 w,
834 "<section id=\"{id}\" class=\"method\">\
835 {}\
836 <h4 class=\"code-header\">{}</h4></section>",
837 render_rightside(cx, m, RenderMode::Normal),
838 render_assoc_item(
839 m,
840 AssocItemLink::Anchor(Some(&id)),
841 ItemType::Impl,
842 cx,
843 RenderMode::Normal,
844 )
845 )?;
846 document_item_info(cx, m, Some(t)).render_into(w).unwrap();
847 if toggled {
848 write!(w, "</summary>{content}</details>")?;
849 }
850 Ok(())
851 })
852 }
853
854 if !required_consts.is_empty() {
855 write!(
856 w,
857 "{}",
858 write_section_heading(
859 "Required Associated Constants",
860 "required-associated-consts",
861 None,
862 "<div class=\"methods\">",
863 )
864 )?;
865 for t in required_consts {
866 write!(w, "{}", trait_item(cx, t, it))?;
867 }
868 w.write_str("</div>")?;
869 }
870 if !provided_consts.is_empty() {
871 write!(
872 w,
873 "{}",
874 write_section_heading(
875 "Provided Associated Constants",
876 "provided-associated-consts",
877 None,
878 "<div class=\"methods\">",
879 )
880 )?;
881 for t in provided_consts {
882 write!(w, "{}", trait_item(cx, t, it))?;
883 }
884 w.write_str("</div>")?;
885 }
886
887 if !required_types.is_empty() {
888 write!(
889 w,
890 "{}",
891 write_section_heading(
892 "Required Associated Types",
893 "required-associated-types",
894 None,
895 "<div class=\"methods\">",
896 )
897 )?;
898 for t in required_types {
899 write!(w, "{}", trait_item(cx, t, it))?;
900 }
901 w.write_str("</div>")?;
902 }
903 if !provided_types.is_empty() {
904 write!(
905 w,
906 "{}",
907 write_section_heading(
908 "Provided Associated Types",
909 "provided-associated-types",
910 None,
911 "<div class=\"methods\">",
912 )
913 )?;
914 for t in provided_types {
915 write!(w, "{}", trait_item(cx, t, it))?;
916 }
917 w.write_str("</div>")?;
918 }
919
920 if !required_methods.is_empty() || must_implement_one_of_functions.is_some() {
922 write!(
923 w,
924 "{}",
925 write_section_heading(
926 "Required Methods",
927 "required-methods",
928 None,
929 "<div class=\"methods\">",
930 )
931 )?;
932
933 if let Some(list) = must_implement_one_of_functions.as_deref() {
934 write!(
935 w,
936 "<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
937 fmt::from_fn(|f| list.iter().joined("`, `", f)),
938 )?;
939 }
940
941 for m in required_methods {
942 write!(w, "{}", trait_item(cx, m, it))?;
943 }
944 w.write_str("</div>")?;
945 }
946 if !provided_methods.is_empty() {
947 write!(
948 w,
949 "{}",
950 write_section_heading(
951 "Provided Methods",
952 "provided-methods",
953 None,
954 "<div class=\"methods\">",
955 )
956 )?;
957 for m in provided_methods {
958 write!(w, "{}", trait_item(cx, m, it))?;
959 }
960 w.write_str("</div>")?;
961 }
962
963 write!(
965 w,
966 "{}",
967 render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
968 )?;
969
970 let mut extern_crates = FxIndexSet::default();
971
972 if !t.is_dyn_compatible(cx.tcx()) {
973 write!(
974 w,
975 "{}",
976 write_section_heading(
977 "Dyn Compatibility",
978 "dyn-compatibility",
979 None,
980 format!(
981 "<div class=\"dyn-compatibility-info\"><p>This trait is <b>not</b> \
982 <a href=\"{base}/reference/items/traits.html#dyn-compatibility\">dyn compatible</a>.</p>\
983 <p><i>In older versions of Rust, dyn compatibility was called \"object safety\", \
984 so this trait is not object safe.</i></p></div>",
985 base = crate::clean::utils::DOC_RUST_LANG_ORG_VERSION
986 ),
987 ),
988 )?;
989 }
990
991 if let Some(implementors) = cx.shared.cache.implementors.get(&it.item_id.expect_def_id()) {
992 let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
995 for implementor in implementors {
996 if let Some(did) =
997 implementor.inner_impl().for_.without_borrowed_ref().def_id(&cx.shared.cache)
998 && !did.is_local()
999 {
1000 extern_crates.insert(did.krate);
1001 }
1002 match implementor.inner_impl().for_.without_borrowed_ref() {
1003 clean::Type::Path { path } if !path.is_assoc_ty() => {
1004 let did = path.def_id();
1005 let &mut (prev_did, ref mut has_duplicates) =
1006 implementor_dups.entry(path.last()).or_insert((did, false));
1007 if prev_did != did {
1008 *has_duplicates = true;
1009 }
1010 }
1011 _ => {}
1012 }
1013 }
1014
1015 let (local, mut foreign) =
1016 implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cx));
1017
1018 let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
1019 local.iter().partition(|i| i.inner_impl().kind.is_auto());
1020
1021 synthetic.sort_by_cached_key(|i| ImplString::new(i, cx));
1022 concrete.sort_by_cached_key(|i| ImplString::new(i, cx));
1023 foreign.sort_by_cached_key(|i| ImplString::new(i, cx));
1024
1025 if !foreign.is_empty() {
1026 write!(
1027 w,
1028 "{}",
1029 write_section_heading(
1030 "Implementations on Foreign Types",
1031 "foreign-impls",
1032 None,
1033 ""
1034 )
1035 )?;
1036
1037 for implementor in foreign {
1038 let provided_methods = implementor.inner_impl().provided_trait_methods(tcx);
1039 let assoc_link =
1040 AssocItemLink::GotoSource(implementor.impl_item.item_id, &provided_methods);
1041 write!(
1042 w,
1043 "{}",
1044 render_impl(
1045 cx,
1046 implementor,
1047 it,
1048 assoc_link,
1049 RenderMode::Normal,
1050 None,
1051 &[],
1052 ImplRenderingParameters {
1053 show_def_docs: false,
1054 show_default_items: false,
1055 show_non_assoc_items: true,
1056 toggle_open_by_default: false,
1057 },
1058 )
1059 )?;
1060 }
1061 }
1062
1063 write!(
1064 w,
1065 "{}",
1066 write_section_heading(
1067 "Implementors",
1068 "implementors",
1069 None,
1070 "<div id=\"implementors-list\">",
1071 )
1072 )?;
1073 for implementor in concrete {
1074 write!(w, "{}", render_implementor(cx, implementor, it, &implementor_dups, &[]))?;
1075 }
1076 w.write_str("</div>")?;
1077
1078 if t.is_auto(tcx) {
1079 write!(
1080 w,
1081 "{}",
1082 write_section_heading(
1083 "Auto implementors",
1084 "synthetic-implementors",
1085 None,
1086 "<div id=\"synthetic-implementors-list\">",
1087 )
1088 )?;
1089 for implementor in synthetic {
1090 write!(
1091 w,
1092 "{}",
1093 render_implementor(
1094 cx,
1095 implementor,
1096 it,
1097 &implementor_dups,
1098 &collect_paths_for_type(
1099 &implementor.inner_impl().for_,
1100 &cx.shared.cache,
1101 ),
1102 )
1103 )?;
1104 }
1105 w.write_str("</div>")?;
1106 }
1107 } else {
1108 write!(
1111 w,
1112 "{}",
1113 write_section_heading(
1114 "Implementors",
1115 "implementors",
1116 None,
1117 "<div id=\"implementors-list\"></div>",
1118 )
1119 )?;
1120
1121 if t.is_auto(tcx) {
1122 write!(
1123 w,
1124 "{}",
1125 write_section_heading(
1126 "Auto implementors",
1127 "synthetic-implementors",
1128 None,
1129 "<div id=\"synthetic-implementors-list\"></div>",
1130 )
1131 )?;
1132 }
1133 }
1134
1135 let mut js_src_path: UrlPartsBuilder =
1207 iter::repeat_n("..", cx.current.len()).chain(iter::once("trait.impl")).collect();
1208 if let Some(did) = it.item_id.as_def_id()
1209 && let get_extern = { || cx.shared.cache.external_paths.get(&did).map(|s| &s.0) }
1210 && let Some(fqp) = cx.shared.cache.exact_paths.get(&did).or_else(get_extern)
1211 {
1212 js_src_path.extend(fqp[..fqp.len() - 1].iter().copied());
1213 js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap()));
1214 } else {
1215 js_src_path.extend(cx.current.iter().copied());
1216 js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
1217 }
1218 let extern_crates = fmt::from_fn(|f| {
1219 if !extern_crates.is_empty() {
1220 f.write_str(" data-ignore-extern-crates=\"")?;
1221 extern_crates.iter().map(|&cnum| tcx.crate_name(cnum)).joined(",", f)?;
1222 f.write_str("\"")?;
1223 }
1224 Ok(())
1225 });
1226 write!(
1227 w,
1228 "<script src=\"{src}\"{extern_crates} async></script>",
1229 src = js_src_path.finish()
1230 )
1231 })
1232}
1233
1234fn item_trait_alias(
1235 cx: &Context<'_>,
1236 it: &clean::Item,
1237 t: &clean::TraitAlias,
1238) -> impl fmt::Display {
1239 fmt::from_fn(|w| {
1240 wrap_item(w, |w| {
1241 render_attributes_in_code(w, it, "", cx);
1242 write!(
1243 w,
1244 "trait {name}{generics} = {bounds}{where_clause};",
1245 name = it.name.unwrap(),
1246 generics = t.generics.print(cx),
1247 bounds = print_bounds(&t.bounds, true, cx),
1248 where_clause =
1249 print_where_clause(&t.generics, cx, 0, Ending::NoNewline).maybe_display(),
1250 )
1251 })?;
1252
1253 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1254 write!(
1259 w,
1260 "{}",
1261 render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1262 )
1263 })
1264}
1265
1266fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) -> impl fmt::Display {
1267 fmt::from_fn(|w| {
1268 wrap_item(w, |w| {
1269 render_attributes_in_code(w, it, "", cx);
1270 write!(
1271 w,
1272 "{vis}type {name}{generics}{where_clause} = {type_};",
1273 vis = visibility_print_with_space(it, cx),
1274 name = it.name.unwrap(),
1275 generics = t.generics.print(cx),
1276 where_clause =
1277 print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display(),
1278 type_ = t.type_.print(cx),
1279 )
1280 })?;
1281
1282 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1283
1284 if let Some(inner_type) = &t.inner_type {
1285 write!(w, "{}", write_section_heading("Aliased Type", "aliased-type", None, ""),)?;
1286
1287 match inner_type {
1288 clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive } => {
1289 let ty = cx.tcx().type_of(it.def_id().unwrap()).instantiate_identity();
1290 let enum_def_id = ty.ty_adt_def().unwrap().did();
1291
1292 DisplayEnum {
1293 variants,
1294 generics: &t.generics,
1295 is_non_exhaustive: *is_non_exhaustive,
1296 def_id: enum_def_id,
1297 }
1298 .render_into(cx, it, true, w)?;
1299 }
1300 clean::TypeAliasInnerType::Union { fields } => {
1301 let ty = cx.tcx().type_of(it.def_id().unwrap()).instantiate_identity();
1302 let union_def_id = ty.ty_adt_def().unwrap().did();
1303
1304 ItemUnion {
1305 cx,
1306 it,
1307 fields,
1308 generics: &t.generics,
1309 is_type_alias: true,
1310 def_id: union_def_id,
1311 }
1312 .render_into(w)?;
1313 }
1314 clean::TypeAliasInnerType::Struct { ctor_kind, fields } => {
1315 let ty = cx.tcx().type_of(it.def_id().unwrap()).instantiate_identity();
1316 let struct_def_id = ty.ty_adt_def().unwrap().did();
1317
1318 DisplayStruct {
1319 ctor_kind: *ctor_kind,
1320 generics: &t.generics,
1321 fields,
1322 def_id: struct_def_id,
1323 }
1324 .render_into(cx, it, true, w)?;
1325 }
1326 }
1327 } else {
1328 let def_id = it.item_id.expect_def_id();
1329 write!(
1334 w,
1335 "{}{}",
1336 render_assoc_items(cx, it, def_id, AssocItemRender::All),
1337 document_type_layout(cx, def_id)
1338 )?;
1339 }
1340
1341 let cache = &cx.shared.cache;
1414 if let Some(target_did) = t.type_.def_id(cache)
1415 && let get_extern = { || cache.external_paths.get(&target_did) }
1416 && let Some(&(ref target_fqp, target_type)) =
1417 cache.paths.get(&target_did).or_else(get_extern)
1418 && target_type.is_adt() && let Some(self_did) = it.item_id.as_def_id()
1420 && let get_local = { || cache.paths.get(&self_did).map(|(p, _)| p) }
1421 && let Some(self_fqp) = cache.exact_paths.get(&self_did).or_else(get_local)
1422 {
1423 let mut js_src_path: UrlPartsBuilder =
1424 iter::repeat_n("..", cx.current.len()).chain(iter::once("type.impl")).collect();
1425 js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
1426 js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
1427 let self_path = join_path_syms(self_fqp);
1428 write!(
1429 w,
1430 "<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",
1431 src = js_src_path.finish(),
1432 )?;
1433 }
1434 Ok(())
1435 })
1436}
1437
1438item_template!(
1439 #[template(path = "item_union.html")]
1440 struct ItemUnion<'a, 'cx> {
1441 cx: &'a Context<'cx>,
1442 it: &'a clean::Item,
1443 fields: &'a [clean::Item],
1444 generics: &'a clean::Generics,
1445 is_type_alias: bool,
1446 def_id: DefId,
1447 },
1448 methods = [document, document_type_layout, render_assoc_items]
1449);
1450
1451impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1452 fn render_union(&self) -> impl Display {
1453 render_union(
1454 self.it,
1455 Some(self.generics),
1456 self.fields,
1457 self.def_id,
1458 self.is_type_alias,
1459 self.cx,
1460 )
1461 }
1462
1463 fn print_field_attrs(&self, field: &'a clean::Item) -> impl Display {
1464 fmt::from_fn(move |w| {
1465 render_attributes_in_code(w, field, "", self.cx);
1466 Ok(())
1467 })
1468 }
1469
1470 fn document_field(&self, field: &'a clean::Item) -> impl Display {
1471 document(self.cx, field, Some(self.it), HeadingOffset::H3)
1472 }
1473
1474 fn stability_field(&self, field: &clean::Item) -> Option<String> {
1475 field.stability_class(self.cx.tcx())
1476 }
1477
1478 fn print_ty(&self, ty: &'a clean::Type) -> impl Display {
1479 ty.print(self.cx)
1480 }
1481
1482 fn fields_iter(&self) -> impl Iterator<Item = (&'a clean::Item, &'a clean::Type)> {
1489 self.fields.iter().filter_map(|f| match f.kind {
1490 clean::StructFieldItem(ref ty) => Some((f, ty)),
1491 _ => None,
1492 })
1493 }
1494}
1495
1496fn item_union(cx: &Context<'_>, it: &clean::Item, s: &clean::Union) -> impl fmt::Display {
1497 fmt::from_fn(|w| {
1498 ItemUnion {
1499 cx,
1500 it,
1501 fields: &s.fields,
1502 generics: &s.generics,
1503 is_type_alias: false,
1504 def_id: it.def_id().unwrap(),
1505 }
1506 .render_into(w)?;
1507 Ok(())
1508 })
1509}
1510
1511fn print_tuple_struct_fields(cx: &Context<'_>, s: &[clean::Item]) -> impl Display {
1512 fmt::from_fn(|f| {
1513 if !s.is_empty()
1514 && s.iter().all(|field| {
1515 matches!(field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
1516 })
1517 {
1518 return f.write_str("<span class=\"comment\">/* private fields */</span>");
1519 }
1520
1521 s.iter()
1522 .map(|ty| {
1523 fmt::from_fn(|f| match ty.kind {
1524 clean::StrippedItem(box clean::StructFieldItem(_)) => f.write_str("_"),
1525 clean::StructFieldItem(ref ty) => write!(f, "{}", ty.print(cx)),
1526 _ => unreachable!(),
1527 })
1528 })
1529 .joined(", ", f)
1530 })
1531}
1532
1533struct DisplayEnum<'clean> {
1534 variants: &'clean IndexVec<VariantIdx, clean::Item>,
1535 generics: &'clean clean::Generics,
1536 is_non_exhaustive: bool,
1537 def_id: DefId,
1538}
1539
1540impl<'clean> DisplayEnum<'clean> {
1541 fn render_into<W: fmt::Write>(
1542 self,
1543 cx: &Context<'_>,
1544 it: &clean::Item,
1545 is_type_alias: bool,
1546 w: &mut W,
1547 ) -> fmt::Result {
1548 let non_stripped_variant_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
1549 let variants_len = self.variants.len();
1550 let has_stripped_entries = variants_len != non_stripped_variant_count;
1551
1552 wrap_item(w, |w| {
1553 if is_type_alias {
1554 render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Enum);
1556 } else {
1557 render_attributes_in_code(w, it, "", cx);
1558 }
1559 write!(
1560 w,
1561 "{}enum {}{}{}",
1562 visibility_print_with_space(it, cx),
1563 it.name.unwrap(),
1564 self.generics.print(cx),
1565 render_enum_fields(
1566 cx,
1567 Some(self.generics),
1568 self.variants,
1569 non_stripped_variant_count,
1570 has_stripped_entries,
1571 self.is_non_exhaustive,
1572 self.def_id,
1573 ),
1574 )
1575 })?;
1576
1577 let def_id = it.item_id.expect_def_id();
1578 let layout_def_id = if is_type_alias {
1579 self.def_id
1580 } else {
1581 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1582 def_id
1585 };
1586
1587 if non_stripped_variant_count != 0 {
1588 write!(w, "{}", item_variants(cx, it, self.variants, self.def_id))?;
1589 }
1590 write!(
1591 w,
1592 "{}{}",
1593 render_assoc_items(cx, it, def_id, AssocItemRender::All),
1594 document_type_layout(cx, layout_def_id)
1595 )
1596 }
1597}
1598
1599fn item_enum(cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) -> impl fmt::Display {
1600 fmt::from_fn(|w| {
1601 DisplayEnum {
1602 variants: &e.variants,
1603 generics: &e.generics,
1604 is_non_exhaustive: it.is_non_exhaustive(),
1605 def_id: it.def_id().unwrap(),
1606 }
1607 .render_into(cx, it, false, w)
1608 })
1609}
1610
1611fn should_show_enum_discriminant(
1615 cx: &Context<'_>,
1616 enum_def_id: DefId,
1617 variants: &IndexVec<VariantIdx, clean::Item>,
1618) -> bool {
1619 let mut has_variants_with_value = false;
1620 for variant in variants {
1621 if let clean::VariantItem(ref var) = variant.kind
1622 && matches!(var.kind, clean::VariantKind::CLike)
1623 {
1624 has_variants_with_value |= var.discriminant.is_some();
1625 } else {
1626 return false;
1627 }
1628 }
1629 if has_variants_with_value {
1630 return true;
1631 }
1632 let repr = cx.tcx().adt_def(enum_def_id).repr();
1633 repr.c() || repr.int.is_some()
1634}
1635
1636fn display_c_like_variant(
1637 cx: &Context<'_>,
1638 item: &clean::Item,
1639 variant: &clean::Variant,
1640 index: VariantIdx,
1641 should_show_enum_discriminant: bool,
1642 enum_def_id: DefId,
1643) -> impl fmt::Display {
1644 fmt::from_fn(move |w| {
1645 let name = item.name.unwrap();
1646 if let Some(ref value) = variant.discriminant {
1647 write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true))?;
1648 } else if should_show_enum_discriminant {
1649 let adt_def = cx.tcx().adt_def(enum_def_id);
1650 let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
1651 write!(w, "{} = {}", name.as_str(), discr)?;
1654 } else {
1655 write!(w, "{name}")?;
1656 }
1657 Ok(())
1658 })
1659}
1660
1661fn render_enum_fields(
1662 cx: &Context<'_>,
1663 g: Option<&clean::Generics>,
1664 variants: &IndexVec<VariantIdx, clean::Item>,
1665 count_variants: usize,
1666 has_stripped_entries: bool,
1667 is_non_exhaustive: bool,
1668 enum_def_id: DefId,
1669) -> impl fmt::Display {
1670 fmt::from_fn(move |w| {
1671 let should_show_enum_discriminant =
1672 should_show_enum_discriminant(cx, enum_def_id, variants);
1673 if let Some(generics) = g
1674 && let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
1675 {
1676 write!(w, "{where_clause}")?;
1677 } else {
1678 w.write_char(' ')?;
1680 }
1681
1682 let variants_stripped = has_stripped_entries;
1683 if count_variants == 0 && !variants_stripped {
1684 w.write_str("{}")
1685 } else {
1686 w.write_str("{\n")?;
1687 let toggle = should_hide_fields(count_variants);
1688 if toggle {
1689 toggle_open(&mut *w, format_args!("{count_variants} variants"));
1690 }
1691 const TAB: &str = " ";
1692 for (index, v) in variants.iter_enumerated() {
1693 if v.is_stripped() {
1694 continue;
1695 }
1696 render_attributes_in_code(w, v, TAB, cx);
1697 w.write_str(TAB)?;
1698 match v.kind {
1699 clean::VariantItem(ref var) => match var.kind {
1700 clean::VariantKind::CLike => {
1701 write!(
1702 w,
1703 "{}",
1704 display_c_like_variant(
1705 cx,
1706 v,
1707 var,
1708 index,
1709 should_show_enum_discriminant,
1710 enum_def_id,
1711 )
1712 )?;
1713 }
1714 clean::VariantKind::Tuple(ref s) => {
1715 write!(w, "{}({})", v.name.unwrap(), print_tuple_struct_fields(cx, s))?;
1716 }
1717 clean::VariantKind::Struct(ref s) => {
1718 write!(
1719 w,
1720 "{}",
1721 render_struct(v, None, None, &s.fields, TAB, false, cx)
1722 )?;
1723 }
1724 },
1725 _ => unreachable!(),
1726 }
1727 w.write_str(",\n")?;
1728 }
1729
1730 if variants_stripped && !is_non_exhaustive {
1731 w.write_str(" <span class=\"comment\">// some variants omitted</span>\n")?;
1732 }
1733 if toggle {
1734 toggle_close(&mut *w);
1735 }
1736 w.write_str("}")
1737 }
1738 })
1739}
1740
1741fn item_variants(
1742 cx: &Context<'_>,
1743 it: &clean::Item,
1744 variants: &IndexVec<VariantIdx, clean::Item>,
1745 enum_def_id: DefId,
1746) -> impl fmt::Display {
1747 fmt::from_fn(move |w| {
1748 let tcx = cx.tcx();
1749 write!(
1750 w,
1751 "{}",
1752 write_section_heading(
1753 &format!("Variants{}", document_non_exhaustive_header(it)),
1754 "variants",
1755 Some("variants"),
1756 format!("{}<div class=\"variants\">", document_non_exhaustive(it)),
1757 ),
1758 )?;
1759
1760 let should_show_enum_discriminant =
1761 should_show_enum_discriminant(cx, enum_def_id, variants);
1762 for (index, variant) in variants.iter_enumerated() {
1763 if variant.is_stripped() {
1764 continue;
1765 }
1766 let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
1767 write!(
1768 w,
1769 "<section id=\"{id}\" class=\"variant\">\
1770 <a href=\"#{id}\" class=\"anchor\">§</a>\
1771 {}\
1772 <h3 class=\"code-header\">",
1773 render_stability_since_raw_with_extra(
1774 variant.stable_since(tcx),
1775 variant.const_stability(tcx),
1776 " rightside",
1777 )
1778 .maybe_display()
1779 )?;
1780 render_attributes_in_code(w, variant, "", cx);
1781 if let clean::VariantItem(ref var) = variant.kind
1782 && let clean::VariantKind::CLike = var.kind
1783 {
1784 write!(
1785 w,
1786 "{}",
1787 display_c_like_variant(
1788 cx,
1789 variant,
1790 var,
1791 index,
1792 should_show_enum_discriminant,
1793 enum_def_id,
1794 )
1795 )?;
1796 } else {
1797 w.write_str(variant.name.unwrap().as_str())?;
1798 }
1799
1800 let clean::VariantItem(variant_data) = &variant.kind else { unreachable!() };
1801
1802 if let clean::VariantKind::Tuple(ref s) = variant_data.kind {
1803 write!(w, "({})", print_tuple_struct_fields(cx, s))?;
1804 }
1805 w.write_str("</h3></section>")?;
1806
1807 write!(w, "{}", document(cx, variant, Some(it), HeadingOffset::H4))?;
1808
1809 let heading_and_fields = match &variant_data.kind {
1810 clean::VariantKind::Struct(s) => {
1811 if s.fields.iter().any(|f| !f.is_doc_hidden()) {
1813 Some(("Fields", &s.fields))
1814 } else {
1815 None
1816 }
1817 }
1818 clean::VariantKind::Tuple(fields) => {
1819 if fields.iter().any(|f| !f.doc_value().is_empty()) {
1822 Some(("Tuple Fields", fields))
1823 } else {
1824 None
1825 }
1826 }
1827 clean::VariantKind::CLike => None,
1828 };
1829
1830 if let Some((heading, fields)) = heading_and_fields {
1831 let variant_id =
1832 cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap()));
1833 write!(
1834 w,
1835 "<div class=\"sub-variant\" id=\"{variant_id}\">\
1836 <h4>{heading}</h4>\
1837 {}",
1838 document_non_exhaustive(variant)
1839 )?;
1840 for field in fields {
1841 match field.kind {
1842 clean::StrippedItem(box clean::StructFieldItem(_)) => {}
1843 clean::StructFieldItem(ref ty) => {
1844 let id = cx.derive_id(format!(
1845 "variant.{}.field.{}",
1846 variant.name.unwrap(),
1847 field.name.unwrap()
1848 ));
1849 write!(
1850 w,
1851 "<div class=\"sub-variant-field\">\
1852 <span id=\"{id}\" class=\"section-header\">\
1853 <a href=\"#{id}\" class=\"anchor field\">§</a>\
1854 <code>"
1855 )?;
1856 render_attributes_in_code(w, field, "", cx);
1857 write!(
1858 w,
1859 "{f}: {t}</code>\
1860 </span>\
1861 {doc}\
1862 </div>",
1863 f = field.name.unwrap(),
1864 t = ty.print(cx),
1865 doc = document(cx, field, Some(variant), HeadingOffset::H5),
1866 )?;
1867 }
1868 _ => unreachable!(),
1869 }
1870 }
1871 w.write_str("</div>")?;
1872 }
1873 }
1874 w.write_str("</div>")
1875 })
1876}
1877
1878fn item_macro(cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) -> impl fmt::Display {
1879 fmt::from_fn(|w| {
1880 wrap_item(w, |w| {
1881 render_attributes_in_code(w, it, "", cx);
1883 if !t.macro_rules {
1884 write!(w, "{}", visibility_print_with_space(it, cx))?;
1885 }
1886 write!(w, "{}", Escape(&t.source))
1887 })?;
1888 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
1889 })
1890}
1891
1892fn item_proc_macro(cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) -> impl fmt::Display {
1893 fmt::from_fn(|w| {
1894 wrap_item(w, |w| {
1895 let name = it.name.expect("proc-macros always have names");
1896 match m.kind {
1897 MacroKind::Bang => {
1898 write!(w, "{name}!() {{ <span class=\"comment\">/* proc-macro */</span> }}")?;
1899 }
1900 MacroKind::Attr => {
1901 write!(w, "#[{name}]")?;
1902 }
1903 MacroKind::Derive => {
1904 write!(w, "#[derive({name})]")?;
1905 if !m.helpers.is_empty() {
1906 w.write_str(
1907 "\n{\n \
1908 <span class=\"comment\">// Attributes available to this derive:</span>\n",
1909 )?;
1910 for attr in &m.helpers {
1911 writeln!(w, " #[{attr}]")?;
1912 }
1913 w.write_str("}\n")?;
1914 }
1915 }
1916 }
1917 fmt::Result::Ok(())
1918 })?;
1919 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
1920 })
1921}
1922
1923fn item_primitive(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
1924 fmt::from_fn(|w| {
1925 let def_id = it.item_id.expect_def_id();
1926 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1927 if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
1928 write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All))?;
1929 } else {
1930 let (concrete, synthetic, blanket_impl) =
1933 get_filtered_impls_for_reference(&cx.shared, it);
1934
1935 render_all_impls(w, cx, it, &concrete, &synthetic, &blanket_impl);
1936 }
1937 Ok(())
1938 })
1939}
1940
1941fn item_constant(
1942 cx: &Context<'_>,
1943 it: &clean::Item,
1944 generics: &clean::Generics,
1945 ty: &clean::Type,
1946 c: &clean::ConstantKind,
1947) -> impl fmt::Display {
1948 fmt::from_fn(|w| {
1949 wrap_item(w, |w| {
1950 let tcx = cx.tcx();
1951 render_attributes_in_code(w, it, "", cx);
1952
1953 write!(
1954 w,
1955 "{vis}const {name}{generics}: {typ}{where_clause}",
1956 vis = visibility_print_with_space(it, cx),
1957 name = it.name.unwrap(),
1958 generics = generics.print(cx),
1959 typ = ty.print(cx),
1960 where_clause =
1961 print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(),
1962 )?;
1963
1964 let value = c.value(tcx);
1974 let is_literal = c.is_literal(tcx);
1975 let expr = c.expr(tcx);
1976 if value.is_some() || is_literal {
1977 write!(w, " = {expr};", expr = Escape(&expr))?;
1978 } else {
1979 w.write_str(";")?;
1980 }
1981
1982 if !is_literal && let Some(value) = &value {
1983 let value_lowercase = value.to_lowercase();
1984 let expr_lowercase = expr.to_lowercase();
1985
1986 if value_lowercase != expr_lowercase
1987 && value_lowercase.trim_end_matches("i32") != expr_lowercase
1988 {
1989 write!(w, " // {value}", value = Escape(value))?;
1990 }
1991 }
1992 Ok::<(), fmt::Error>(())
1993 })?;
1994
1995 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
1996 })
1997}
1998
1999struct DisplayStruct<'a> {
2000 ctor_kind: Option<CtorKind>,
2001 generics: &'a clean::Generics,
2002 fields: &'a [clean::Item],
2003 def_id: DefId,
2004}
2005
2006impl<'a> DisplayStruct<'a> {
2007 fn render_into<W: fmt::Write>(
2008 self,
2009 cx: &Context<'_>,
2010 it: &clean::Item,
2011 is_type_alias: bool,
2012 w: &mut W,
2013 ) -> fmt::Result {
2014 wrap_item(w, |w| {
2015 if is_type_alias {
2016 render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Struct);
2018 } else {
2019 render_attributes_in_code(w, it, "", cx);
2020 }
2021 write!(
2022 w,
2023 "{}",
2024 render_struct(it, Some(self.generics), self.ctor_kind, self.fields, "", true, cx)
2025 )
2026 })?;
2027
2028 if !is_type_alias {
2029 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
2030 }
2031
2032 let def_id = it.item_id.expect_def_id();
2033 write!(
2034 w,
2035 "{}{}{}",
2036 item_fields(cx, it, self.fields, self.ctor_kind),
2037 render_assoc_items(cx, it, def_id, AssocItemRender::All),
2038 document_type_layout(cx, def_id),
2039 )
2040 }
2041}
2042
2043fn item_struct(cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) -> impl fmt::Display {
2044 fmt::from_fn(|w| {
2045 DisplayStruct {
2046 ctor_kind: s.ctor_kind,
2047 generics: &s.generics,
2048 fields: s.fields.as_slice(),
2049 def_id: it.def_id().unwrap(),
2050 }
2051 .render_into(cx, it, false, w)
2052 })
2053}
2054
2055fn item_fields(
2056 cx: &Context<'_>,
2057 it: &clean::Item,
2058 fields: &[clean::Item],
2059 ctor_kind: Option<CtorKind>,
2060) -> impl fmt::Display {
2061 fmt::from_fn(move |w| {
2062 let mut fields = fields
2063 .iter()
2064 .filter_map(|f| match f.kind {
2065 clean::StructFieldItem(ref ty) => Some((f, ty)),
2066 _ => None,
2067 })
2068 .peekable();
2069 if let None | Some(CtorKind::Fn) = ctor_kind
2070 && fields.peek().is_some()
2071 {
2072 let title = format!(
2073 "{}{}",
2074 if ctor_kind.is_none() { "Fields" } else { "Tuple Fields" },
2075 document_non_exhaustive_header(it),
2076 );
2077 write!(
2078 w,
2079 "{}",
2080 write_section_heading(
2081 &title,
2082 "fields",
2083 Some("fields"),
2084 document_non_exhaustive(it)
2085 )
2086 )?;
2087 for (index, (field, ty)) in fields.enumerate() {
2088 let field_name =
2089 field.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
2090 let id = cx.derive_id(format!("{typ}.{field_name}", typ = ItemType::StructField));
2091 write!(
2092 w,
2093 "<span id=\"{id}\" class=\"{item_type} section-header\">\
2094 <a href=\"#{id}\" class=\"anchor field\">§</a>\
2095 <code>",
2096 item_type = ItemType::StructField,
2097 )?;
2098 render_attributes_in_code(w, field, "", cx);
2099 write!(
2100 w,
2101 "{field_name}: {ty}</code>\
2102 </span>\
2103 {doc}",
2104 ty = ty.print(cx),
2105 doc = document(cx, field, Some(it), HeadingOffset::H3),
2106 )?;
2107 }
2108 }
2109 Ok(())
2110 })
2111}
2112
2113fn item_static(
2114 cx: &Context<'_>,
2115 it: &clean::Item,
2116 s: &clean::Static,
2117 safety: Option<hir::Safety>,
2118) -> impl fmt::Display {
2119 fmt::from_fn(move |w| {
2120 wrap_item(w, |w| {
2121 render_attributes_in_code(w, it, "", cx);
2122 write!(
2123 w,
2124 "{vis}{safe}static {mutability}{name}: {typ}",
2125 vis = visibility_print_with_space(it, cx),
2126 safe = safety.map(|safe| safe.prefix_str()).unwrap_or(""),
2127 mutability = s.mutability.print_with_space(),
2128 name = it.name.unwrap(),
2129 typ = s.type_.print(cx)
2130 )
2131 })?;
2132
2133 write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
2134 })
2135}
2136
2137fn item_foreign_type(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
2138 fmt::from_fn(|w| {
2139 wrap_item(w, |w| {
2140 w.write_str("extern {\n")?;
2141 render_attributes_in_code(w, it, "", cx);
2142 write!(w, " {}type {};\n}}", visibility_print_with_space(it, cx), it.name.unwrap(),)
2143 })?;
2144
2145 write!(
2146 w,
2147 "{}{}",
2148 document(cx, it, None, HeadingOffset::H2),
2149 render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
2150 )
2151 })
2152}
2153
2154fn item_keyword(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
2155 document(cx, it, None, HeadingOffset::H2)
2156}
2157
2158pub(crate) fn compare_names(left: &str, right: &str) -> Ordering {
2164 let mut left = left.chars().peekable();
2165 let mut right = right.chars().peekable();
2166
2167 loop {
2168 let (l, r) = match (left.next(), right.next()) {
2170 (None, None) => return Ordering::Equal,
2172 (None, Some(_)) => return Ordering::Less,
2174 (Some(_), None) => return Ordering::Greater,
2175 (Some(l), Some(r)) => (l, r),
2176 };
2177 let next_ordering = match (l.to_digit(10), r.to_digit(10)) {
2178 (None, None) => Ord::cmp(&l, &r),
2180 (None, Some(_)) => Ordering::Greater,
2183 (Some(_), None) => Ordering::Less,
2184 (Some(l), Some(r)) => {
2186 if l == 0 || r == 0 {
2187 let ordering = Ord::cmp(&l, &r);
2189 if ordering != Ordering::Equal {
2190 return ordering;
2191 }
2192 loop {
2193 let (l, r) = match (left.peek(), right.peek()) {
2195 (None, None) => return Ordering::Equal,
2197 (None, Some(_)) => return Ordering::Less,
2199 (Some(_), None) => return Ordering::Greater,
2200 (Some(l), Some(r)) => (l, r),
2201 };
2202 match (l.to_digit(10), r.to_digit(10)) {
2204 (None, None) => break Ordering::Equal,
2206 (None, Some(_)) => return Ordering::Less,
2208 (Some(_), None) => return Ordering::Greater,
2209 (Some(l), Some(r)) => {
2211 left.next();
2212 right.next();
2213 let ordering = Ord::cmp(&l, &r);
2214 if ordering != Ordering::Equal {
2215 return ordering;
2216 }
2217 }
2218 }
2219 }
2220 } else {
2221 let mut same_length_ordering = Ord::cmp(&l, &r);
2223 loop {
2224 let (l, r) = match (left.peek(), right.peek()) {
2226 (None, None) => return same_length_ordering,
2228 (None, Some(_)) => return Ordering::Less,
2230 (Some(_), None) => return Ordering::Greater,
2231 (Some(l), Some(r)) => (l, r),
2232 };
2233 match (l.to_digit(10), r.to_digit(10)) {
2235 (None, None) => break same_length_ordering,
2237 (None, Some(_)) => return Ordering::Less,
2239 (Some(_), None) => return Ordering::Greater,
2240 (Some(l), Some(r)) => {
2242 left.next();
2243 right.next();
2244 same_length_ordering = same_length_ordering.then(Ord::cmp(&l, &r));
2245 }
2246 }
2247 }
2248 }
2249 }
2250 };
2251 if next_ordering != Ordering::Equal {
2252 return next_ordering;
2253 }
2254 }
2255}
2256
2257pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
2258 let mut s = join_path_syms(&cx.current);
2259 s.push_str("::");
2260 s.push_str(item.name.unwrap().as_str());
2261 s
2262}
2263
2264pub(super) fn print_item_path(ty: ItemType, name: &str) -> impl Display {
2265 fmt::from_fn(move |f| match ty {
2266 ItemType::Module => write!(f, "{}index.html", ensure_trailing_slash(name)),
2267 _ => write!(f, "{ty}.{name}.html"),
2268 })
2269}
2270
2271fn print_bounds(
2272 bounds: &[clean::GenericBound],
2273 trait_alias: bool,
2274 cx: &Context<'_>,
2275) -> impl Display {
2276 (!bounds.is_empty())
2277 .then_some(fmt::from_fn(move |f| {
2278 let has_lots_of_bounds = bounds.len() > 2;
2279 let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2280 if !trait_alias {
2281 if has_lots_of_bounds {
2282 f.write_str(":\n ")?;
2283 } else {
2284 f.write_str(": ")?;
2285 }
2286 }
2287
2288 bounds.iter().map(|p| p.print(cx)).joined(inter_str, f)
2289 }))
2290 .maybe_display()
2291}
2292
2293fn wrap_item<W, F, T>(w: &mut W, f: F) -> T
2294where
2295 W: fmt::Write,
2296 F: FnOnce(&mut W) -> T,
2297{
2298 write!(w, r#"<pre class="rust item-decl"><code>"#).unwrap();
2299 let res = f(w);
2300 write!(w, "</code></pre>").unwrap();
2301 res
2302}
2303
2304#[derive(PartialEq, Eq)]
2305struct ImplString(String);
2306
2307impl ImplString {
2308 fn new(i: &Impl, cx: &Context<'_>) -> ImplString {
2309 ImplString(format!("{}", i.inner_impl().print(false, cx)))
2310 }
2311}
2312
2313impl PartialOrd for ImplString {
2314 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2315 Some(Ord::cmp(self, other))
2316 }
2317}
2318
2319impl Ord for ImplString {
2320 fn cmp(&self, other: &Self) -> Ordering {
2321 compare_names(&self.0, &other.0)
2322 }
2323}
2324
2325fn render_implementor(
2326 cx: &Context<'_>,
2327 implementor: &Impl,
2328 trait_: &clean::Item,
2329 implementor_dups: &FxHashMap<Symbol, (DefId, bool)>,
2330 aliases: &[String],
2331) -> impl fmt::Display {
2332 let use_absolute = match implementor.inner_impl().for_ {
2335 clean::Type::Path { ref path, .. }
2336 | clean::BorrowedRef { type_: box clean::Type::Path { ref path, .. }, .. }
2337 if !path.is_assoc_ty() =>
2338 {
2339 implementor_dups[&path.last()].1
2340 }
2341 _ => false,
2342 };
2343 render_impl(
2344 cx,
2345 implementor,
2346 trait_,
2347 AssocItemLink::Anchor(None),
2348 RenderMode::Normal,
2349 Some(use_absolute),
2350 aliases,
2351 ImplRenderingParameters {
2352 show_def_docs: false,
2353 show_default_items: false,
2354 show_non_assoc_items: false,
2355 toggle_open_by_default: false,
2356 },
2357 )
2358}
2359
2360fn render_union(
2361 it: &clean::Item,
2362 g: Option<&clean::Generics>,
2363 fields: &[clean::Item],
2364 def_id: DefId,
2365 is_type_alias: bool,
2366 cx: &Context<'_>,
2367) -> impl Display {
2368 fmt::from_fn(move |mut f| {
2369 if is_type_alias {
2370 render_repr_attributes_in_code(f, cx, def_id, ItemType::Union);
2372 } else {
2373 render_attributes_in_code(f, it, "", cx);
2374 }
2375 write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
2376
2377 let where_displayed = if let Some(generics) = g {
2378 write!(f, "{}", generics.print(cx))?;
2379 if let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline) {
2380 write!(f, "{where_clause}")?;
2381 true
2382 } else {
2383 false
2384 }
2385 } else {
2386 false
2387 };
2388
2389 if !where_displayed {
2391 f.write_str(" ")?;
2392 }
2393
2394 writeln!(f, "{{")?;
2395 let count_fields =
2396 fields.iter().filter(|field| matches!(field.kind, clean::StructFieldItem(..))).count();
2397 let toggle = should_hide_fields(count_fields);
2398 if toggle {
2399 toggle_open(&mut f, format_args!("{count_fields} fields"));
2400 }
2401
2402 for field in fields {
2403 if let clean::StructFieldItem(ref ty) = field.kind {
2404 render_attributes_in_code(&mut f, field, " ", cx);
2405 writeln!(
2406 f,
2407 " {}{}: {},",
2408 visibility_print_with_space(field, cx),
2409 field.name.unwrap(),
2410 ty.print(cx)
2411 )?;
2412 }
2413 }
2414
2415 if it.has_stripped_entries().unwrap() {
2416 writeln!(f, " <span class=\"comment\">/* private fields */</span>")?;
2417 }
2418 if toggle {
2419 toggle_close(&mut f);
2420 }
2421 f.write_str("}").unwrap();
2422 Ok(())
2423 })
2424}
2425
2426fn render_struct(
2427 it: &clean::Item,
2428 g: Option<&clean::Generics>,
2429 ty: Option<CtorKind>,
2430 fields: &[clean::Item],
2431 tab: &str,
2432 structhead: bool,
2433 cx: &Context<'_>,
2434) -> impl fmt::Display {
2435 fmt::from_fn(move |w| {
2436 write!(
2437 w,
2438 "{}{}{}",
2439 visibility_print_with_space(it, cx),
2440 if structhead { "struct " } else { "" },
2441 it.name.unwrap()
2442 )?;
2443 if let Some(g) = g {
2444 write!(w, "{}", g.print(cx))?;
2445 }
2446 write!(
2447 w,
2448 "{}",
2449 render_struct_fields(
2450 g,
2451 ty,
2452 fields,
2453 tab,
2454 structhead,
2455 it.has_stripped_entries().unwrap_or(false),
2456 cx,
2457 )
2458 )
2459 })
2460}
2461
2462fn render_struct_fields(
2463 g: Option<&clean::Generics>,
2464 ty: Option<CtorKind>,
2465 fields: &[clean::Item],
2466 tab: &str,
2467 structhead: bool,
2468 has_stripped_entries: bool,
2469 cx: &Context<'_>,
2470) -> impl fmt::Display {
2471 fmt::from_fn(move |w| {
2472 match ty {
2473 None => {
2474 let where_displayed = if let Some(generics) = g
2475 && let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
2476 {
2477 write!(w, "{where_clause}")?;
2478 true
2479 } else {
2480 false
2481 };
2482
2483 if !where_displayed {
2485 w.write_str(" {")?;
2486 } else {
2487 w.write_str("{")?;
2488 }
2489 let count_fields =
2490 fields.iter().filter(|f| matches!(f.kind, clean::StructFieldItem(..))).count();
2491 let has_visible_fields = count_fields > 0;
2492 let toggle = should_hide_fields(count_fields);
2493 if toggle {
2494 toggle_open(&mut *w, format_args!("{count_fields} fields"));
2495 }
2496 if has_visible_fields {
2497 writeln!(w)?;
2498 }
2499 for field in fields {
2500 if let clean::StructFieldItem(ref ty) = field.kind {
2501 render_attributes_in_code(w, field, &format!("{tab} "), cx);
2502 writeln!(
2503 w,
2504 "{tab} {vis}{name}: {ty},",
2505 vis = visibility_print_with_space(field, cx),
2506 name = field.name.unwrap(),
2507 ty = ty.print(cx)
2508 )?;
2509 }
2510 }
2511
2512 if has_visible_fields {
2513 if has_stripped_entries {
2514 writeln!(
2515 w,
2516 "{tab} <span class=\"comment\">/* private fields */</span>"
2517 )?;
2518 }
2519 write!(w, "{tab}")?;
2520 } else if has_stripped_entries {
2521 write!(w, " <span class=\"comment\">/* private fields */</span> ")?;
2522 }
2523 if toggle {
2524 toggle_close(&mut *w);
2525 }
2526 w.write_str("}")?;
2527 }
2528 Some(CtorKind::Fn) => {
2529 w.write_str("(")?;
2530 if !fields.is_empty()
2531 && fields.iter().all(|field| {
2532 matches!(field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
2533 })
2534 {
2535 write!(w, "<span class=\"comment\">/* private fields */</span>")?;
2536 } else {
2537 for (i, field) in fields.iter().enumerate() {
2538 if i > 0 {
2539 w.write_str(", ")?;
2540 }
2541 match field.kind {
2542 clean::StrippedItem(box clean::StructFieldItem(..)) => {
2543 write!(w, "_")?;
2544 }
2545 clean::StructFieldItem(ref ty) => {
2546 write!(
2547 w,
2548 "{}{}",
2549 visibility_print_with_space(field, cx),
2550 ty.print(cx)
2551 )?;
2552 }
2553 _ => unreachable!(),
2554 }
2555 }
2556 }
2557 w.write_str(")")?;
2558 if let Some(g) = g {
2559 write!(
2560 w,
2561 "{}",
2562 print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2563 )?;
2564 }
2565 if structhead {
2567 w.write_str(";")?;
2568 }
2569 }
2570 Some(CtorKind::Const) => {
2571 if let Some(g) = g {
2573 write!(
2574 w,
2575 "{}",
2576 print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2577 )?;
2578 }
2579 w.write_str(";")?;
2580 }
2581 }
2582 Ok(())
2583 })
2584}
2585
2586fn document_non_exhaustive_header(item: &clean::Item) -> &str {
2587 if item.is_non_exhaustive() { " (Non-exhaustive)" } else { "" }
2588}
2589
2590fn document_non_exhaustive(item: &clean::Item) -> impl Display {
2591 fmt::from_fn(|f| {
2592 if item.is_non_exhaustive() {
2593 write!(
2594 f,
2595 "<details class=\"toggle non-exhaustive\">\
2596 <summary class=\"hideme\"><span>{}</span></summary>\
2597 <div class=\"docblock\">",
2598 {
2599 if item.is_struct() {
2600 "This struct is marked as non-exhaustive"
2601 } else if item.is_enum() {
2602 "This enum is marked as non-exhaustive"
2603 } else if item.is_variant() {
2604 "This variant is marked as non-exhaustive"
2605 } else {
2606 "This type is marked as non-exhaustive"
2607 }
2608 }
2609 )?;
2610
2611 if item.is_struct() {
2612 f.write_str(
2613 "Non-exhaustive structs could have additional fields added in future. \
2614 Therefore, non-exhaustive structs cannot be constructed in external crates \
2615 using the traditional <code>Struct { .. }</code> syntax; cannot be \
2616 matched against without a wildcard <code>..</code>; and \
2617 struct update syntax will not work.",
2618 )?;
2619 } else if item.is_enum() {
2620 f.write_str(
2621 "Non-exhaustive enums could have additional variants added in future. \
2622 Therefore, when matching against variants of non-exhaustive enums, an \
2623 extra wildcard arm must be added to account for any future variants.",
2624 )?;
2625 } else if item.is_variant() {
2626 f.write_str(
2627 "Non-exhaustive enum variants could have additional fields added in future. \
2628 Therefore, non-exhaustive enum variants cannot be constructed in external \
2629 crates and cannot be matched against.",
2630 )?;
2631 } else {
2632 f.write_str(
2633 "This type will require a wildcard arm in any match statements or constructors.",
2634 )?;
2635 }
2636
2637 f.write_str("</div></details>")?;
2638 }
2639 Ok(())
2640 })
2641}
2642
2643fn pluralize(count: usize) -> &'static str {
2644 if count > 1 { "s" } else { "" }
2645}