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