]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/render/print_item.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / src / librustdoc / html / render / print_item.rs
1 use clean::AttributesExt;
2
3 use std::cmp::Ordering;
4 use std::fmt;
5
6 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7 use rustc_hir as hir;
8 use rustc_hir::def::CtorKind;
9 use rustc_hir::def_id::DefId;
10 use rustc_middle::middle::stability;
11 use rustc_middle::span_bug;
12 use rustc_middle::ty::layout::LayoutError;
13 use rustc_middle::ty::{Adt, TyCtxt};
14 use rustc_span::hygiene::MacroKind;
15 use rustc_span::symbol::{kw, sym, Symbol};
16 use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
17
18 use super::{
19 collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
20 notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
21 render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
22 AssocItemLink, Context, ImplRenderingParameters,
23 };
24 use crate::clean;
25 use crate::formats::item_type::ItemType;
26 use crate::formats::{AssocItemRender, Impl, RenderMode};
27 use crate::html::escape::Escape;
28 use crate::html::format::{
29 join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
30 Buffer, PrintWithSpace,
31 };
32 use crate::html::highlight;
33 use crate::html::layout::Page;
34 use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
35 use crate::html::url_parts_builder::UrlPartsBuilder;
36
37 use askama::Template;
38
39 const ITEM_TABLE_OPEN: &str = "<div class=\"item-table\">";
40 const ITEM_TABLE_CLOSE: &str = "</div>";
41 const ITEM_TABLE_ROW_OPEN: &str = "<div class=\"item-row\">";
42 const ITEM_TABLE_ROW_CLOSE: &str = "</div>";
43
44 // A component in a `use` path, like `string` in std::string::ToString
45 struct PathComponent {
46 path: String,
47 name: Symbol,
48 }
49
50 #[derive(Template)]
51 #[template(path = "print_item.html")]
52 struct ItemVars<'a> {
53 page: &'a Page<'a>,
54 static_root_path: &'a str,
55 typ: &'a str,
56 name: &'a str,
57 item_type: &'a str,
58 path_components: Vec<PathComponent>,
59 stability_since_raw: &'a str,
60 src_href: Option<&'a str>,
61 }
62
63 pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
64 debug_assert!(!item.is_stripped());
65 let typ = match *item.kind {
66 clean::ModuleItem(_) => {
67 if item.is_crate() {
68 "Crate "
69 } else {
70 "Module "
71 }
72 }
73 clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
74 clean::TraitItem(..) => "Trait ",
75 clean::StructItem(..) => "Struct ",
76 clean::UnionItem(..) => "Union ",
77 clean::EnumItem(..) => "Enum ",
78 clean::TypedefItem(..) => "Type Definition ",
79 clean::MacroItem(..) => "Macro ",
80 clean::ProcMacroItem(ref mac) => match mac.kind {
81 MacroKind::Bang => "Macro ",
82 MacroKind::Attr => "Attribute Macro ",
83 MacroKind::Derive => "Derive Macro ",
84 },
85 clean::PrimitiveItem(..) => "Primitive Type ",
86 clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
87 clean::ConstantItem(..) => "Constant ",
88 clean::ForeignTypeItem => "Foreign Type ",
89 clean::KeywordItem(..) => "Keyword ",
90 clean::OpaqueTyItem(..) => "Opaque Type ",
91 clean::TraitAliasItem(..) => "Trait Alias ",
92 _ => {
93 // We don't generate pages for any other type.
94 unreachable!();
95 }
96 };
97 let mut stability_since_raw = Buffer::new();
98 render_stability_since_raw(
99 &mut stability_since_raw,
100 item.stable_since(cx.tcx()),
101 item.const_stability(cx.tcx()),
102 None,
103 None,
104 );
105 let stability_since_raw: String = stability_since_raw.into_inner();
106
107 // Write source tag
108 //
109 // When this item is part of a `crate use` in a downstream crate, the
110 // source link in the downstream documentation will actually come back to
111 // this page, and this link will be auto-clicked. The `id` attribute is
112 // used to find the link to auto-click.
113 let src_href =
114 if cx.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
115
116 let path_components = if item.is_primitive() || item.is_keyword() {
117 vec![]
118 } else {
119 let cur = &cx.current;
120 let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
121 cur.iter()
122 .enumerate()
123 .take(amt)
124 .map(|(i, component)| PathComponent {
125 path: "../".repeat(cur.len() - i - 1),
126 name: *component,
127 })
128 .collect()
129 };
130
131 let item_vars = ItemVars {
132 page,
133 static_root_path: page.get_static_root_path(),
134 typ,
135 name: item.name.as_ref().unwrap().as_str(),
136 item_type: &item.type_().to_string(),
137 path_components,
138 stability_since_raw: &stability_since_raw,
139 src_href: src_href.as_deref(),
140 };
141
142 item_vars.render_into(buf).unwrap();
143
144 match &*item.kind {
145 clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),
146 clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => {
147 item_function(buf, cx, item, f)
148 }
149 clean::TraitItem(ref t) => item_trait(buf, cx, item, t),
150 clean::StructItem(ref s) => item_struct(buf, cx, item, s),
151 clean::UnionItem(ref s) => item_union(buf, cx, item, s),
152 clean::EnumItem(ref e) => item_enum(buf, cx, item, e),
153 clean::TypedefItem(ref t) => item_typedef(buf, cx, item, t),
154 clean::MacroItem(ref m) => item_macro(buf, cx, item, m),
155 clean::ProcMacroItem(ref m) => item_proc_macro(buf, cx, item, m),
156 clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
157 clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) => item_static(buf, cx, item, i),
158 clean::ConstantItem(ref c) => item_constant(buf, cx, item, c),
159 clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
160 clean::KeywordItem(_) => item_keyword(buf, cx, item),
161 clean::OpaqueTyItem(ref e) => item_opaque_ty(buf, cx, item, e),
162 clean::TraitAliasItem(ref ta) => item_trait_alias(buf, cx, item, ta),
163 _ => {
164 // We don't generate pages for any other type.
165 unreachable!();
166 }
167 }
168 }
169
170 /// For large structs, enums, unions, etc, determine whether to hide their fields
171 fn should_hide_fields(n_fields: usize) -> bool {
172 n_fields > 12
173 }
174
175 fn toggle_open(w: &mut Buffer, text: impl fmt::Display) {
176 write!(
177 w,
178 "<details class=\"rustdoc-toggle type-contents-toggle\">\
179 <summary class=\"hideme\">\
180 <span>Show {}</span>\
181 </summary>",
182 text
183 );
184 }
185
186 fn toggle_close(w: &mut Buffer) {
187 w.write_str("</details>");
188 }
189
190 fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) {
191 document(w, cx, item, None, HeadingOffset::H2);
192
193 let mut indices = (0..items.len()).filter(|i| !items[*i].is_stripped()).collect::<Vec<usize>>();
194
195 // the order of item types in the listing
196 fn reorder(ty: ItemType) -> u8 {
197 match ty {
198 ItemType::ExternCrate => 0,
199 ItemType::Import => 1,
200 ItemType::Primitive => 2,
201 ItemType::Module => 3,
202 ItemType::Macro => 4,
203 ItemType::Struct => 5,
204 ItemType::Enum => 6,
205 ItemType::Constant => 7,
206 ItemType::Static => 8,
207 ItemType::Trait => 9,
208 ItemType::Function => 10,
209 ItemType::Typedef => 12,
210 ItemType::Union => 13,
211 _ => 14 + ty as u8,
212 }
213 }
214
215 fn cmp(
216 i1: &clean::Item,
217 i2: &clean::Item,
218 idx1: usize,
219 idx2: usize,
220 tcx: TyCtxt<'_>,
221 ) -> Ordering {
222 let ty1 = i1.type_();
223 let ty2 = i2.type_();
224 if item_ty_to_section(ty1) != item_ty_to_section(ty2)
225 || (ty1 != ty2 && (ty1 == ItemType::ExternCrate || ty2 == ItemType::ExternCrate))
226 {
227 return (reorder(ty1), idx1).cmp(&(reorder(ty2), idx2));
228 }
229 let s1 = i1.stability(tcx).as_ref().map(|s| s.level);
230 let s2 = i2.stability(tcx).as_ref().map(|s| s.level);
231 if let (Some(a), Some(b)) = (s1, s2) {
232 match (a.is_stable(), b.is_stable()) {
233 (true, true) | (false, false) => {}
234 (false, true) => return Ordering::Less,
235 (true, false) => return Ordering::Greater,
236 }
237 }
238 let lhs = i1.name.unwrap_or(kw::Empty);
239 let rhs = i2.name.unwrap_or(kw::Empty);
240 compare_names(lhs.as_str(), rhs.as_str())
241 }
242
243 if cx.shared.sort_modules_alphabetically {
244 indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2, cx.tcx()));
245 }
246 // This call is to remove re-export duplicates in cases such as:
247 //
248 // ```
249 // crate mod foo {
250 // crate mod bar {
251 // crate trait Double { fn foo(); }
252 // }
253 // }
254 //
255 // crate use foo::bar::*;
256 // crate use foo::*;
257 // ```
258 //
259 // `Double` will appear twice in the generated docs.
260 //
261 // FIXME: This code is quite ugly and could be improved. Small issue: DefId
262 // can be identical even if the elements are different (mostly in imports).
263 // So in case this is an import, we keep everything by adding a "unique id"
264 // (which is the position in the vector).
265 indices.dedup_by_key(|i| {
266 (
267 items[*i].item_id,
268 if items[*i].name.is_some() { Some(full_path(cx, &items[*i])) } else { None },
269 items[*i].type_(),
270 if items[*i].is_import() { *i } else { 0 },
271 )
272 });
273
274 debug!("{:?}", indices);
275 let mut last_section = None;
276
277 for &idx in &indices {
278 let myitem = &items[idx];
279 if myitem.is_stripped() {
280 continue;
281 }
282
283 let my_section = item_ty_to_section(myitem.type_());
284 if Some(my_section) != last_section {
285 if last_section.is_some() {
286 w.write_str(ITEM_TABLE_CLOSE);
287 }
288 last_section = Some(my_section);
289 write!(
290 w,
291 "<h2 id=\"{id}\" class=\"small-section-header\">\
292 <a href=\"#{id}\">{name}</a>\
293 </h2>\n{}",
294 ITEM_TABLE_OPEN,
295 id = cx.derive_id(my_section.id().to_owned()),
296 name = my_section.name(),
297 );
298 }
299
300 match *myitem.kind {
301 clean::ExternCrateItem { ref src } => {
302 use crate::html::format::anchor;
303
304 w.write_str(ITEM_TABLE_ROW_OPEN);
305 match *src {
306 Some(src) => write!(
307 w,
308 "<div class=\"item-left\"><code>{}extern crate {} as {};",
309 myitem.visibility.print_with_space(myitem.item_id, cx),
310 anchor(myitem.item_id.expect_def_id(), src, cx),
311 myitem.name.unwrap(),
312 ),
313 None => write!(
314 w,
315 "<div class=\"item-left\"><code>{}extern crate {};",
316 myitem.visibility.print_with_space(myitem.item_id, cx),
317 anchor(myitem.item_id.expect_def_id(), myitem.name.unwrap(), cx),
318 ),
319 }
320 w.write_str("</code></div>");
321 w.write_str(ITEM_TABLE_ROW_CLOSE);
322 }
323
324 clean::ImportItem(ref import) => {
325 let (stab, stab_tags) = if let Some(import_def_id) = import.source.did {
326 let ast_attrs = cx.tcx().get_attrs_unchecked(import_def_id);
327 let import_attrs = Box::new(clean::Attributes::from_ast(ast_attrs, None));
328
329 // Just need an item with the correct def_id and attrs
330 let import_item = clean::Item {
331 item_id: import_def_id.into(),
332 attrs: import_attrs,
333 cfg: ast_attrs.cfg(cx.tcx(), &cx.cache().hidden_cfg),
334 ..myitem.clone()
335 };
336
337 let stab = import_item.stability_class(cx.tcx());
338 let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx()));
339 (stab, stab_tags)
340 } else {
341 (None, None)
342 };
343
344 let add = if stab.is_some() { " " } else { "" };
345
346 w.write_str(ITEM_TABLE_ROW_OPEN);
347 write!(
348 w,
349 "<div class=\"item-left {stab}{add}import-item\"{id}>\
350 <code>{vis}{imp}</code>\
351 </div>\
352 <div class=\"item-right docblock-short\">{stab_tags}</div>",
353 stab = stab.unwrap_or_default(),
354 add = add,
355 vis = myitem.visibility.print_with_space(myitem.item_id, cx),
356 imp = import.print(cx),
357 stab_tags = stab_tags.unwrap_or_default(),
358 id = match import.kind {
359 clean::ImportKind::Simple(s) =>
360 format!(" id=\"{}\"", cx.derive_id(format!("reexport.{}", s))),
361 clean::ImportKind::Glob => String::new(),
362 },
363 );
364 w.write_str(ITEM_TABLE_ROW_CLOSE);
365 }
366
367 _ => {
368 if myitem.name.is_none() {
369 continue;
370 }
371
372 let unsafety_flag = match *myitem.kind {
373 clean::FunctionItem(_) | clean::ForeignFunctionItem(_)
374 if myitem.fn_header(cx.tcx()).unwrap().unsafety
375 == hir::Unsafety::Unsafe =>
376 {
377 "<a title=\"unsafe function\" href=\"#\"><sup>⚠</sup></a>"
378 }
379 _ => "",
380 };
381
382 let stab = myitem.stability_class(cx.tcx());
383 let add = if stab.is_some() { " " } else { "" };
384
385 let visibility_emoji = match myitem.visibility {
386 clean::Visibility::Restricted(_) => {
387 "<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
388 }
389 _ => "",
390 };
391
392 let doc_value = myitem.doc_value().unwrap_or_default();
393 w.write_str(ITEM_TABLE_ROW_OPEN);
394 write!(
395 w,
396 "<div class=\"item-left {stab}{add}module-item\">\
397 <a class=\"{class}\" href=\"{href}\" title=\"{title}\">{name}</a>\
398 {visibility_emoji}\
399 {unsafety_flag}\
400 {stab_tags}\
401 </div>\
402 <div class=\"item-right docblock-short\">{docs}</div>",
403 name = myitem.name.unwrap(),
404 visibility_emoji = visibility_emoji,
405 stab_tags = extra_info_tags(myitem, item, cx.tcx()),
406 docs = MarkdownSummaryLine(&doc_value, &myitem.links(cx)).into_string(),
407 class = myitem.type_(),
408 add = add,
409 stab = stab.unwrap_or_default(),
410 unsafety_flag = unsafety_flag,
411 href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
412 title = [full_path(cx, myitem), myitem.type_().to_string()]
413 .iter()
414 .filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
415 .collect::<Vec<_>>()
416 .join(" "),
417 );
418 w.write_str(ITEM_TABLE_ROW_CLOSE);
419 }
420 }
421 }
422
423 if last_section.is_some() {
424 w.write_str(ITEM_TABLE_CLOSE);
425 }
426 }
427
428 /// Render the stability, deprecation and portability tags that are displayed in the item's summary
429 /// at the module level.
430 fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> String {
431 let mut tags = String::new();
432
433 fn tag_html(class: &str, title: &str, contents: &str) -> String {
434 format!(r#"<span class="stab {}" title="{}">{}</span>"#, class, Escape(title), contents)
435 }
436
437 // The trailing space after each tag is to space it properly against the rest of the docs.
438 if let Some(depr) = &item.deprecation(tcx) {
439 let mut message = "Deprecated";
440 if !stability::deprecation_in_effect(depr) {
441 message = "Deprecation planned";
442 }
443 tags += &tag_html("deprecated", "", message);
444 }
445
446 // The "rustc_private" crates are permanently unstable so it makes no sense
447 // to render "unstable" everywhere.
448 if item
449 .stability(tcx)
450 .as_ref()
451 .map(|s| s.level.is_unstable() && s.feature != sym::rustc_private)
452 == Some(true)
453 {
454 tags += &tag_html("unstable", "", "Experimental");
455 }
456
457 let cfg = match (&item.cfg, parent.cfg.as_ref()) {
458 (Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
459 (cfg, _) => cfg.as_deref().cloned(),
460 };
461
462 debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.cfg, cfg);
463 if let Some(ref cfg) = cfg {
464 tags += &tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html());
465 }
466
467 tags
468 }
469
470 fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
471 let header = it.fn_header(cx.tcx()).expect("printing a function which isn't a function");
472 let constness = print_constness_with_space(&header.constness, it.const_stability(cx.tcx()));
473 let unsafety = header.unsafety.print_with_space();
474 let abi = print_abi_with_space(header.abi).to_string();
475 let asyncness = header.asyncness.print_with_space();
476 let visibility = it.visibility.print_with_space(it.item_id, cx).to_string();
477 let name = it.name.unwrap();
478
479 let generics_len = format!("{:#}", f.generics.print(cx)).len();
480 let header_len = "fn ".len()
481 + visibility.len()
482 + constness.len()
483 + asyncness.len()
484 + unsafety.len()
485 + abi.len()
486 + name.as_str().len()
487 + generics_len;
488
489 wrap_into_docblock(w, |w| {
490 wrap_item(w, "fn", |w| {
491 render_attributes_in_pre(w, it, "");
492 w.reserve(header_len);
493 write!(
494 w,
495 "{vis}{constness}{asyncness}{unsafety}{abi}fn \
496 {name}{generics}{decl}{notable_traits}{where_clause}",
497 vis = visibility,
498 constness = constness,
499 asyncness = asyncness,
500 unsafety = unsafety,
501 abi = abi,
502 name = name,
503 generics = f.generics.print(cx),
504 where_clause = print_where_clause(&f.generics, cx, 0, true),
505 decl = f.decl.full_print(header_len, 0, header.asyncness, cx),
506 notable_traits = notable_traits_decl(&f.decl, cx),
507 );
508 });
509 });
510 document(w, cx, it, None, HeadingOffset::H2)
511 }
512
513 fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) {
514 let bounds = bounds(&t.bounds, false, cx);
515 let required_types = t.items.iter().filter(|m| m.is_ty_associated_type()).collect::<Vec<_>>();
516 let provided_types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
517 let required_consts = t.items.iter().filter(|m| m.is_ty_associated_const()).collect::<Vec<_>>();
518 let provided_consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
519 let required_methods = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
520 let provided_methods = t.items.iter().filter(|m| m.is_method()).collect::<Vec<_>>();
521 let count_types = required_types.len() + provided_types.len();
522 let count_consts = required_consts.len() + provided_consts.len();
523 let count_methods = required_methods.len() + provided_methods.len();
524
525 // Output the trait definition
526 wrap_into_docblock(w, |w| {
527 wrap_item(w, "trait", |w| {
528 render_attributes_in_pre(w, it, "");
529 write!(
530 w,
531 "{}{}{}trait {}{}{}",
532 it.visibility.print_with_space(it.item_id, cx),
533 t.unsafety.print_with_space(),
534 if t.is_auto { "auto " } else { "" },
535 it.name.unwrap(),
536 t.generics.print(cx),
537 bounds
538 );
539
540 if !t.generics.where_predicates.is_empty() {
541 write!(w, "{}", print_where_clause(&t.generics, cx, 0, true));
542 } else {
543 w.write_str(" ");
544 }
545
546 if t.items.is_empty() {
547 w.write_str("{ }");
548 } else {
549 // FIXME: we should be using a derived_id for the Anchors here
550 w.write_str("{\n");
551 let mut toggle = false;
552
553 // If there are too many associated types, hide _everything_
554 if should_hide_fields(count_types) {
555 toggle = true;
556 toggle_open(
557 w,
558 format_args!(
559 "{} associated items",
560 count_types + count_consts + count_methods
561 ),
562 );
563 }
564 for types in [&required_types, &provided_types] {
565 for t in types {
566 render_assoc_item(
567 w,
568 t,
569 AssocItemLink::Anchor(None),
570 ItemType::Trait,
571 cx,
572 RenderMode::Normal,
573 );
574 w.write_str(";\n");
575 }
576 }
577 // If there are too many associated constants, hide everything after them
578 // We also do this if the types + consts is large because otherwise we could
579 // render a bunch of types and _then_ a bunch of consts just because both were
580 // _just_ under the limit
581 if !toggle && should_hide_fields(count_types + count_consts) {
582 toggle = true;
583 toggle_open(
584 w,
585 format_args!(
586 "{} associated constant{} and {} method{}",
587 count_consts,
588 pluralize(count_consts),
589 count_methods,
590 pluralize(count_methods),
591 ),
592 );
593 }
594 if count_types != 0 && (count_consts != 0 || count_methods != 0) {
595 w.write_str("\n");
596 }
597 for consts in [&required_consts, &provided_consts] {
598 for c in consts {
599 render_assoc_item(
600 w,
601 c,
602 AssocItemLink::Anchor(None),
603 ItemType::Trait,
604 cx,
605 RenderMode::Normal,
606 );
607 w.write_str(";\n");
608 }
609 }
610 if !toggle && should_hide_fields(count_methods) {
611 toggle = true;
612 toggle_open(w, format_args!("{} methods", count_methods));
613 }
614 if count_consts != 0 && count_methods != 0 {
615 w.write_str("\n");
616 }
617 for (pos, m) in required_methods.iter().enumerate() {
618 render_assoc_item(
619 w,
620 m,
621 AssocItemLink::Anchor(None),
622 ItemType::Trait,
623 cx,
624 RenderMode::Normal,
625 );
626 w.write_str(";\n");
627
628 if pos < required_methods.len() - 1 {
629 w.write_str("<span class=\"item-spacer\"></span>");
630 }
631 }
632 if !required_methods.is_empty() && !provided_methods.is_empty() {
633 w.write_str("\n");
634 }
635 for (pos, m) in provided_methods.iter().enumerate() {
636 render_assoc_item(
637 w,
638 m,
639 AssocItemLink::Anchor(None),
640 ItemType::Trait,
641 cx,
642 RenderMode::Normal,
643 );
644 match *m.kind {
645 clean::MethodItem(ref inner, _)
646 if !inner.generics.where_predicates.is_empty() =>
647 {
648 w.write_str(",\n { ... }\n");
649 }
650 _ => {
651 w.write_str(" { ... }\n");
652 }
653 }
654
655 if pos < provided_methods.len() - 1 {
656 w.write_str("<span class=\"item-spacer\"></span>");
657 }
658 }
659 if toggle {
660 toggle_close(w);
661 }
662 w.write_str("}");
663 }
664 });
665 });
666
667 // Trait documentation
668 document(w, cx, it, None, HeadingOffset::H2);
669
670 fn write_small_section_header(w: &mut Buffer, id: &str, title: &str, extra_content: &str) {
671 write!(
672 w,
673 "<h2 id=\"{0}\" class=\"small-section-header\">\
674 {1}<a href=\"#{0}\" class=\"anchor\"></a>\
675 </h2>{2}",
676 id, title, extra_content
677 )
678 }
679
680 fn trait_item(w: &mut Buffer, cx: &Context<'_>, m: &clean::Item, t: &clean::Item) {
681 let name = m.name.unwrap();
682 info!("Documenting {} on {:?}", name, t.name);
683 let item_type = m.type_();
684 let id = cx.derive_id(format!("{}.{}", item_type, name));
685 let mut content = Buffer::empty_from(w);
686 document(&mut content, cx, m, Some(t), HeadingOffset::H5);
687 let toggled = !content.is_empty();
688 if toggled {
689 write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
690 }
691 write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
692 write!(w, "<div class=\"rightside\">");
693
694 let has_stability = render_stability_since(w, m, t, cx.tcx());
695 if has_stability {
696 w.write_str(" · ");
697 }
698 write_srclink(cx, m, w);
699 write!(w, "</div>");
700 write!(w, "<h4 class=\"code-header\">");
701 render_assoc_item(
702 w,
703 m,
704 AssocItemLink::Anchor(Some(&id)),
705 ItemType::Impl,
706 cx,
707 RenderMode::Normal,
708 );
709 w.write_str("</h4>");
710 w.write_str("</div>");
711 if toggled {
712 write!(w, "</summary>");
713 w.push_buffer(content);
714 write!(w, "</details>");
715 }
716 }
717
718 if !required_types.is_empty() {
719 write_small_section_header(
720 w,
721 "required-associated-types",
722 "Required Associated Types",
723 "<div class=\"methods\">",
724 );
725 for t in required_types {
726 trait_item(w, cx, t, it);
727 }
728 w.write_str("</div>");
729 }
730 if !provided_types.is_empty() {
731 write_small_section_header(
732 w,
733 "provided-associated-types",
734 "Provided Associated Types",
735 "<div class=\"methods\">",
736 );
737 for t in provided_types {
738 trait_item(w, cx, t, it);
739 }
740 w.write_str("</div>");
741 }
742
743 if !required_consts.is_empty() {
744 write_small_section_header(
745 w,
746 "required-associated-consts",
747 "Required Associated Constants",
748 "<div class=\"methods\">",
749 );
750 for t in required_consts {
751 trait_item(w, cx, t, it);
752 }
753 w.write_str("</div>");
754 }
755 if !provided_consts.is_empty() {
756 write_small_section_header(
757 w,
758 "provided-associated-consts",
759 "Provided Associated Constants",
760 "<div class=\"methods\">",
761 );
762 for t in provided_consts {
763 trait_item(w, cx, t, it);
764 }
765 w.write_str("</div>");
766 }
767
768 // Output the documentation for each function individually
769 if !required_methods.is_empty() {
770 write_small_section_header(
771 w,
772 "required-methods",
773 "Required Methods",
774 "<div class=\"methods\">",
775 );
776 for m in required_methods {
777 trait_item(w, cx, m, it);
778 }
779 w.write_str("</div>");
780 }
781 if !provided_methods.is_empty() {
782 write_small_section_header(
783 w,
784 "provided-methods",
785 "Provided Methods",
786 "<div class=\"methods\">",
787 );
788 for m in provided_methods {
789 trait_item(w, cx, m, it);
790 }
791 w.write_str("</div>");
792 }
793
794 // If there are methods directly on this trait object, render them here.
795 render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All);
796
797 let cache = cx.cache();
798 let mut extern_crates = FxHashSet::default();
799 if let Some(implementors) = cache.implementors.get(&it.item_id.expect_def_id()) {
800 // The DefId is for the first Type found with that name. The bool is
801 // if any Types with the same name but different DefId have been found.
802 let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
803 for implementor in implementors {
804 if let Some(did) = implementor.inner_impl().for_.without_borrowed_ref().def_id(cx.cache()) &&
805 !did.is_local() {
806 extern_crates.insert(did.krate);
807 }
808 match implementor.inner_impl().for_.without_borrowed_ref() {
809 clean::Type::Path { ref path } if !path.is_assoc_ty() => {
810 let did = path.def_id();
811 let &mut (prev_did, ref mut has_duplicates) =
812 implementor_dups.entry(path.last()).or_insert((did, false));
813 if prev_did != did {
814 *has_duplicates = true;
815 }
816 }
817 _ => {}
818 }
819 }
820
821 let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
822 i.inner_impl().for_.def_id(cache).map_or(true, |d| cache.paths.contains_key(&d))
823 });
824
825 let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
826 local.iter().partition(|i| i.inner_impl().kind.is_auto());
827
828 synthetic.sort_by(|a, b| compare_impl(a, b, cx));
829 concrete.sort_by(|a, b| compare_impl(a, b, cx));
830
831 if !foreign.is_empty() {
832 write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
833
834 for implementor in foreign {
835 let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx());
836 let assoc_link =
837 AssocItemLink::GotoSource(implementor.impl_item.item_id, &provided_methods);
838 render_impl(
839 w,
840 cx,
841 implementor,
842 it,
843 assoc_link,
844 RenderMode::Normal,
845 None,
846 &[],
847 ImplRenderingParameters {
848 show_def_docs: false,
849 is_on_foreign_type: true,
850 show_default_items: false,
851 show_non_assoc_items: true,
852 toggle_open_by_default: false,
853 },
854 );
855 }
856 }
857
858 write_small_section_header(
859 w,
860 "implementors",
861 "Implementors",
862 "<div class=\"item-list\" id=\"implementors-list\">",
863 );
864 for implementor in concrete {
865 render_implementor(cx, implementor, it, w, &implementor_dups, &[]);
866 }
867 w.write_str("</div>");
868
869 if t.is_auto {
870 write_small_section_header(
871 w,
872 "synthetic-implementors",
873 "Auto implementors",
874 "<div class=\"item-list\" id=\"synthetic-implementors-list\">",
875 );
876 for implementor in synthetic {
877 render_implementor(
878 cx,
879 implementor,
880 it,
881 w,
882 &implementor_dups,
883 &collect_paths_for_type(implementor.inner_impl().for_.clone(), cache),
884 );
885 }
886 w.write_str("</div>");
887 }
888 } else {
889 // even without any implementations to write in, we still want the heading and list, so the
890 // implementors javascript file pulled in below has somewhere to write the impls into
891 write_small_section_header(
892 w,
893 "implementors",
894 "Implementors",
895 "<div class=\"item-list\" id=\"implementors-list\"></div>",
896 );
897
898 if t.is_auto {
899 write_small_section_header(
900 w,
901 "synthetic-implementors",
902 "Auto implementors",
903 "<div class=\"item-list\" id=\"synthetic-implementors-list\"></div>",
904 );
905 }
906 }
907
908 // Include implementors in crates that depend on the current crate.
909 //
910 // This is complicated by the way rustdoc is invoked, which is basically
911 // the same way rustc is invoked: it gets called, one at a time, for each
912 // crate. When building the rustdocs for the current crate, rustdoc can
913 // see crate metadata for its dependencies, but cannot see metadata for its
914 // dependents.
915 //
916 // To make this work, we generate a "hook" at this stage, and our
917 // dependents can "plug in" to it when they build. For simplicity's sake,
918 // it's [JSONP]: a JavaScript file with the data we need (and can parse),
919 // surrounded by a tiny wrapper that the Rust side ignores, but allows the
920 // JavaScript side to include without having to worry about Same Origin
921 // Policy. The code for *that* is in `write_shared.rs`.
922 //
923 // This is further complicated by `#[doc(inline)]`. We want all copies
924 // of an inlined trait to reference the same JS file, to address complex
925 // dependency graphs like this one (lower crates depend on higher crates):
926 //
927 // ```text
928 // --------------------------------------------
929 // | crate A: trait Foo |
930 // --------------------------------------------
931 // | |
932 // -------------------------------- |
933 // | crate B: impl A::Foo for Bar | |
934 // -------------------------------- |
935 // | |
936 // ---------------------------------------------
937 // | crate C: #[doc(inline)] use A::Foo as Baz |
938 // | impl Baz for Quux |
939 // ---------------------------------------------
940 // ```
941 //
942 // Basically, we want `C::Baz` and `A::Foo` to show the same set of
943 // impls, which is easier if they both treat `/implementors/A/trait.Foo.js`
944 // as the Single Source of Truth.
945 //
946 // We also want the `impl Baz for Quux` to be written to
947 // `trait.Foo.js`. However, when we generate plain HTML for `C::Baz`,
948 // we're going to want to generate plain HTML for `impl Baz for Quux` too,
949 // because that'll load faster, and it's better for SEO. And we don't want
950 // the same impl to show up twice on the same page.
951 //
952 // To make this work, the implementors JS file has a structure kinda
953 // like this:
954 //
955 // ```js
956 // JSONP({
957 // "B": {"impl A::Foo for Bar"},
958 // "C": {"impl Baz for Quux"},
959 // });
960 // ```
961 //
962 // First of all, this means we can rebuild a crate, and it'll replace its own
963 // data if something changes. That is, `rustdoc` is idempotent. The other
964 // advantage is that we can list the crates that get included in the HTML,
965 // and ignore them when doing the JavaScript-based part of rendering.
966 // So C's HTML will have something like this:
967 //
968 // ```html
969 // <script type="text/javascript" src="/implementors/A/trait.Foo.js"
970 // data-ignore-extern-crates="A,B" async></script>
971 // ```
972 //
973 // And, when the JS runs, anything in data-ignore-extern-crates is known
974 // to already be in the HTML, and will be ignored.
975 //
976 // [JSONP]: https://en.wikipedia.org/wiki/JSONP
977 let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
978 .take(cx.current.len())
979 .chain(std::iter::once("implementors"))
980 .collect();
981 if let Some(did) = it.item_id.as_def_id() &&
982 let get_extern = { || cache.external_paths.get(&did).map(|s| s.0.clone()) } &&
983 let Some(fqp) = cache.exact_paths.get(&did).cloned().or_else(get_extern) {
984 js_src_path.extend(fqp[..fqp.len() - 1].iter().copied());
985 js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap()));
986 } else {
987 js_src_path.extend(cx.current.iter().copied());
988 js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
989 }
990 let extern_crates = extern_crates
991 .into_iter()
992 .map(|cnum| cx.shared.tcx.crate_name(cnum).to_string())
993 .collect::<Vec<_>>()
994 .join(",");
995 write!(
996 w,
997 "<script type=\"text/javascript\" src=\"{src}\" data-ignore-extern-crates=\"{extern_crates}\" async></script>",
998 src = js_src_path.finish(),
999 );
1000 }
1001
1002 fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
1003 wrap_into_docblock(w, |w| {
1004 wrap_item(w, "trait-alias", |w| {
1005 render_attributes_in_pre(w, it, "");
1006 write!(
1007 w,
1008 "trait {}{}{} = {};",
1009 it.name.unwrap(),
1010 t.generics.print(cx),
1011 print_where_clause(&t.generics, cx, 0, true),
1012 bounds(&t.bounds, true, cx)
1013 );
1014 });
1015 });
1016
1017 document(w, cx, it, None, HeadingOffset::H2);
1018
1019 // Render any items associated directly to this alias, as otherwise they
1020 // won't be visible anywhere in the docs. It would be nice to also show
1021 // associated items from the aliased type (see discussion in #32077), but
1022 // we need #14072 to make sense of the generics.
1023 render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1024 }
1025
1026 fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
1027 wrap_into_docblock(w, |w| {
1028 wrap_item(w, "opaque", |w| {
1029 render_attributes_in_pre(w, it, "");
1030 write!(
1031 w,
1032 "type {}{}{where_clause} = impl {bounds};",
1033 it.name.unwrap(),
1034 t.generics.print(cx),
1035 where_clause = print_where_clause(&t.generics, cx, 0, true),
1036 bounds = bounds(&t.bounds, false, cx),
1037 );
1038 });
1039 });
1040
1041 document(w, cx, it, None, HeadingOffset::H2);
1042
1043 // Render any items associated directly to this alias, as otherwise they
1044 // won't be visible anywhere in the docs. It would be nice to also show
1045 // associated items from the aliased type (see discussion in #32077), but
1046 // we need #14072 to make sense of the generics.
1047 render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1048 }
1049
1050 fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
1051 fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
1052 wrap_item(w, "typedef", |w| {
1053 render_attributes_in_pre(w, it, "");
1054 write!(w, "{}", it.visibility.print_with_space(it.item_id, cx));
1055 write!(
1056 w,
1057 "type {}{}{where_clause} = {type_};",
1058 it.name.unwrap(),
1059 t.generics.print(cx),
1060 where_clause = print_where_clause(&t.generics, cx, 0, true),
1061 type_ = t.type_.print(cx),
1062 );
1063 });
1064 }
1065
1066 wrap_into_docblock(w, |w| write_content(w, cx, it, t));
1067
1068 document(w, cx, it, None, HeadingOffset::H2);
1069
1070 let def_id = it.item_id.expect_def_id();
1071 // Render any items associated directly to this alias, as otherwise they
1072 // won't be visible anywhere in the docs. It would be nice to also show
1073 // associated items from the aliased type (see discussion in #32077), but
1074 // we need #14072 to make sense of the generics.
1075 render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1076 document_type_layout(w, cx, def_id);
1077 }
1078
1079 fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Union) {
1080 wrap_into_docblock(w, |w| {
1081 wrap_item(w, "union", |w| {
1082 render_attributes_in_pre(w, it, "");
1083 render_union(w, it, Some(&s.generics), &s.fields, "", cx);
1084 });
1085 });
1086
1087 document(w, cx, it, None, HeadingOffset::H2);
1088
1089 let mut fields = s
1090 .fields
1091 .iter()
1092 .filter_map(|f| match *f.kind {
1093 clean::StructFieldItem(ref ty) => Some((f, ty)),
1094 _ => None,
1095 })
1096 .peekable();
1097 if fields.peek().is_some() {
1098 write!(
1099 w,
1100 "<h2 id=\"fields\" class=\"fields small-section-header\">\
1101 Fields<a href=\"#fields\" class=\"anchor\"></a></h2>"
1102 );
1103 for (field, ty) in fields {
1104 let name = field.name.expect("union field name");
1105 let id = format!("{}.{}", ItemType::StructField, name);
1106 write!(
1107 w,
1108 "<span id=\"{id}\" class=\"{shortty} small-section-header\">\
1109 <a href=\"#{id}\" class=\"anchor field\"></a>\
1110 <code>{name}: {ty}</code>\
1111 </span>",
1112 id = id,
1113 name = name,
1114 shortty = ItemType::StructField,
1115 ty = ty.print(cx),
1116 );
1117 if let Some(stability_class) = field.stability_class(cx.tcx()) {
1118 write!(w, "<span class=\"stab {stab}\"></span>", stab = stability_class);
1119 }
1120 document(w, cx, field, Some(it), HeadingOffset::H3);
1121 }
1122 }
1123 let def_id = it.item_id.expect_def_id();
1124 render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1125 document_type_layout(w, cx, def_id);
1126 }
1127
1128 fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]) {
1129 for (i, ty) in s.iter().enumerate() {
1130 if i > 0 {
1131 w.write_str(",&nbsp;");
1132 }
1133 match *ty.kind {
1134 clean::StrippedItem(box clean::StructFieldItem(_)) => w.write_str("_"),
1135 clean::StructFieldItem(ref ty) => write!(w, "{}", ty.print(cx)),
1136 _ => unreachable!(),
1137 }
1138 }
1139 }
1140
1141 fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
1142 wrap_into_docblock(w, |w| {
1143 wrap_item(w, "enum", |w| {
1144 render_attributes_in_pre(w, it, "");
1145 write!(
1146 w,
1147 "{}enum {}{}{}",
1148 it.visibility.print_with_space(it.item_id, cx),
1149 it.name.unwrap(),
1150 e.generics.print(cx),
1151 print_where_clause(&e.generics, cx, 0, true),
1152 );
1153 if e.variants.is_empty() && !e.variants_stripped {
1154 w.write_str(" {}");
1155 } else {
1156 w.write_str(" {\n");
1157 let count_variants = e.variants.len();
1158 let toggle = should_hide_fields(count_variants);
1159 if toggle {
1160 toggle_open(w, format_args!("{} variants", count_variants));
1161 }
1162 for v in &e.variants {
1163 w.write_str(" ");
1164 let name = v.name.unwrap();
1165 match *v.kind {
1166 clean::VariantItem(ref var) => match var {
1167 clean::Variant::CLike => write!(w, "{}", name),
1168 clean::Variant::Tuple(ref s) => {
1169 write!(w, "{}(", name);
1170 print_tuple_struct_fields(w, cx, s);
1171 w.write_str(")");
1172 }
1173 clean::Variant::Struct(ref s) => {
1174 render_struct(
1175 w,
1176 v,
1177 None,
1178 s.struct_type,
1179 &s.fields,
1180 " ",
1181 false,
1182 cx,
1183 );
1184 }
1185 },
1186 _ => unreachable!(),
1187 }
1188 w.write_str(",\n");
1189 }
1190
1191 if e.variants_stripped {
1192 w.write_str(" // some variants omitted\n");
1193 }
1194 if toggle {
1195 toggle_close(w);
1196 }
1197 w.write_str("}");
1198 }
1199 });
1200 });
1201
1202 document(w, cx, it, None, HeadingOffset::H2);
1203
1204 if !e.variants.is_empty() {
1205 write!(
1206 w,
1207 "<h2 id=\"variants\" class=\"variants small-section-header\">\
1208 Variants{}<a href=\"#variants\" class=\"anchor\"></a></h2>",
1209 document_non_exhaustive_header(it)
1210 );
1211 document_non_exhaustive(w, it);
1212 for variant in &e.variants {
1213 let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
1214 write!(
1215 w,
1216 "<h3 id=\"{id}\" class=\"variant small-section-header\">\
1217 <a href=\"#{id}\" class=\"anchor field\"></a>\
1218 <code>{name}",
1219 id = id,
1220 name = variant.name.unwrap()
1221 );
1222 if let clean::VariantItem(clean::Variant::Tuple(ref s)) = *variant.kind {
1223 w.write_str("(");
1224 print_tuple_struct_fields(w, cx, s);
1225 w.write_str(")");
1226 }
1227 w.write_str("</code>");
1228 render_stability_since(w, variant, it, cx.tcx());
1229 w.write_str("</h3>");
1230
1231 use crate::clean::Variant;
1232
1233 let heading_and_fields = match &*variant.kind {
1234 clean::VariantItem(Variant::Struct(s)) => Some(("Fields", &s.fields)),
1235 // Documentation on tuple variant fields is rare, so to reduce noise we only emit
1236 // the section if at least one field is documented.
1237 clean::VariantItem(Variant::Tuple(fields))
1238 if fields.iter().any(|f| f.doc_value().is_some()) =>
1239 {
1240 Some(("Tuple Fields", fields))
1241 }
1242 _ => None,
1243 };
1244
1245 if let Some((heading, fields)) = heading_and_fields {
1246 let variant_id =
1247 cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap()));
1248 write!(w, "<div class=\"sub-variant\" id=\"{id}\">", id = variant_id);
1249 write!(w, "<h4>{heading}</h4>", heading = heading);
1250 document_non_exhaustive(w, variant);
1251 for field in fields {
1252 match *field.kind {
1253 clean::StrippedItem(box clean::StructFieldItem(_)) => {}
1254 clean::StructFieldItem(ref ty) => {
1255 let id = cx.derive_id(format!(
1256 "variant.{}.field.{}",
1257 variant.name.unwrap(),
1258 field.name.unwrap()
1259 ));
1260 write!(
1261 w,
1262 "<div class=\"sub-variant-field\">\
1263 <span id=\"{id}\" class=\"variant small-section-header\">\
1264 <a href=\"#{id}\" class=\"anchor field\"></a>\
1265 <code>{f}:&nbsp;{t}</code>\
1266 </span>",
1267 id = id,
1268 f = field.name.unwrap(),
1269 t = ty.print(cx)
1270 );
1271 document(w, cx, field, Some(variant), HeadingOffset::H5);
1272 write!(w, "</div>");
1273 }
1274 _ => unreachable!(),
1275 }
1276 }
1277 w.write_str("</div>");
1278 }
1279
1280 document(w, cx, variant, Some(it), HeadingOffset::H4);
1281 }
1282 }
1283 let def_id = it.item_id.expect_def_id();
1284 render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1285 document_type_layout(w, cx, def_id);
1286 }
1287
1288 fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) {
1289 wrap_into_docblock(w, |w| {
1290 highlight::render_with_highlighting(
1291 &t.source,
1292 w,
1293 Some("macro"),
1294 None,
1295 None,
1296 it.span(cx.tcx()).inner().edition(),
1297 None,
1298 None,
1299 None,
1300 );
1301 });
1302 document(w, cx, it, None, HeadingOffset::H2)
1303 }
1304
1305 fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) {
1306 wrap_into_docblock(w, |w| {
1307 let name = it.name.expect("proc-macros always have names");
1308 match m.kind {
1309 MacroKind::Bang => {
1310 wrap_item(w, "macro", |w| {
1311 write!(w, "{}!() {{ /* proc-macro */ }}", name);
1312 });
1313 }
1314 MacroKind::Attr => {
1315 wrap_item(w, "attr", |w| {
1316 write!(w, "#[{}]", name);
1317 });
1318 }
1319 MacroKind::Derive => {
1320 wrap_item(w, "derive", |w| {
1321 write!(w, "#[derive({})]", name);
1322 if !m.helpers.is_empty() {
1323 w.push_str("\n{\n");
1324 w.push_str(" // Attributes available to this derive:\n");
1325 for attr in &m.helpers {
1326 writeln!(w, " #[{}]", attr);
1327 }
1328 w.push_str("}\n");
1329 }
1330 });
1331 }
1332 }
1333 });
1334 document(w, cx, it, None, HeadingOffset::H2)
1335 }
1336
1337 fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
1338 document(w, cx, it, None, HeadingOffset::H2);
1339 render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1340 }
1341
1342 fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) {
1343 wrap_into_docblock(w, |w| {
1344 wrap_item(w, "const", |w| {
1345 render_attributes_in_code(w, it);
1346
1347 write!(
1348 w,
1349 "{vis}const {name}: {typ}",
1350 vis = it.visibility.print_with_space(it.item_id, cx),
1351 name = it.name.unwrap(),
1352 typ = c.type_.print(cx),
1353 );
1354
1355 let value = c.value(cx.tcx());
1356 let is_literal = c.is_literal(cx.tcx());
1357 let expr = c.expr(cx.tcx());
1358 if value.is_some() || is_literal {
1359 write!(w, " = {expr};", expr = Escape(&expr));
1360 } else {
1361 w.write_str(";");
1362 }
1363
1364 if !is_literal {
1365 if let Some(value) = &value {
1366 let value_lowercase = value.to_lowercase();
1367 let expr_lowercase = expr.to_lowercase();
1368
1369 if value_lowercase != expr_lowercase
1370 && value_lowercase.trim_end_matches("i32") != expr_lowercase
1371 {
1372 write!(w, " // {value}", value = Escape(value));
1373 }
1374 }
1375 }
1376 });
1377 });
1378
1379 document(w, cx, it, None, HeadingOffset::H2)
1380 }
1381
1382 fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) {
1383 wrap_into_docblock(w, |w| {
1384 wrap_item(w, "struct", |w| {
1385 render_attributes_in_code(w, it);
1386 render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", true, cx);
1387 });
1388 });
1389
1390 document(w, cx, it, None, HeadingOffset::H2);
1391
1392 let mut fields = s
1393 .fields
1394 .iter()
1395 .filter_map(|f| match *f.kind {
1396 clean::StructFieldItem(ref ty) => Some((f, ty)),
1397 _ => None,
1398 })
1399 .peekable();
1400 if let CtorKind::Fictive | CtorKind::Fn = s.struct_type {
1401 if fields.peek().is_some() {
1402 write!(
1403 w,
1404 "<h2 id=\"fields\" class=\"fields small-section-header\">\
1405 {}{}<a href=\"#fields\" class=\"anchor\"></a>\
1406 </h2>",
1407 if let CtorKind::Fictive = s.struct_type { "Fields" } else { "Tuple Fields" },
1408 document_non_exhaustive_header(it)
1409 );
1410 document_non_exhaustive(w, it);
1411 for (index, (field, ty)) in fields.enumerate() {
1412 let field_name =
1413 field.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
1414 let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name));
1415 write!(
1416 w,
1417 "<span id=\"{id}\" class=\"{item_type} small-section-header\">\
1418 <a href=\"#{id}\" class=\"anchor field\"></a>\
1419 <code>{name}: {ty}</code>\
1420 </span>",
1421 item_type = ItemType::StructField,
1422 id = id,
1423 name = field_name,
1424 ty = ty.print(cx)
1425 );
1426 document(w, cx, field, Some(it), HeadingOffset::H3);
1427 }
1428 }
1429 }
1430 let def_id = it.item_id.expect_def_id();
1431 render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1432 document_type_layout(w, cx, def_id);
1433 }
1434
1435 fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) {
1436 wrap_into_docblock(w, |w| {
1437 wrap_item(w, "static", |w| {
1438 render_attributes_in_code(w, it);
1439 write!(
1440 w,
1441 "{vis}static {mutability}{name}: {typ}",
1442 vis = it.visibility.print_with_space(it.item_id, cx),
1443 mutability = s.mutability.print_with_space(),
1444 name = it.name.unwrap(),
1445 typ = s.type_.print(cx)
1446 );
1447 });
1448 });
1449 document(w, cx, it, None, HeadingOffset::H2)
1450 }
1451
1452 fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
1453 wrap_into_docblock(w, |w| {
1454 wrap_item(w, "foreigntype", |w| {
1455 w.write_str("extern {\n");
1456 render_attributes_in_code(w, it);
1457 write!(
1458 w,
1459 " {}type {};\n}}",
1460 it.visibility.print_with_space(it.item_id, cx),
1461 it.name.unwrap(),
1462 );
1463 });
1464 });
1465
1466 document(w, cx, it, None, HeadingOffset::H2);
1467
1468 render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1469 }
1470
1471 fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
1472 document(w, cx, it, None, HeadingOffset::H2)
1473 }
1474
1475 /// Compare two strings treating multi-digit numbers as single units (i.e. natural sort order).
1476 crate fn compare_names(mut lhs: &str, mut rhs: &str) -> Ordering {
1477 /// Takes a non-numeric and a numeric part from the given &str.
1478 fn take_parts<'a>(s: &mut &'a str) -> (&'a str, &'a str) {
1479 let i = s.find(|c: char| c.is_ascii_digit());
1480 let (a, b) = s.split_at(i.unwrap_or(s.len()));
1481 let i = b.find(|c: char| !c.is_ascii_digit());
1482 let (b, c) = b.split_at(i.unwrap_or(b.len()));
1483 *s = c;
1484 (a, b)
1485 }
1486
1487 while !lhs.is_empty() || !rhs.is_empty() {
1488 let (la, lb) = take_parts(&mut lhs);
1489 let (ra, rb) = take_parts(&mut rhs);
1490 // First process the non-numeric part.
1491 match la.cmp(ra) {
1492 Ordering::Equal => (),
1493 x => return x,
1494 }
1495 // Then process the numeric part, if both sides have one (and they fit in a u64).
1496 if let (Ok(ln), Ok(rn)) = (lb.parse::<u64>(), rb.parse::<u64>()) {
1497 match ln.cmp(&rn) {
1498 Ordering::Equal => (),
1499 x => return x,
1500 }
1501 }
1502 // Then process the numeric part again, but this time as strings.
1503 match lb.cmp(rb) {
1504 Ordering::Equal => (),
1505 x => return x,
1506 }
1507 }
1508
1509 Ordering::Equal
1510 }
1511
1512 pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
1513 let mut s = join_with_double_colon(&cx.current);
1514 s.push_str("::");
1515 s.push_str(item.name.unwrap().as_str());
1516 s
1517 }
1518
1519 pub(super) fn item_path(ty: ItemType, name: &str) -> String {
1520 match ty {
1521 ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)),
1522 _ => format!("{}.{}.html", ty, name),
1523 }
1524 }
1525
1526 fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) -> String {
1527 let mut bounds = String::new();
1528 if !t_bounds.is_empty() {
1529 if !trait_alias {
1530 bounds.push_str(": ");
1531 }
1532 for (i, p) in t_bounds.iter().enumerate() {
1533 if i > 0 {
1534 bounds.push_str(" + ");
1535 }
1536 bounds.push_str(&p.print(cx).to_string());
1537 }
1538 }
1539 bounds
1540 }
1541
1542 fn wrap_into_docblock<F>(w: &mut Buffer, f: F)
1543 where
1544 F: FnOnce(&mut Buffer),
1545 {
1546 w.write_str("<div class=\"docblock item-decl\">");
1547 f(w);
1548 w.write_str("</div>")
1549 }
1550
1551 fn wrap_item<F>(w: &mut Buffer, item_name: &str, f: F)
1552 where
1553 F: FnOnce(&mut Buffer),
1554 {
1555 w.write_fmt(format_args!("<pre class=\"rust {}\"><code>", item_name));
1556 f(w);
1557 w.write_str("</code></pre>");
1558 }
1559
1560 fn render_stability_since(
1561 w: &mut Buffer,
1562 item: &clean::Item,
1563 containing_item: &clean::Item,
1564 tcx: TyCtxt<'_>,
1565 ) -> bool {
1566 render_stability_since_raw(
1567 w,
1568 item.stable_since(tcx),
1569 item.const_stability(tcx),
1570 containing_item.stable_since(tcx),
1571 containing_item.const_stable_since(tcx),
1572 )
1573 }
1574
1575 fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
1576 let lhss = format!("{}", lhs.inner_impl().print(false, cx));
1577 let rhss = format!("{}", rhs.inner_impl().print(false, cx));
1578
1579 // lhs and rhs are formatted as HTML, which may be unnecessary
1580 compare_names(&lhss, &rhss)
1581 }
1582
1583 fn render_implementor(
1584 cx: &Context<'_>,
1585 implementor: &Impl,
1586 trait_: &clean::Item,
1587 w: &mut Buffer,
1588 implementor_dups: &FxHashMap<Symbol, (DefId, bool)>,
1589 aliases: &[String],
1590 ) {
1591 // If there's already another implementor that has the same abridged name, use the
1592 // full path, for example in `std::iter::ExactSizeIterator`
1593 let use_absolute = match implementor.inner_impl().for_ {
1594 clean::Type::Path { ref path, .. }
1595 | clean::BorrowedRef { type_: box clean::Type::Path { ref path, .. }, .. }
1596 if !path.is_assoc_ty() =>
1597 {
1598 implementor_dups[&path.last()].1
1599 }
1600 _ => false,
1601 };
1602 render_impl(
1603 w,
1604 cx,
1605 implementor,
1606 trait_,
1607 AssocItemLink::Anchor(None),
1608 RenderMode::Normal,
1609 Some(use_absolute),
1610 aliases,
1611 ImplRenderingParameters {
1612 show_def_docs: false,
1613 is_on_foreign_type: false,
1614 show_default_items: false,
1615 show_non_assoc_items: false,
1616 toggle_open_by_default: false,
1617 },
1618 );
1619 }
1620
1621 fn render_union(
1622 w: &mut Buffer,
1623 it: &clean::Item,
1624 g: Option<&clean::Generics>,
1625 fields: &[clean::Item],
1626 tab: &str,
1627 cx: &Context<'_>,
1628 ) {
1629 write!(w, "{}union {}", it.visibility.print_with_space(it.item_id, cx), it.name.unwrap());
1630 if let Some(g) = g {
1631 write!(w, "{}", g.print(cx));
1632 write!(w, "{}", print_where_clause(g, cx, 0, true));
1633 }
1634
1635 write!(w, " {{\n{}", tab);
1636 let count_fields =
1637 fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
1638 let toggle = should_hide_fields(count_fields);
1639 if toggle {
1640 toggle_open(w, format_args!("{} fields", count_fields));
1641 }
1642
1643 for field in fields {
1644 if let clean::StructFieldItem(ref ty) = *field.kind {
1645 write!(
1646 w,
1647 " {}{}: {},\n{}",
1648 field.visibility.print_with_space(field.item_id, cx),
1649 field.name.unwrap(),
1650 ty.print(cx),
1651 tab
1652 );
1653 }
1654 }
1655
1656 if it.has_stripped_fields().unwrap() {
1657 write!(w, " /* private fields */\n{}", tab);
1658 }
1659 if toggle {
1660 toggle_close(w);
1661 }
1662 w.write_str("}");
1663 }
1664
1665 fn render_struct(
1666 w: &mut Buffer,
1667 it: &clean::Item,
1668 g: Option<&clean::Generics>,
1669 ty: CtorKind,
1670 fields: &[clean::Item],
1671 tab: &str,
1672 structhead: bool,
1673 cx: &Context<'_>,
1674 ) {
1675 write!(
1676 w,
1677 "{}{}{}",
1678 it.visibility.print_with_space(it.item_id, cx),
1679 if structhead { "struct " } else { "" },
1680 it.name.unwrap()
1681 );
1682 if let Some(g) = g {
1683 write!(w, "{}", g.print(cx))
1684 }
1685 match ty {
1686 CtorKind::Fictive => {
1687 if let Some(g) = g {
1688 write!(w, "{}", print_where_clause(g, cx, 0, true),)
1689 }
1690 w.write_str(" {");
1691 let count_fields =
1692 fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
1693 let has_visible_fields = count_fields > 0;
1694 let toggle = should_hide_fields(count_fields);
1695 if toggle {
1696 toggle_open(w, format_args!("{} fields", count_fields));
1697 }
1698 for field in fields {
1699 if let clean::StructFieldItem(ref ty) = *field.kind {
1700 write!(
1701 w,
1702 "\n{} {}{}: {},",
1703 tab,
1704 field.visibility.print_with_space(field.item_id, cx),
1705 field.name.unwrap(),
1706 ty.print(cx),
1707 );
1708 }
1709 }
1710
1711 if has_visible_fields {
1712 if it.has_stripped_fields().unwrap() {
1713 write!(w, "\n{} /* private fields */", tab);
1714 }
1715 write!(w, "\n{}", tab);
1716 } else if it.has_stripped_fields().unwrap() {
1717 write!(w, " /* private fields */ ");
1718 }
1719 if toggle {
1720 toggle_close(w);
1721 }
1722 w.write_str("}");
1723 }
1724 CtorKind::Fn => {
1725 w.write_str("(");
1726 for (i, field) in fields.iter().enumerate() {
1727 if i > 0 {
1728 w.write_str(", ");
1729 }
1730 match *field.kind {
1731 clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"),
1732 clean::StructFieldItem(ref ty) => {
1733 write!(
1734 w,
1735 "{}{}",
1736 field.visibility.print_with_space(field.item_id, cx),
1737 ty.print(cx),
1738 )
1739 }
1740 _ => unreachable!(),
1741 }
1742 }
1743 w.write_str(")");
1744 if let Some(g) = g {
1745 write!(w, "{}", print_where_clause(g, cx, 0, false),)
1746 }
1747 // We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
1748 if structhead {
1749 w.write_str(";");
1750 }
1751 }
1752 CtorKind::Const => {
1753 // Needed for PhantomData.
1754 if let Some(g) = g {
1755 write!(w, "{}", print_where_clause(g, cx, 0, false),)
1756 }
1757 w.write_str(";");
1758 }
1759 }
1760 }
1761
1762 fn document_non_exhaustive_header(item: &clean::Item) -> &str {
1763 if item.is_non_exhaustive() { " (Non-exhaustive)" } else { "" }
1764 }
1765
1766 fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
1767 if item.is_non_exhaustive() {
1768 write!(
1769 w,
1770 "<details class=\"rustdoc-toggle non-exhaustive\">\
1771 <summary class=\"hideme\"><span>{}</span></summary>\
1772 <div class=\"docblock\">",
1773 {
1774 if item.is_struct() {
1775 "This struct is marked as non-exhaustive"
1776 } else if item.is_enum() {
1777 "This enum is marked as non-exhaustive"
1778 } else if item.is_variant() {
1779 "This variant is marked as non-exhaustive"
1780 } else {
1781 "This type is marked as non-exhaustive"
1782 }
1783 }
1784 );
1785
1786 if item.is_struct() {
1787 w.write_str(
1788 "Non-exhaustive structs could have additional fields added in future. \
1789 Therefore, non-exhaustive structs cannot be constructed in external crates \
1790 using the traditional <code>Struct { .. }</code> syntax; cannot be \
1791 matched against without a wildcard <code>..</code>; and \
1792 struct update syntax will not work.",
1793 );
1794 } else if item.is_enum() {
1795 w.write_str(
1796 "Non-exhaustive enums could have additional variants added in future. \
1797 Therefore, when matching against variants of non-exhaustive enums, an \
1798 extra wildcard arm must be added to account for any future variants.",
1799 );
1800 } else if item.is_variant() {
1801 w.write_str(
1802 "Non-exhaustive enum variants could have additional fields added in future. \
1803 Therefore, non-exhaustive enum variants cannot be constructed in external \
1804 crates and cannot be matched against.",
1805 );
1806 } else {
1807 w.write_str(
1808 "This type will require a wildcard arm in any match statements or constructors.",
1809 );
1810 }
1811
1812 w.write_str("</div></details>");
1813 }
1814 }
1815
1816 fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1817 fn write_size_of_layout(w: &mut Buffer, layout: Layout<'_>, tag_size: u64) {
1818 if layout.abi().is_unsized() {
1819 write!(w, "(unsized)");
1820 } else {
1821 let bytes = layout.size().bytes() - tag_size;
1822 write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
1823 }
1824 }
1825
1826 if !cx.shared.show_type_layout {
1827 return;
1828 }
1829
1830 writeln!(w, "<h2 class=\"small-section-header\">Layout</h2>");
1831 writeln!(w, "<div class=\"docblock\">");
1832
1833 let tcx = cx.tcx();
1834 let param_env = tcx.param_env(ty_def_id);
1835 let ty = tcx.type_of(ty_def_id);
1836 match tcx.layout_of(param_env.and(ty)) {
1837 Ok(ty_layout) => {
1838 writeln!(
1839 w,
1840 "<div class=\"warning\"><p><strong>Note:</strong> Most layout information is \
1841 <strong>completely unstable</strong> and may even differ between compilations. \
1842 The only exception is types with certain <code>repr(...)</code> attributes. \
1843 Please see the Rust Reference’s \
1844 <a href=\"https://doc.rust-lang.org/reference/type-layout.html\">“Type Layout”</a> \
1845 chapter for details on type layout guarantees.</p></div>"
1846 );
1847 w.write_str("<p><strong>Size:</strong> ");
1848 write_size_of_layout(w, ty_layout.layout, 0);
1849 writeln!(w, "</p>");
1850 if let Variants::Multiple { variants, tag, tag_encoding, .. } =
1851 &ty_layout.layout.variants()
1852 {
1853 if !variants.is_empty() {
1854 w.write_str(
1855 "<p><strong>Size for each variant:</strong></p>\
1856 <ul>",
1857 );
1858
1859 let Adt(adt, _) = ty_layout.ty.kind() else {
1860 span_bug!(tcx.def_span(ty_def_id), "not an adt")
1861 };
1862
1863 let tag_size = if let TagEncoding::Niche { .. } = tag_encoding {
1864 0
1865 } else if let Primitive::Int(i, _) = tag.primitive() {
1866 i.size().bytes()
1867 } else {
1868 span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int")
1869 };
1870
1871 for (index, layout) in variants.iter_enumerated() {
1872 let name = adt.variant(index).name;
1873 write!(w, "<li><code>{name}</code>: ", name = name);
1874 write_size_of_layout(w, *layout, tag_size);
1875 writeln!(w, "</li>");
1876 }
1877 w.write_str("</ul>");
1878 }
1879 }
1880 }
1881 // This kind of layout error can occur with valid code, e.g. if you try to
1882 // get the layout of a generic type such as `Vec<T>`.
1883 Err(LayoutError::Unknown(_)) => {
1884 writeln!(
1885 w,
1886 "<p><strong>Note:</strong> Unable to compute type layout, \
1887 possibly due to this type having generic parameters. \
1888 Layout can only be computed for concrete, fully-instantiated types.</p>"
1889 );
1890 }
1891 // This kind of error probably can't happen with valid code, but we don't
1892 // want to panic and prevent the docs from building, so we just let the
1893 // user know that we couldn't compute the layout.
1894 Err(LayoutError::SizeOverflow(_)) => {
1895 writeln!(
1896 w,
1897 "<p><strong>Note:</strong> Encountered an error during type layout; \
1898 the type was too big.</p>"
1899 );
1900 }
1901 Err(LayoutError::NormalizationFailure(_, _)) => {
1902 writeln!(
1903 w,
1904 "<p><strong>Note:</strong> Encountered an error during type layout; \
1905 the type failed to be normalized.</p>"
1906 )
1907 }
1908 }
1909
1910 writeln!(w, "</div>");
1911 }
1912
1913 fn pluralize(count: usize) -> &'static str {
1914 if count > 1 { "s" } else { "" }
1915 }