1 //! HTML formatting module
3 //! This module contains a large number of `fmt::Display` implementations for
4 //! various types in `rustdoc::clean`. These implementations all currently
5 //! assume that HTML output is desired, although it may be possible to redesign
6 //! them in the future to instead emit any format desired.
12 use rustc_attr::{ConstStability, StabilityLevel};
13 use rustc_data_structures::captures::Captures;
14 use rustc_data_structures::fx::FxHashSet;
16 use rustc_hir::def::DefKind;
17 use rustc_hir::def_id::DefId;
19 use rustc_middle::ty::DefIdTree;
20 use rustc_middle::ty::TyCtxt;
21 use rustc_span::def_id::CRATE_DEF_INDEX;
22 use rustc_span::{sym, Symbol};
23 use rustc_target::spec::abi::Abi;
26 self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
29 use crate::formats::item_type::ItemType;
30 use crate::html::escape::Escape;
31 use crate::html::render::Context;
33 use super::url_parts_builder::estimate_item_path_byte_length;
34 use super::url_parts_builder::UrlPartsBuilder;
37 fn print(self, buffer: &mut Buffer);
42 F: FnOnce(&mut Buffer),
44 fn print(self, buffer: &mut Buffer) {
49 impl Print for String {
50 fn print(self, buffer: &mut Buffer) {
51 buffer.write_str(&self);
55 impl Print for &'_ str {
56 fn print(self, buffer: &mut Buffer) {
57 buffer.write_str(self);
61 #[derive(Debug, Clone)]
67 impl core::fmt::Write for Buffer {
69 fn write_str(&mut self, s: &str) -> fmt::Result {
70 self.buffer.write_str(s)
74 fn write_char(&mut self, c: char) -> fmt::Result {
75 self.buffer.write_char(c)
79 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
80 self.buffer.write_fmt(args)
85 crate fn empty_from(v: &Buffer) -> Buffer {
86 Buffer { for_html: v.for_html, buffer: String::new() }
89 crate fn html() -> Buffer {
90 Buffer { for_html: true, buffer: String::new() }
93 crate fn new() -> Buffer {
94 Buffer { for_html: false, buffer: String::new() }
97 crate fn is_empty(&self) -> bool {
98 self.buffer.is_empty()
101 crate fn into_inner(self) -> String {
105 crate fn insert_str(&mut self, idx: usize, s: &str) {
106 self.buffer.insert_str(idx, s);
109 crate fn push_str(&mut self, s: &str) {
110 self.buffer.push_str(s);
113 crate fn push_buffer(&mut self, other: Buffer) {
114 self.buffer.push_str(&other.buffer);
117 // Intended for consumption by write! and writeln! (std::fmt) but without
118 // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
120 crate fn write_str(&mut self, s: &str) {
121 self.buffer.push_str(s);
124 // Intended for consumption by write! and writeln! (std::fmt) but without
125 // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
127 crate fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
129 self.buffer.write_fmt(v).unwrap();
132 crate fn to_display<T: Print>(mut self, t: T) -> String {
137 crate fn is_for_html(&self) -> bool {
141 crate fn reserve(&mut self, additional: usize) {
142 self.buffer.reserve(additional)
146 fn comma_sep<T: fmt::Display>(items: impl Iterator<Item = T>) -> impl fmt::Display {
147 display_fn(move |f| {
148 for (i, item) in items.enumerate() {
152 fmt::Display::fmt(&item, f)?;
158 crate fn print_generic_bounds<'a, 'tcx: 'a>(
159 bounds: &'a [clean::GenericBound],
160 cx: &'a Context<'tcx>,
161 ) -> impl fmt::Display + 'a + Captures<'tcx> {
162 display_fn(move |f| {
163 let mut bounds_dup = FxHashSet::default();
165 for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(b.clone())).enumerate() {
169 fmt::Display::fmt(&bound.print(cx), f)?;
175 impl clean::GenericParamDef {
176 crate fn print<'a, 'tcx: 'a>(
178 cx: &'a Context<'tcx>,
179 ) -> impl fmt::Display + 'a + Captures<'tcx> {
180 display_fn(move |f| match &self.kind {
181 clean::GenericParamDefKind::Lifetime { outlives } => {
182 write!(f, "{}", self.name)?;
184 if !outlives.is_empty() {
186 for (i, lt) in outlives.iter().enumerate() {
190 write!(f, "{}", lt.print())?;
196 clean::GenericParamDefKind::Type { bounds, default, .. } => {
197 f.write_str(self.name.as_str())?;
199 if !bounds.is_empty() {
201 write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
203 write!(f, ": {}", print_generic_bounds(bounds, cx))?;
207 if let Some(ref ty) = default {
209 write!(f, " = {:#}", ty.print(cx))?;
211 write!(f, " = {}", ty.print(cx))?;
217 clean::GenericParamDefKind::Const { ty, default, .. } => {
219 write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
221 write!(f, "const {}: {}", self.name, ty.print(cx))?;
224 if let Some(default) = default {
226 write!(f, " = {:#}", default)?;
228 write!(f, " = {}", default)?;
238 impl clean::Generics {
239 crate fn print<'a, 'tcx: 'a>(
241 cx: &'a Context<'tcx>,
242 ) -> impl fmt::Display + 'a + Captures<'tcx> {
243 display_fn(move |f| {
244 let mut real_params =
245 self.params.iter().filter(|p| !p.is_synthetic_type_param()).peekable();
246 if real_params.peek().is_none() {
251 write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx))))
253 write!(f, "<{}>", comma_sep(real_params.map(|g| g.print(cx))))
259 /// * The Generics from which to emit a where-clause.
260 /// * The number of spaces to indent each line with.
261 /// * Whether the where-clause needs to add a comma and newline after the last bound.
262 crate fn print_where_clause<'a, 'tcx: 'a>(
263 gens: &'a clean::Generics,
264 cx: &'a Context<'tcx>,
267 ) -> impl fmt::Display + 'a + Captures<'tcx> {
268 display_fn(move |f| {
269 if gens.where_predicates.is_empty() {
272 let mut clause = String::new();
274 clause.push_str(" where");
277 clause.push_str(" <span class=\"where fmt-newline\">where");
279 clause.push_str(" <span class=\"where\">where");
282 for (i, pred) in gens.where_predicates.iter().enumerate() {
286 clause.push_str("<br>");
290 clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
292 let for_prefix = match bound_params.len() {
294 _ if f.alternate() => {
297 comma_sep(bound_params.iter().map(|lt| lt.print()))
302 comma_sep(bound_params.iter().map(|lt| lt.print()))
307 clause.push_str(&format!(
311 print_generic_bounds(bounds, cx)
314 clause.push_str(&format!(
318 print_generic_bounds(bounds, cx)
322 clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
323 clause.push_str(&format!(
328 .map(|b| b.print(cx).to_string())
333 clean::WherePredicate::EqPredicate { lhs, rhs } => {
335 clause.push_str(&format!("{:#} == {:#}", lhs.print(cx), rhs.print(cx),));
337 clause.push_str(&format!("{} == {}", lhs.print(cx), rhs.print(cx),));
342 if i < gens.where_predicates.len() - 1 || end_newline {
348 // add a space so stripping <br> tags and breaking spaces still renders properly
352 clause.push_str(" ");
357 clause.push_str("</span>");
358 let padding = " ".repeat(indent + 4);
359 clause = clause.replace("<br>", &format!("<br>{}", padding));
360 clause.insert_str(0, &" ".repeat(indent.saturating_sub(1)));
362 clause.insert_str(0, "<br>");
365 write!(f, "{}", clause)
369 impl clean::Lifetime {
370 crate fn print(&self) -> impl fmt::Display + '_ {
375 impl clean::Constant {
376 crate fn print(&self, tcx: TyCtxt<'_>) -> impl fmt::Display + '_ {
377 let expr = self.expr(tcx);
380 if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
386 impl clean::PolyTrait {
387 fn print<'a, 'tcx: 'a>(
389 cx: &'a Context<'tcx>,
390 ) -> impl fmt::Display + 'a + Captures<'tcx> {
391 display_fn(move |f| {
392 if !self.generic_params.is_empty() {
397 comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
403 comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
408 write!(f, "{:#}", self.trait_.print(cx))
410 write!(f, "{}", self.trait_.print(cx))
416 impl clean::GenericBound {
417 crate fn print<'a, 'tcx: 'a>(
419 cx: &'a Context<'tcx>,
420 ) -> impl fmt::Display + 'a + Captures<'tcx> {
421 display_fn(move |f| match self {
422 clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
423 clean::GenericBound::TraitBound(ty, modifier) => {
424 let modifier_str = match modifier {
425 hir::TraitBoundModifier::None => "",
426 hir::TraitBoundModifier::Maybe => "?",
427 hir::TraitBoundModifier::MaybeConst => "~const",
430 write!(f, "{}{:#}", modifier_str, ty.print(cx))
432 write!(f, "{}{}", modifier_str, ty.print(cx))
439 impl clean::GenericArgs {
440 fn print<'a, 'tcx: 'a>(
442 cx: &'a Context<'tcx>,
443 ) -> impl fmt::Display + 'a + Captures<'tcx> {
444 display_fn(move |f| {
446 clean::GenericArgs::AngleBracketed { args, bindings } => {
447 if !args.is_empty() || !bindings.is_empty() {
451 f.write_str("<")?;
453 let mut comma = false;
460 write!(f, "{:#}", arg.print(cx))?;
462 write!(f, "{}", arg.print(cx))?;
465 for binding in bindings {
471 write!(f, "{:#}", binding.print(cx))?;
473 write!(f, "{}", binding.print(cx))?;
479 f.write_str(">")?;
483 clean::GenericArgs::Parenthesized { inputs, output } => {
485 let mut comma = false;
492 write!(f, "{:#}", ty.print(cx))?;
494 write!(f, "{}", ty.print(cx))?;
498 if let Some(ref ty) = *output {
500 write!(f, " -> {:#}", ty.print(cx))?;
502 write!(f, " -> {}", ty.print(cx))?;
512 // Possible errors when computing href link source for a `DefId`
513 crate enum HrefError {
514 /// This item is known to rustdoc, but from a crate that does not have documentation generated.
516 /// This can only happen for non-local items.
517 DocumentationNotBuilt,
518 /// This can only happen for non-local items when `--document-private-items` is not passed.
520 // Not in external cache, href link should be in same page
524 // Panics if `syms` is empty.
525 crate fn join_with_double_colon(syms: &[Symbol]) -> String {
526 let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
527 s.push_str(&syms[0].as_str());
528 for sym in &syms[1..] {
530 s.push_str(&sym.as_str());
535 crate fn href_with_root_path(
538 root_path: Option<&str>,
539 ) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
541 let def_kind = tcx.def_kind(did);
542 let did = match def_kind {
543 DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
544 // documented on their parent's page
545 tcx.parent(did).unwrap()
549 let cache = cx.cache();
550 let relative_to = &cx.current;
551 fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
552 if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
556 && !cache.access_levels.is_public(did)
557 && !cache.document_private
558 && !cache.primitive_locations.values().any(|&id| id == did)
560 return Err(HrefError::Private);
563 let mut is_remote = false;
564 let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
565 Some(&(ref fqp, shortty)) => (fqp, shortty, {
566 let module_fqp = to_module_fqp(shortty, fqp.as_slice());
567 debug!(?fqp, ?shortty, ?module_fqp);
568 href_relative_parts(module_fqp, relative_to).collect()
571 if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&did) {
572 let module_fqp = to_module_fqp(shortty, fqp);
576 match cache.extern_locations[&did.krate] {
577 ExternalLocation::Remote(ref s) => {
579 let s = s.trim_end_matches('/');
580 let mut builder = UrlPartsBuilder::singleton(s);
581 builder.extend(module_fqp.iter().copied());
584 ExternalLocation::Local => {
585 href_relative_parts(module_fqp, relative_to).collect()
587 ExternalLocation::Unknown => return Err(HrefError::DocumentationNotBuilt),
591 return Err(HrefError::NotInExternalCache);
596 if let Some(root_path) = root_path {
597 let root = root_path.trim_end_matches('/');
598 url_parts.push_front(root);
603 ItemType::Module => {
604 url_parts.push("index.html");
607 let prefix = shortty.as_str();
608 let last = fqp.last().unwrap();
609 url_parts.push_fmt(format_args!("{}.{}.html", prefix, last));
612 Ok((url_parts.finish(), shortty, fqp.to_vec()))
615 crate fn href(did: DefId, cx: &Context<'_>) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
616 href_with_root_path(did, cx, None)
619 /// Both paths should only be modules.
620 /// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
621 /// both need `../iter/trait.Iterator.html` to get at the iterator trait.
622 crate fn href_relative_parts<'fqp>(
624 relative_to_fqp: &[Symbol],
625 ) -> Box<dyn Iterator<Item = Symbol> + 'fqp> {
626 for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
627 // e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
629 let dissimilar_part_count = relative_to_fqp.len() - i;
630 let fqp_module = &fqp[i..fqp.len()];
631 return box iter::repeat(sym::dotdot)
632 .take(dissimilar_part_count)
633 .chain(fqp_module.iter().copied());
636 // e.g. linking to std::sync::atomic from std::sync
637 if relative_to_fqp.len() < fqp.len() {
638 box fqp[relative_to_fqp.len()..fqp.len()].iter().copied()
639 // e.g. linking to std::sync from std::sync::atomic
640 } else if fqp.len() < relative_to_fqp.len() {
641 let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
642 box iter::repeat(sym::dotdot).take(dissimilar_part_count)
643 // linking to the same module
649 /// Used to render a [`clean::Path`].
650 fn resolved_path<'cx>(
651 w: &mut fmt::Formatter<'_>,
656 cx: &'cx Context<'_>,
658 let last = path.segments.last().unwrap();
661 for seg in &path.segments[..path.segments.len() - 1] {
662 write!(w, "{}::", seg.name)?;
666 write!(w, "{}{:#}", &last.name, last.args.print(cx))?;
668 let path = if use_absolute {
669 if let Ok((_, _, fqp)) = href(did, cx) {
672 join_with_double_colon(&fqp[..fqp.len() - 1]),
673 anchor(did, *fqp.last().unwrap(), cx)
676 last.name.to_string()
679 anchor(did, last.name, cx).to_string()
681 write!(w, "{}{}", path, last.args.print(cx))?;
687 f: &mut fmt::Formatter<'_>,
688 prim: clean::PrimitiveType,
693 let mut needs_termination = false;
695 match m.primitive_locations.get(&prim) {
696 Some(&def_id) if def_id.is_local() => {
697 let len = cx.current.len();
698 let len = if len == 0 { 0 } else { len - 1 };
701 "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
705 needs_termination = true;
708 let loc = match m.extern_locations[&def_id.krate] {
709 ExternalLocation::Remote(ref s) => {
710 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
711 let builder: UrlPartsBuilder =
712 [s.as_str().trim_end_matches('/'), cname_sym.as_str()]
717 ExternalLocation::Local => {
718 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
719 Some(if cx.current.first() == Some(&cname_sym) {
720 iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
722 iter::repeat(sym::dotdot)
723 .take(cx.current.len())
724 .chain(iter::once(cname_sym))
728 ExternalLocation::Unknown => None,
730 if let Some(mut loc) = loc {
731 loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
732 write!(f, "<a class=\"primitive\" href=\"{}\">", loc.finish())?;
733 needs_termination = true;
739 write!(f, "{}", name)?;
740 if needs_termination {
746 /// Helper to render type parameters
747 fn tybounds<'a, 'tcx: 'a>(
748 bounds: &'a [clean::PolyTrait],
749 lt: &'a Option<clean::Lifetime>,
750 cx: &'a Context<'tcx>,
751 ) -> impl fmt::Display + 'a + Captures<'tcx> {
752 display_fn(move |f| {
753 for (i, bound) in bounds.iter().enumerate() {
758 fmt::Display::fmt(&bound.print(cx), f)?;
761 if let Some(lt) = lt {
763 fmt::Display::fmt(<.print(), f)?;
769 crate fn anchor<'a, 'cx: 'a>(
772 cx: &'cx Context<'_>,
773 ) -> impl fmt::Display + 'a {
774 let parts = href(did, cx);
775 display_fn(move |f| {
776 if let Ok((url, short_ty, fqp)) = parts {
779 r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
783 join_with_double_colon(&fqp),
787 write!(f, "{}", text)
794 f: &mut fmt::Formatter<'_>,
796 cx: &'cx Context<'_>,
798 trace!("fmt_type(t = {:?})", t);
801 clean::Generic(name) => write!(f, "{}", name),
802 clean::Type::Path { ref path } => {
803 // Paths like `T::Output` and `Self::Output` should be rendered with all segments.
804 let did = path.def_id();
805 resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
807 clean::DynTrait(ref bounds, ref lt) => {
808 f.write_str("dyn ")?;
809 fmt::Display::fmt(&tybounds(bounds, lt, cx), f)
811 clean::Infer => write!(f, "_"),
812 clean::Primitive(clean::PrimitiveType::Never) => {
813 primitive_link(f, PrimitiveType::Never, "!", cx)
815 clean::Primitive(prim) => primitive_link(f, prim, prim.as_sym().as_str(), cx),
816 clean::BareFunction(ref decl) => {
821 decl.print_hrtb_with_space(cx),
822 decl.unsafety.print_with_space(),
823 print_abi_with_space(decl.abi),
830 decl.print_hrtb_with_space(cx),
831 decl.unsafety.print_with_space(),
832 print_abi_with_space(decl.abi)
834 primitive_link(f, PrimitiveType::Fn, "fn", cx)?;
835 write!(f, "{}", decl.decl.print(cx))
838 clean::Tuple(ref typs) => {
840 &[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
842 primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
843 // Carry `f.alternate()` into this display w/o branching manually.
844 fmt::Display::fmt(&one.print(cx), f)?;
845 primitive_link(f, PrimitiveType::Tuple, ",)", cx)
848 primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
849 for (i, item) in many.iter().enumerate() {
853 fmt::Display::fmt(&item.print(cx), f)?;
855 primitive_link(f, PrimitiveType::Tuple, ")", cx)
859 clean::Slice(ref t) => {
860 primitive_link(f, PrimitiveType::Slice, "[", cx)?;
861 fmt::Display::fmt(&t.print(cx), f)?;
862 primitive_link(f, PrimitiveType::Slice, "]", cx)
864 clean::Array(ref t, ref n) => {
865 primitive_link(f, PrimitiveType::Array, "[", cx)?;
866 fmt::Display::fmt(&t.print(cx), f)?;
868 primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
870 primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
873 clean::RawPointer(m, ref t) => {
875 hir::Mutability::Mut => "mut",
876 hir::Mutability::Not => "const",
879 if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
880 let text = if f.alternate() {
881 format!("*{} {:#}", m, t.print(cx))
883 format!("*{} {}", m, t.print(cx))
885 primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
887 primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
888 fmt::Display::fmt(&t.print(cx), f)
891 clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
893 Some(l) => format!("{} ", l.print()),
896 let m = mutability.print_with_space();
897 let amp = if f.alternate() { "&".to_string() } else { "&".to_string() };
899 clean::Slice(ref bt) => {
900 // `BorrowedRef{ ... Slice(T) }` is `&[T]`
902 clean::Generic(_) => {
906 PrimitiveType::Slice,
907 &format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cx)),
913 PrimitiveType::Slice,
914 &format!("{}{}{}[{}]", amp, lt, m, bt.print(cx)),
922 PrimitiveType::Slice,
923 &format!("{}{}{}[", amp, lt, m),
927 write!(f, "{:#}", bt.print(cx))?;
929 write!(f, "{}", bt.print(cx))?;
931 primitive_link(f, PrimitiveType::Slice, "]", cx)
935 clean::DynTrait(ref bounds, ref trait_lt)
936 if bounds.len() > 1 || trait_lt.is_some() =>
938 write!(f, "{}{}{}(", amp, lt, m)?;
939 fmt_type(ty, f, use_absolute, cx)?;
942 clean::Generic(..) => {
945 PrimitiveType::Reference,
946 &format!("{}{}{}", amp, lt, m),
949 fmt_type(ty, f, use_absolute, cx)
952 write!(f, "{}{}{}", amp, lt, m)?;
953 fmt_type(ty, f, use_absolute, cx)
957 clean::ImplTrait(ref bounds) => {
959 write!(f, "impl {:#}", print_generic_bounds(bounds, cx))
961 write!(f, "impl {}", print_generic_bounds(bounds, cx))
964 clean::QPath { ref name, ref self_type, ref trait_, ref self_def_id } => {
965 let should_show_cast = !trait_.segments.is_empty()
967 .zip(Some(trait_.def_id()))
968 .map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_);
970 if should_show_cast {
971 write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
973 write!(f, "{:#}::", self_type.print(cx))?
976 if should_show_cast {
977 write!(f, "<{} as {}>::", self_type.print(cx), trait_.print(cx))?
979 write!(f, "{}::", self_type.print(cx))?
982 // It's pretty unsightly to look at `<A as B>::C` in output, and
983 // we've got hyperlinking on our side, so try to avoid longer
984 // notation as much as possible by making `C` a hyperlink to trait
985 // `B` to disambiguate.
987 // FIXME: this is still a lossy conversion and there should probably
988 // be a better way of representing this in general? Most of
989 // the ugliness comes from inlining across crates where
990 // everything comes in as a fully resolved QPath (hard to
992 match href(trait_.def_id(), cx) {
993 Ok((ref url, _, ref path)) if !f.alternate() => {
996 "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
997 title=\"type {path}::{name}\">{name}</a>",
999 shortty = ItemType::AssocType,
1001 path = join_with_double_colon(path),
1004 _ => write!(f, "{}", name)?,
1012 crate fn print<'b, 'a: 'b, 'tcx: 'a>(
1014 cx: &'a Context<'tcx>,
1015 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1016 display_fn(move |f| fmt_type(self, f, false, cx))
1021 crate fn print<'b, 'a: 'b, 'tcx: 'a>(
1023 cx: &'a Context<'tcx>,
1024 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1025 display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1030 crate fn print<'a, 'tcx: 'a>(
1033 cx: &'a Context<'tcx>,
1034 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1035 display_fn(move |f| {
1037 write!(f, "impl{:#} ", self.generics.print(cx))?;
1039 write!(f, "impl{} ", self.generics.print(cx))?;
1042 if let Some(ref ty) = self.trait_ {
1043 match self.polarity {
1044 ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => {}
1045 ty::ImplPolarity::Negative => write!(f, "!")?,
1047 fmt::Display::fmt(&ty.print(cx), f)?;
1048 write!(f, " for ")?;
1051 if let Some(ref ty) = self.kind.as_blanket_ty() {
1052 fmt_type(ty, f, use_absolute, cx)?;
1054 fmt_type(&self.for_, f, use_absolute, cx)?;
1057 fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, true), f)?;
1063 impl clean::Arguments {
1064 crate fn print<'a, 'tcx: 'a>(
1066 cx: &'a Context<'tcx>,
1067 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1068 display_fn(move |f| {
1069 for (i, input) in self.values.iter().enumerate() {
1070 if !input.name.is_empty() {
1071 write!(f, "{}: ", input.name)?;
1074 write!(f, "{:#}", input.type_.print(cx))?;
1076 write!(f, "{}", input.type_.print(cx))?;
1078 if i + 1 < self.values.len() {
1087 impl clean::FnRetTy {
1088 crate fn print<'a, 'tcx: 'a>(
1090 cx: &'a Context<'tcx>,
1091 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1092 display_fn(move |f| match self {
1093 clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
1094 clean::Return(ty) if f.alternate() => {
1095 write!(f, " -> {:#}", ty.print(cx))
1097 clean::Return(ty) => write!(f, " -> {}", ty.print(cx)),
1098 clean::DefaultReturn => Ok(()),
1103 impl clean::BareFunctionDecl {
1104 fn print_hrtb_with_space<'a, 'tcx: 'a>(
1106 cx: &'a Context<'tcx>,
1107 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1108 display_fn(move |f| {
1109 if !self.generic_params.is_empty() {
1113 comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
1122 impl clean::FnDecl {
1123 crate fn print<'b, 'a: 'b, 'tcx: 'a>(
1125 cx: &'a Context<'tcx>,
1126 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1127 display_fn(move |f| {
1128 let ellipsis = if self.c_variadic { ", ..." } else { "" };
1132 "({args:#}{ellipsis}){arrow:#}",
1133 args = self.inputs.print(cx),
1134 ellipsis = ellipsis,
1135 arrow = self.output.print(cx)
1140 "({args}{ellipsis}){arrow}",
1141 args = self.inputs.print(cx),
1142 ellipsis = ellipsis,
1143 arrow = self.output.print(cx)
1149 /// * `header_len`: The length of the function header and name. In other words, the number of
1150 /// characters in the function declaration up to but not including the parentheses.
1151 /// <br>Used to determine line-wrapping.
1152 /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
1154 /// * `asyncness`: Whether the function is async or not.
1155 crate fn full_print<'a, 'tcx: 'a>(
1159 asyncness: hir::IsAsync,
1160 cx: &'a Context<'tcx>,
1161 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1162 display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, f, cx))
1165 fn inner_full_print(
1169 asyncness: hir::IsAsync,
1170 f: &mut fmt::Formatter<'_>,
1173 let amp = if f.alternate() { "&" } else { "&" };
1174 let mut args = String::new();
1175 let mut args_plain = String::new();
1176 for (i, input) in self.inputs.values.iter().enumerate() {
1178 args.push_str("<br>");
1181 if let Some(selfty) = input.to_self() {
1183 clean::SelfValue => {
1184 args.push_str("self");
1185 args_plain.push_str("self");
1187 clean::SelfBorrowed(Some(ref lt), mtbl) => {
1188 args.push_str(&format!(
1192 mtbl.print_with_space()
1194 args_plain.push_str(&format!(
1197 mtbl.print_with_space()
1200 clean::SelfBorrowed(None, mtbl) => {
1201 args.push_str(&format!("{}{}self", amp, mtbl.print_with_space()));
1202 args_plain.push_str(&format!("&{}self", mtbl.print_with_space()));
1204 clean::SelfExplicit(ref typ) => {
1206 args.push_str(&format!("self: {:#}", typ.print(cx)));
1208 args.push_str(&format!("self: {}", typ.print(cx)));
1210 args_plain.push_str(&format!("self: {:#}", typ.print(cx)));
1215 args.push_str(" <br>");
1216 args_plain.push(' ');
1219 args.push_str("const ");
1220 args_plain.push_str("const ");
1222 if !input.name.is_empty() {
1223 args.push_str(&format!("{}: ", input.name));
1224 args_plain.push_str(&format!("{}: ", input.name));
1228 args.push_str(&format!("{:#}", input.type_.print(cx)));
1230 args.push_str(&input.type_.print(cx).to_string());
1232 args_plain.push_str(&format!("{:#}", input.type_.print(cx)));
1234 if i + 1 < self.inputs.values.len() {
1236 args_plain.push(',');
1240 let mut args_plain = format!("({})", args_plain);
1242 if self.c_variadic {
1243 args.push_str(",<br> ...");
1244 args_plain.push_str(", ...");
1248 let arrow = if let hir::IsAsync::Async = asyncness {
1249 let output = self.sugared_async_return_type();
1250 arrow_plain = format!("{:#}", output.print(cx));
1251 if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) }
1253 arrow_plain = format!("{:#}", self.output.print(cx));
1254 if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }
1257 let declaration_len = header_len + args_plain.len() + arrow_plain.len();
1258 let output = if declaration_len > 80 {
1259 let full_pad = format!("<br>{}", " ".repeat(indent + 4));
1260 let close_pad = format!("<br>{}", " ".repeat(indent));
1262 "({args}{close}){arrow}",
1263 args = args.replace("<br>", &full_pad),
1268 format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
1272 write!(f, "{}", output.replace("<br>", "\n"))
1274 write!(f, "{}", output)
1279 impl clean::Visibility {
1280 crate fn print_with_space<'a, 'tcx: 'a>(
1283 cx: &'a Context<'tcx>,
1284 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1285 let to_print = match self {
1286 clean::Public => "pub ".to_owned(),
1287 clean::Inherited => String::new(),
1288 clean::Visibility::Restricted(vis_did) => {
1289 // FIXME(camelid): This may not work correctly if `item_did` is a module.
1290 // However, rustdoc currently never displays a module's
1291 // visibility, so it shouldn't matter.
1292 let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
1294 if vis_did.index == CRATE_DEF_INDEX {
1295 "pub(crate) ".to_owned()
1296 } else if parent_module == Some(vis_did) {
1297 // `pub(in foo)` where `foo` is the parent module
1298 // is the same as no visibility modifier
1300 } else if parent_module
1301 .map(|parent| find_nearest_parent_module(cx.tcx(), parent))
1305 "pub(super) ".to_owned()
1307 let path = cx.tcx().def_path(vis_did);
1308 debug!("path={:?}", path);
1309 // modified from `resolved_path()` to work with `DefPathData`
1310 let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1311 let anchor = anchor(vis_did, last_name, cx).to_string();
1313 let mut s = "pub(in ".to_owned();
1314 for seg in &path.data[..path.data.len() - 1] {
1315 s.push_str(&format!("{}::", seg.data.get_opt_name().unwrap()));
1317 s.push_str(&format!("{}) ", anchor));
1322 display_fn(move |f| f.write_str(&to_print))
1325 /// This function is the same as print_with_space, except that it renders no links.
1326 /// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1328 crate fn to_src_with_space<'a, 'tcx: 'a>(
1332 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1333 let to_print = match self {
1334 clean::Public => "pub ".to_owned(),
1335 clean::Inherited => String::new(),
1336 clean::Visibility::Restricted(vis_did) => {
1337 // FIXME(camelid): This may not work correctly if `item_did` is a module.
1338 // However, rustdoc currently never displays a module's
1339 // visibility, so it shouldn't matter.
1340 let parent_module = find_nearest_parent_module(tcx, item_did);
1342 if vis_did.index == CRATE_DEF_INDEX {
1343 "pub(crate) ".to_owned()
1344 } else if parent_module == Some(vis_did) {
1345 // `pub(in foo)` where `foo` is the parent module
1346 // is the same as no visibility modifier
1348 } else if parent_module
1349 .map(|parent| find_nearest_parent_module(tcx, parent))
1353 "pub(super) ".to_owned()
1355 format!("pub(in {}) ", tcx.def_path_str(vis_did))
1359 display_fn(move |f| f.write_str(&to_print))
1363 crate trait PrintWithSpace {
1364 fn print_with_space(&self) -> &str;
1367 impl PrintWithSpace for hir::Unsafety {
1368 fn print_with_space(&self) -> &str {
1370 hir::Unsafety::Unsafe => "unsafe ",
1371 hir::Unsafety::Normal => "",
1376 impl PrintWithSpace for hir::IsAsync {
1377 fn print_with_space(&self) -> &str {
1379 hir::IsAsync::Async => "async ",
1380 hir::IsAsync::NotAsync => "",
1385 impl PrintWithSpace for hir::Mutability {
1386 fn print_with_space(&self) -> &str {
1388 hir::Mutability::Not => "",
1389 hir::Mutability::Mut => "mut ",
1394 crate fn print_constness_with_space(c: &hir::Constness, s: Option<ConstStability>) -> &'static str {
1396 // const stable or when feature(staged_api) is not set
1398 hir::Constness::Const,
1399 Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
1401 | (hir::Constness::Const, None) => "const ",
1402 // const unstable or not const
1407 impl clean::Import {
1408 crate fn print<'a, 'tcx: 'a>(
1410 cx: &'a Context<'tcx>,
1411 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1412 display_fn(move |f| match self.kind {
1413 clean::ImportKind::Simple(name) => {
1414 if name == self.source.path.last() {
1415 write!(f, "use {};", self.source.print(cx))
1417 write!(f, "use {} as {};", self.source.print(cx), name)
1420 clean::ImportKind::Glob => {
1421 if self.source.path.segments.is_empty() {
1424 write!(f, "use {}::*;", self.source.print(cx))
1431 impl clean::ImportSource {
1432 crate fn print<'a, 'tcx: 'a>(
1434 cx: &'a Context<'tcx>,
1435 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1436 display_fn(move |f| match self.did {
1437 Some(did) => resolved_path(f, did, &self.path, true, false, cx),
1439 for seg in &self.path.segments[..self.path.segments.len() - 1] {
1440 write!(f, "{}::", seg.name)?;
1442 let name = self.path.last();
1443 if let hir::def::Res::PrimTy(p) = self.path.res {
1444 primitive_link(f, PrimitiveType::from(p), name.as_str(), cx)?;
1446 write!(f, "{}", name)?;
1454 impl clean::TypeBinding {
1455 crate fn print<'a, 'tcx: 'a>(
1457 cx: &'a Context<'tcx>,
1458 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1459 display_fn(move |f| {
1460 f.write_str(self.name.as_str())?;
1462 clean::TypeBindingKind::Equality { ref term } => {
1464 write!(f, " = {:#}", term.print(cx))?;
1466 write!(f, " = {}", term.print(cx))?;
1469 clean::TypeBindingKind::Constraint { ref bounds } => {
1470 if !bounds.is_empty() {
1472 write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
1474 write!(f, ": {}", print_generic_bounds(bounds, cx))?;
1484 crate fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
1485 display_fn(move |f| {
1486 let quot = if f.alternate() { "\"" } else { """ };
1488 Abi::Rust => Ok(()),
1489 abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1494 crate fn print_default_space<'a>(v: bool) -> &'a str {
1495 if v { "default " } else { "" }
1498 impl clean::GenericArg {
1499 crate fn print<'a, 'tcx: 'a>(
1501 cx: &'a Context<'tcx>,
1502 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1503 display_fn(move |f| match self {
1504 clean::GenericArg::Lifetime(lt) => fmt::Display::fmt(<.print(), f),
1505 clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(cx), f),
1506 clean::GenericArg::Const(ct) => fmt::Display::fmt(&ct.print(cx.tcx()), f),
1507 clean::GenericArg::Infer => fmt::Display::fmt("_", f),
1512 impl clean::types::Term {
1513 crate fn print<'a, 'tcx: 'a>(
1515 cx: &'a Context<'tcx>,
1516 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1518 clean::types::Term::Type(ty) => ty.print(cx),
1524 crate fn display_fn(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
1525 struct WithFormatter<F>(Cell<Option<F>>);
1527 impl<F> fmt::Display for WithFormatter<F>
1529 F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
1531 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1532 (self.0.take()).unwrap()(f)
1536 WithFormatter(Cell::new(Some(f)))