1use std::borrow::Cow;
11use std::cmp::Ordering;
12use std::fmt::{self, Display, Write};
13use std::iter::{self, once};
14
15use itertools::Either;
16use rustc_abi::ExternAbi;
17use rustc_attr_parsing::{ConstStability, StabilityLevel, StableSince};
18use rustc_data_structures::captures::Captures;
19use rustc_data_structures::fx::FxHashSet;
20use rustc_hir as hir;
21use rustc_hir::def::DefKind;
22use rustc_hir::def_id::{DefId, LOCAL_CRATE};
23use rustc_metadata::creader::{CStore, LoadedMacro};
24use rustc_middle::ty::{self, TyCtxt, TypingMode};
25use rustc_span::symbol::kw;
26use rustc_span::{Symbol, sym};
27use tracing::{debug, trace};
28
29use super::url_parts_builder::{UrlPartsBuilder, estimate_item_path_byte_length};
30use crate::clean::types::ExternalLocation;
31use crate::clean::utils::find_nearest_parent_module;
32use crate::clean::{self, ExternalCrate, PrimitiveType};
33use crate::display::Joined as _;
34use crate::formats::cache::Cache;
35use crate::formats::item_type::ItemType;
36use crate::html::escape::{Escape, EscapeBodyText};
37use crate::html::render::Context;
38use crate::passes::collect_intra_doc_links::UrlFragment;
39
40pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
41 s.write_fmt(f).unwrap();
42}
43
44pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
45 bounds: &'a [clean::GenericBound],
46 cx: &'a Context<'tcx>,
47) -> impl Display + 'a + Captures<'tcx> {
48 fmt::from_fn(move |f| {
49 let mut bounds_dup = FxHashSet::default();
50
51 bounds
52 .iter()
53 .filter(move |b| bounds_dup.insert(*b))
54 .map(|bound| bound.print(cx))
55 .joined(" + ", f)
56 })
57}
58
59impl clean::GenericParamDef {
60 pub(crate) fn print<'a, 'tcx: 'a>(
61 &'a self,
62 cx: &'a Context<'tcx>,
63 ) -> impl Display + 'a + Captures<'tcx> {
64 fmt::from_fn(move |f| match &self.kind {
65 clean::GenericParamDefKind::Lifetime { outlives } => {
66 write!(f, "{}", self.name)?;
67
68 if !outlives.is_empty() {
69 f.write_str(": ")?;
70 outlives.iter().map(|lt| lt.print()).joined(" + ", f)?;
71 }
72
73 Ok(())
74 }
75 clean::GenericParamDefKind::Type { bounds, default, .. } => {
76 f.write_str(self.name.as_str())?;
77
78 if !bounds.is_empty() {
79 f.write_str(": ")?;
80 print_generic_bounds(bounds, cx).fmt(f)?;
81 }
82
83 if let Some(ref ty) = default {
84 f.write_str(" = ")?;
85 ty.print(cx).fmt(f)?;
86 }
87
88 Ok(())
89 }
90 clean::GenericParamDefKind::Const { ty, default, .. } => {
91 write!(f, "const {}: ", self.name)?;
92 ty.print(cx).fmt(f)?;
93
94 if let Some(default) = default {
95 f.write_str(" = ")?;
96 if f.alternate() {
97 write!(f, "{default}")?;
98 } else {
99 write!(f, "{}", Escape(default))?;
100 }
101 }
102
103 Ok(())
104 }
105 })
106 }
107}
108
109impl clean::Generics {
110 pub(crate) fn print<'a, 'tcx: 'a>(
111 &'a self,
112 cx: &'a Context<'tcx>,
113 ) -> impl Display + 'a + Captures<'tcx> {
114 fmt::from_fn(move |f| {
115 let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable();
116 if real_params.peek().is_none() {
117 return Ok(());
118 }
119
120 let real_params =
121 fmt::from_fn(|f| real_params.clone().map(|g| g.print(cx)).joined(", ", f));
122 if f.alternate() {
123 write!(f, "<{:#}>", real_params)
124 } else {
125 write!(f, "<{}>", real_params)
126 }
127 })
128 }
129}
130
131#[derive(Clone, Copy, PartialEq, Eq)]
132pub(crate) enum Ending {
133 Newline,
134 NoNewline,
135}
136
137fn print_where_predicate<'a, 'tcx: 'a>(
138 predicate: &'a clean::WherePredicate,
139 cx: &'a Context<'tcx>,
140) -> impl Display + 'a + Captures<'tcx> {
141 fmt::from_fn(move |f| {
142 match predicate {
143 clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
144 print_higher_ranked_params_with_space(bound_params, cx, "for").fmt(f)?;
145 ty.print(cx).fmt(f)?;
146 f.write_str(":")?;
147 if !bounds.is_empty() {
148 f.write_str(" ")?;
149 print_generic_bounds(bounds, cx).fmt(f)?;
150 }
151 Ok(())
152 }
153 clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
154 write!(f, "{}:", lifetime.print())?;
157 if !bounds.is_empty() {
158 write!(f, " {}", print_generic_bounds(bounds, cx))?;
159 }
160 Ok(())
161 }
162 clean::WherePredicate::EqPredicate { lhs, rhs } => {
163 if f.alternate() {
164 write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
165 } else {
166 write!(f, "{} == {}", lhs.print(cx), rhs.print(cx))
167 }
168 }
169 }
170 })
171}
172
173pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
177 gens: &'a clean::Generics,
178 cx: &'a Context<'tcx>,
179 indent: usize,
180 ending: Ending,
181) -> impl Display + 'a + Captures<'tcx> {
182 fmt::from_fn(move |f| {
183 if gens.where_predicates.is_empty() {
184 return Ok(());
185 }
186
187 let where_preds = fmt::from_fn(|f| {
188 gens.where_predicates
189 .iter()
190 .map(|predicate| {
191 fmt::from_fn(|f| {
192 if f.alternate() {
193 f.write_str(" ")?;
194 } else {
195 f.write_str("\n")?;
196 }
197 print_where_predicate(predicate, cx).fmt(f)
198 })
199 })
200 .joined(",", f)
201 });
202
203 let clause = if f.alternate() {
204 if ending == Ending::Newline {
205 format!(" where{where_preds},")
206 } else {
207 format!(" where{where_preds}")
208 }
209 } else {
210 let mut br_with_padding = String::with_capacity(6 * indent + 28);
211 br_with_padding.push('\n');
212
213 let where_indent = 3;
214 let padding_amount = if ending == Ending::Newline {
215 indent + 4
216 } else if indent == 0 {
217 4
218 } else {
219 indent + where_indent + "where ".len()
220 };
221
222 for _ in 0..padding_amount {
223 br_with_padding.push(' ');
224 }
225 let where_preds = where_preds.to_string().replace('\n', &br_with_padding);
226
227 if ending == Ending::Newline {
228 let mut clause = " ".repeat(indent.saturating_sub(1));
229 write!(clause, "<div class=\"where\">where{where_preds},</div>")?;
230 clause
231 } else {
232 if indent == 0 {
234 format!("\n<span class=\"where\">where{where_preds}</span>")
235 } else {
236 let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
238
239 let mut clause = br_with_padding;
240 clause.truncate(indent + 1 + where_indent);
242
243 write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
244 clause
245 }
246 }
247 };
248 write!(f, "{clause}")
249 })
250}
251
252impl clean::Lifetime {
253 pub(crate) fn print(&self) -> impl Display + '_ {
254 self.0.as_str()
255 }
256}
257
258impl clean::ConstantKind {
259 pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ {
260 let expr = self.expr(tcx);
261 fmt::from_fn(move |f| {
262 if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
263 })
264 }
265}
266
267impl clean::PolyTrait {
268 fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
269 fmt::from_fn(move |f| {
270 print_higher_ranked_params_with_space(&self.generic_params, cx, "for").fmt(f)?;
271 self.trait_.print(cx).fmt(f)
272 })
273 }
274}
275
276impl clean::GenericBound {
277 pub(crate) fn print<'a, 'tcx: 'a>(
278 &'a self,
279 cx: &'a Context<'tcx>,
280 ) -> impl Display + 'a + Captures<'tcx> {
281 fmt::from_fn(move |f| match self {
282 clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
283 clean::GenericBound::TraitBound(ty, modifiers) => {
284 let hir::TraitBoundModifiers { polarity, constness: _ } = modifiers;
286 f.write_str(match polarity {
287 hir::BoundPolarity::Positive => "",
288 hir::BoundPolarity::Maybe(_) => "?",
289 hir::BoundPolarity::Negative(_) => "!",
290 })?;
291 ty.print(cx).fmt(f)
292 }
293 clean::GenericBound::Use(args) => {
294 if f.alternate() {
295 f.write_str("use<")?;
296 } else {
297 f.write_str("use<")?;
298 }
299 args.iter().joined(", ", f)?;
300 if f.alternate() { f.write_str(">") } else { f.write_str(">") }
301 }
302 })
303 }
304}
305
306impl clean::GenericArgs {
307 fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
308 fmt::from_fn(move |f| {
309 match self {
310 clean::GenericArgs::AngleBracketed { args, constraints } => {
311 if !args.is_empty() || !constraints.is_empty() {
312 if f.alternate() {
313 f.write_str("<")?;
314 } else {
315 f.write_str("<")?;
316 }
317
318 [Either::Left(args), Either::Right(constraints)]
319 .into_iter()
320 .flat_map(Either::factor_into_iter)
321 .map(|either| {
322 either.map_either(
323 |arg| arg.print(cx),
324 |constraint| constraint.print(cx),
325 )
326 })
327 .joined(", ", f)?;
328
329 if f.alternate() {
330 f.write_str(">")?;
331 } else {
332 f.write_str(">")?;
333 }
334 }
335 }
336 clean::GenericArgs::Parenthesized { inputs, output } => {
337 f.write_str("(")?;
338 inputs.iter().map(|ty| ty.print(cx)).joined(", ", f)?;
339 f.write_str(")")?;
340 if let Some(ref ty) = *output {
341 if f.alternate() {
342 write!(f, " -> {:#}", ty.print(cx))?;
343 } else {
344 write!(f, " -> {}", ty.print(cx))?;
345 }
346 }
347 }
348 }
349 Ok(())
350 })
351 }
352}
353
354#[derive(PartialEq, Eq)]
356pub(crate) enum HrefError {
357 DocumentationNotBuilt,
376 Private,
378 NotInExternalCache,
380}
381
382pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
384 let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
385 s.push_str(syms[0].as_str());
387 for sym in &syms[1..] {
388 s.push_str("::");
389 s.push_str(sym.as_str());
390 }
391 s
392}
393
394fn generate_macro_def_id_path(
397 def_id: DefId,
398 cx: &Context<'_>,
399 root_path: Option<&str>,
400) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
401 let tcx = cx.tcx();
402 let crate_name = tcx.crate_name(def_id.krate);
403 let cache = cx.cache();
404
405 let fqp = clean::inline::item_relative_path(tcx, def_id);
406 let mut relative = fqp.iter().copied();
407 let cstore = CStore::from_tcx(tcx);
408 if !cstore.has_crate_data(def_id.krate) {
410 debug!("No data for crate {crate_name}");
411 return Err(HrefError::NotInExternalCache);
412 }
413 let is_macro_2 = match cstore.load_macro_untracked(def_id, tcx) {
416 LoadedMacro::MacroDef { def, .. } => !def.macro_rules,
418 _ => false,
419 };
420
421 let mut path = if is_macro_2 {
422 once(crate_name).chain(relative).collect()
423 } else {
424 vec![crate_name, relative.next_back().unwrap()]
425 };
426 if path.len() < 2 {
427 debug!("macro path cannot be empty!");
430 return Err(HrefError::NotInExternalCache);
431 }
432
433 if let Some(last) = path.last_mut() {
434 *last = Symbol::intern(&format!("macro.{last}.html"));
435 }
436
437 let url = match cache.extern_locations[&def_id.krate] {
438 ExternalLocation::Remote(ref s) => {
439 format!("{s}{path}", path = fmt::from_fn(|f| path.iter().joined("/", f)))
441 }
442 ExternalLocation::Local => {
443 format!(
445 "{root_path}{path}",
446 root_path = root_path.unwrap_or(""),
447 path = fmt::from_fn(|f| path.iter().joined("/", f))
448 )
449 }
450 ExternalLocation::Unknown => {
451 debug!("crate {crate_name} not in cache when linkifying macros");
452 return Err(HrefError::NotInExternalCache);
453 }
454 };
455 Ok((url, ItemType::Macro, fqp))
456}
457
458fn generate_item_def_id_path(
459 mut def_id: DefId,
460 original_def_id: DefId,
461 cx: &Context<'_>,
462 root_path: Option<&str>,
463 original_def_kind: DefKind,
464) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
465 use rustc_middle::traits::ObligationCause;
466 use rustc_trait_selection::infer::TyCtxtInferExt;
467 use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
468
469 let tcx = cx.tcx();
470 let crate_name = tcx.crate_name(def_id.krate);
471
472 if def_id != original_def_id && matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
475 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
476 def_id = infcx
477 .at(&ObligationCause::dummy(), tcx.param_env(def_id))
478 .query_normalize(ty::Binder::dummy(tcx.type_of(def_id).instantiate_identity()))
479 .map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
480 .ok()
481 .and_then(|normalized| normalized.skip_binder().ty_adt_def())
482 .map(|adt| adt.did())
483 .unwrap_or(def_id);
484 }
485
486 let relative = clean::inline::item_relative_path(tcx, def_id);
487 let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
488
489 let def_kind = tcx.def_kind(def_id);
490 let shortty = def_kind.into();
491 let module_fqp = to_module_fqp(shortty, &fqp);
492 let mut is_remote = false;
493
494 let url_parts = url_parts(cx.cache(), def_id, module_fqp, &cx.current, &mut is_remote)?;
495 let (url_parts, shortty, fqp) = make_href(root_path, shortty, url_parts, &fqp, is_remote)?;
496 if def_id == original_def_id {
497 return Ok((url_parts, shortty, fqp));
498 }
499 let kind = ItemType::from_def_kind(original_def_kind, Some(def_kind));
500 Ok((format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id)), shortty, fqp))
501}
502
503fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
504 if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
505}
506
507fn url_parts(
508 cache: &Cache,
509 def_id: DefId,
510 module_fqp: &[Symbol],
511 relative_to: &[Symbol],
512 is_remote: &mut bool,
513) -> Result<UrlPartsBuilder, HrefError> {
514 match cache.extern_locations[&def_id.krate] {
515 ExternalLocation::Remote(ref s) => {
516 *is_remote = true;
517 let s = s.trim_end_matches('/');
518 let mut builder = UrlPartsBuilder::singleton(s);
519 builder.extend(module_fqp.iter().copied());
520 Ok(builder)
521 }
522 ExternalLocation::Local => Ok(href_relative_parts(module_fqp, relative_to).collect()),
523 ExternalLocation::Unknown => Err(HrefError::DocumentationNotBuilt),
524 }
525}
526
527fn make_href(
528 root_path: Option<&str>,
529 shortty: ItemType,
530 mut url_parts: UrlPartsBuilder,
531 fqp: &[Symbol],
532 is_remote: bool,
533) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
534 if !is_remote && let Some(root_path) = root_path {
535 let root = root_path.trim_end_matches('/');
536 url_parts.push_front(root);
537 }
538 debug!(?url_parts);
539 match shortty {
540 ItemType::Module => {
541 url_parts.push("index.html");
542 }
543 _ => {
544 let last = fqp.last().unwrap();
545 url_parts.push_fmt(format_args!("{shortty}.{last}.html"));
546 }
547 }
548 Ok((url_parts.finish(), shortty, fqp.to_vec()))
549}
550
551pub(crate) fn href_with_root_path(
552 original_did: DefId,
553 cx: &Context<'_>,
554 root_path: Option<&str>,
555) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
556 let tcx = cx.tcx();
557 let def_kind = tcx.def_kind(original_did);
558 let did = match def_kind {
559 DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
560 tcx.parent(original_did)
562 }
563 DefKind::Ctor(..) => return href_with_root_path(tcx.parent(original_did), cx, root_path),
566 DefKind::ExternCrate => {
567 if let Some(local_did) = original_did.as_local() {
569 tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id()
570 } else {
571 original_did
572 }
573 }
574 _ => original_did,
575 };
576 let cache = cx.cache();
577 let relative_to = &cx.current;
578
579 if !original_did.is_local() {
580 if root_path.is_some() {
583 if tcx.is_doc_hidden(original_did) {
584 return Err(HrefError::Private);
585 }
586 } else if !cache.effective_visibilities.is_directly_public(tcx, did)
587 && !cache.document_private
588 && !cache.primitive_locations.values().any(|&id| id == did)
589 {
590 return Err(HrefError::Private);
591 }
592 }
593
594 let mut is_remote = false;
595 let (fqp, shortty, url_parts) = match cache.paths.get(&did) {
596 Some(&(ref fqp, shortty)) => (fqp, shortty, {
597 let module_fqp = to_module_fqp(shortty, fqp.as_slice());
598 debug!(?fqp, ?shortty, ?module_fqp);
599 href_relative_parts(module_fqp, relative_to).collect()
600 }),
601 None => {
602 let def_id_to_get = if root_path.is_some() { original_did } else { did };
606 if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&def_id_to_get) {
607 let module_fqp = to_module_fqp(shortty, fqp);
608 (fqp, shortty, url_parts(cache, did, module_fqp, relative_to, &mut is_remote)?)
609 } else if matches!(def_kind, DefKind::Macro(_)) {
610 return generate_macro_def_id_path(did, cx, root_path);
611 } else if did.is_local() {
612 return Err(HrefError::Private);
613 } else {
614 return generate_item_def_id_path(did, original_did, cx, root_path, def_kind);
615 }
616 }
617 };
618 make_href(root_path, shortty, url_parts, fqp, is_remote)
619}
620
621pub(crate) fn href(
622 did: DefId,
623 cx: &Context<'_>,
624) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
625 href_with_root_path(did, cx, None)
626}
627
628pub(crate) fn href_relative_parts<'fqp>(
632 fqp: &'fqp [Symbol],
633 relative_to_fqp: &[Symbol],
634) -> Box<dyn Iterator<Item = Symbol> + 'fqp> {
635 for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
636 if f != r {
638 let dissimilar_part_count = relative_to_fqp.len() - i;
639 let fqp_module = &fqp[i..fqp.len()];
640 return Box::new(
641 iter::repeat(sym::dotdot)
642 .take(dissimilar_part_count)
643 .chain(fqp_module.iter().copied()),
644 );
645 }
646 }
647 match relative_to_fqp.len().cmp(&fqp.len()) {
648 Ordering::Less => {
649 Box::new(fqp[relative_to_fqp.len()..fqp.len()].iter().copied())
651 }
652 Ordering::Greater => {
653 let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
655 Box::new(iter::repeat(sym::dotdot).take(dissimilar_part_count))
656 }
657 Ordering::Equal => {
658 Box::new(iter::empty())
660 }
661 }
662}
663
664pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Context<'_>) -> String {
665 let cache = cx.cache();
666 let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
667 else {
668 return String::new();
669 };
670 let mut buf = String::new();
671 let fqp = if *shortty == ItemType::Primitive {
672 &fqp[fqp.len() - 1..]
674 } else {
675 fqp
676 };
677 if let &Some(UrlFragment::Item(id)) = fragment {
678 write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
679 for component in fqp {
680 write_str(&mut buf, format_args!("{component}::"));
681 }
682 write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
683 } else if !fqp.is_empty() {
684 let mut fqp_it = fqp.iter();
685 write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
686 for component in fqp_it {
687 write_str(&mut buf, format_args!("::{component}"));
688 }
689 }
690 buf
691}
692
693fn resolved_path(
695 w: &mut fmt::Formatter<'_>,
696 did: DefId,
697 path: &clean::Path,
698 print_all: bool,
699 use_absolute: bool,
700 cx: &Context<'_>,
701) -> fmt::Result {
702 let last = path.segments.last().unwrap();
703
704 if print_all {
705 for seg in &path.segments[..path.segments.len() - 1] {
706 write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?;
707 }
708 }
709 if w.alternate() {
710 write!(w, "{}{:#}", last.name, last.args.print(cx))?;
711 } else {
712 let path = fmt::from_fn(|f| {
713 if use_absolute {
714 if let Ok((_, _, fqp)) = href(did, cx) {
715 write!(
716 f,
717 "{path}::{anchor}",
718 path = join_with_double_colon(&fqp[..fqp.len() - 1]),
719 anchor = anchor(did, *fqp.last().unwrap(), cx)
720 )
721 } else {
722 write!(f, "{}", last.name)
723 }
724 } else {
725 write!(f, "{}", anchor(did, last.name, cx))
726 }
727 });
728 write!(w, "{path}{args}", args = last.args.print(cx))?;
729 }
730 Ok(())
731}
732
733fn primitive_link(
734 f: &mut fmt::Formatter<'_>,
735 prim: clean::PrimitiveType,
736 name: fmt::Arguments<'_>,
737 cx: &Context<'_>,
738) -> fmt::Result {
739 primitive_link_fragment(f, prim, name, "", cx)
740}
741
742fn primitive_link_fragment(
743 f: &mut fmt::Formatter<'_>,
744 prim: clean::PrimitiveType,
745 name: fmt::Arguments<'_>,
746 fragment: &str,
747 cx: &Context<'_>,
748) -> fmt::Result {
749 let m = &cx.cache();
750 let mut needs_termination = false;
751 if !f.alternate() {
752 match m.primitive_locations.get(&prim) {
753 Some(&def_id) if def_id.is_local() => {
754 let len = cx.current.len();
755 let path = fmt::from_fn(|f| {
756 if len == 0 {
757 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
758 write!(f, "{cname_sym}/")?;
759 } else {
760 for _ in 0..(len - 1) {
761 f.write_str("../")?;
762 }
763 }
764 Ok(())
765 });
766 write!(
767 f,
768 "<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">",
769 prim.as_sym()
770 )?;
771 needs_termination = true;
772 }
773 Some(&def_id) => {
774 let loc = match m.extern_locations[&def_id.krate] {
775 ExternalLocation::Remote(ref s) => {
776 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
777 let builder: UrlPartsBuilder =
778 [s.as_str().trim_end_matches('/'), cname_sym.as_str()]
779 .into_iter()
780 .collect();
781 Some(builder)
782 }
783 ExternalLocation::Local => {
784 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
785 Some(if cx.current.first() == Some(&cname_sym) {
786 iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
787 } else {
788 iter::repeat(sym::dotdot)
789 .take(cx.current.len())
790 .chain(iter::once(cname_sym))
791 .collect()
792 })
793 }
794 ExternalLocation::Unknown => None,
795 };
796 if let Some(mut loc) = loc {
797 loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
798 write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
799 needs_termination = true;
800 }
801 }
802 None => {}
803 }
804 }
805 Display::fmt(&name, f)?;
806 if needs_termination {
807 write!(f, "</a>")?;
808 }
809 Ok(())
810}
811
812fn tybounds<'a, 'tcx: 'a>(
813 bounds: &'a [clean::PolyTrait],
814 lt: &'a Option<clean::Lifetime>,
815 cx: &'a Context<'tcx>,
816) -> impl Display + 'a + Captures<'tcx> {
817 fmt::from_fn(move |f| {
818 bounds.iter().map(|bound| bound.print(cx)).joined(" + ", f)?;
819 if let Some(lt) = lt {
820 write!(f, " + {}", lt.print())?;
823 }
824 Ok(())
825 })
826}
827
828fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
829 params: &'a [clean::GenericParamDef],
830 cx: &'a Context<'tcx>,
831 keyword: &'static str,
832) -> impl Display + 'a + Captures<'tcx> {
833 fmt::from_fn(move |f| {
834 if !params.is_empty() {
835 f.write_str(keyword)?;
836 f.write_str(if f.alternate() { "<" } else { "<" })?;
837 params.iter().map(|lt| lt.print(cx)).joined(", ", f)?;
838 f.write_str(if f.alternate() { "> " } else { "> " })?;
839 }
840 Ok(())
841 })
842}
843
844pub(crate) fn anchor<'a: 'cx, 'cx>(
845 did: DefId,
846 text: Symbol,
847 cx: &'cx Context<'a>,
848) -> impl Display + Captures<'a> + 'cx {
849 fmt::from_fn(move |f| {
850 let parts = href(did, cx);
851 if let Ok((url, short_ty, fqp)) = parts {
852 write!(
853 f,
854 r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
855 path = join_with_double_colon(&fqp),
856 text = EscapeBodyText(text.as_str()),
857 )
858 } else {
859 f.write_str(text.as_str())
860 }
861 })
862}
863
864fn fmt_type(
865 t: &clean::Type,
866 f: &mut fmt::Formatter<'_>,
867 use_absolute: bool,
868 cx: &Context<'_>,
869) -> fmt::Result {
870 trace!("fmt_type(t = {t:?})");
871
872 match *t {
873 clean::Generic(name) => f.write_str(name.as_str()),
874 clean::SelfTy => f.write_str("Self"),
875 clean::Type::Path { ref path } => {
876 let did = path.def_id();
878 resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
879 }
880 clean::DynTrait(ref bounds, ref lt) => {
881 f.write_str("dyn ")?;
882 tybounds(bounds, lt, cx).fmt(f)
883 }
884 clean::Infer => write!(f, "_"),
885 clean::Primitive(clean::PrimitiveType::Never) => {
886 primitive_link(f, PrimitiveType::Never, format_args!("!"), cx)
887 }
888 clean::Primitive(prim) => primitive_link(f, prim, format_args!("{}", prim.as_sym()), cx),
889 clean::BareFunction(ref decl) => {
890 print_higher_ranked_params_with_space(&decl.generic_params, cx, "for").fmt(f)?;
891 decl.safety.print_with_space().fmt(f)?;
892 print_abi_with_space(decl.abi).fmt(f)?;
893 if f.alternate() {
894 f.write_str("fn")?;
895 } else {
896 primitive_link(f, PrimitiveType::Fn, format_args!("fn"), cx)?;
897 }
898 decl.decl.print(cx).fmt(f)
899 }
900 clean::UnsafeBinder(ref binder) => {
901 print_higher_ranked_params_with_space(&binder.generic_params, cx, "unsafe").fmt(f)?;
902 binder.ty.print(cx).fmt(f)
903 }
904 clean::Tuple(ref typs) => match &typs[..] {
905 &[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
906 [one] => {
907 if let clean::Generic(name) = one {
908 primitive_link(f, PrimitiveType::Tuple, format_args!("({name},)"), cx)
909 } else {
910 write!(f, "(")?;
911 one.print(cx).fmt(f)?;
912 write!(f, ",)")
913 }
914 }
915 many => {
916 let generic_names: Vec<Symbol> = many
917 .iter()
918 .filter_map(|t| match t {
919 clean::Generic(name) => Some(*name),
920 _ => None,
921 })
922 .collect();
923 let is_generic = generic_names.len() == many.len();
924 if is_generic {
925 primitive_link(
926 f,
927 PrimitiveType::Tuple,
928 format_args!(
929 "({})",
930 fmt::from_fn(|f| generic_names.iter().joined(", ", f))
931 ),
932 cx,
933 )
934 } else {
935 f.write_str("(")?;
936 many.iter().map(|item| item.print(cx)).joined(", ", f)?;
937 f.write_str(")")
938 }
939 }
940 },
941 clean::Slice(ref t) => match **t {
942 clean::Generic(name) => {
943 primitive_link(f, PrimitiveType::Slice, format_args!("[{name}]"), cx)
944 }
945 _ => {
946 write!(f, "[")?;
947 t.print(cx).fmt(f)?;
948 write!(f, "]")
949 }
950 },
951 clean::Type::Pat(ref t, ref pat) => {
952 fmt::Display::fmt(&t.print(cx), f)?;
953 write!(f, " is {pat}")
954 }
955 clean::Array(ref t, ref n) => match **t {
956 clean::Generic(name) if !f.alternate() => primitive_link(
957 f,
958 PrimitiveType::Array,
959 format_args!("[{name}; {n}]", n = Escape(n)),
960 cx,
961 ),
962 _ => {
963 write!(f, "[")?;
964 t.print(cx).fmt(f)?;
965 if f.alternate() {
966 write!(f, "; {n}")?;
967 } else {
968 write!(f, "; ")?;
969 primitive_link(
970 f,
971 PrimitiveType::Array,
972 format_args!("{n}", n = Escape(n)),
973 cx,
974 )?;
975 }
976 write!(f, "]")
977 }
978 },
979 clean::RawPointer(m, ref t) => {
980 let m = match m {
981 hir::Mutability::Mut => "mut",
982 hir::Mutability::Not => "const",
983 };
984
985 if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
986 let ty = t.print(cx);
987 if f.alternate() {
988 primitive_link(
989 f,
990 clean::PrimitiveType::RawPointer,
991 format_args!("*{m} {ty:#}"),
992 cx,
993 )
994 } else {
995 primitive_link(
996 f,
997 clean::PrimitiveType::RawPointer,
998 format_args!("*{m} {ty}"),
999 cx,
1000 )
1001 }
1002 } else {
1003 primitive_link(f, clean::PrimitiveType::RawPointer, format_args!("*{m} "), cx)?;
1004 t.print(cx).fmt(f)
1005 }
1006 }
1007 clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
1008 let lt = fmt::from_fn(|f| match l {
1009 Some(l) => write!(f, "{} ", l.print()),
1010 _ => Ok(()),
1011 });
1012 let m = mutability.print_with_space();
1013 let amp = if f.alternate() { "&" } else { "&" };
1014
1015 if let clean::Generic(name) = **ty {
1016 return primitive_link(
1017 f,
1018 PrimitiveType::Reference,
1019 format_args!("{amp}{lt}{m}{name}"),
1020 cx,
1021 );
1022 }
1023
1024 write!(f, "{amp}{lt}{m}")?;
1025
1026 let needs_parens = match **ty {
1027 clean::DynTrait(ref bounds, ref trait_lt)
1028 if bounds.len() > 1 || trait_lt.is_some() =>
1029 {
1030 true
1031 }
1032 clean::ImplTrait(ref bounds) if bounds.len() > 1 => true,
1033 _ => false,
1034 };
1035 if needs_parens {
1036 f.write_str("(")?;
1037 }
1038 fmt_type(ty, f, use_absolute, cx)?;
1039 if needs_parens {
1040 f.write_str(")")?;
1041 }
1042 Ok(())
1043 }
1044 clean::ImplTrait(ref bounds) => {
1045 f.write_str("impl ")?;
1046 print_generic_bounds(bounds, cx).fmt(f)
1047 }
1048 clean::QPath(box clean::QPathData {
1049 ref assoc,
1050 ref self_type,
1051 ref trait_,
1052 should_show_cast,
1053 }) => {
1054 if f.alternate() {
1058 if let Some(trait_) = trait_
1059 && should_show_cast
1060 {
1061 write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
1062 } else {
1063 write!(f, "{:#}::", self_type.print(cx))?
1064 }
1065 } else {
1066 if let Some(trait_) = trait_
1067 && should_show_cast
1068 {
1069 write!(f, "<{} as {}>::", self_type.print(cx), trait_.print(cx))?
1070 } else {
1071 write!(f, "{}::", self_type.print(cx))?
1072 }
1073 };
1074 if !f.alternate() {
1085 let parent_href = match trait_ {
1098 Some(trait_) => href(trait_.def_id(), cx).ok(),
1099 None => self_type.def_id(cx.cache()).and_then(|did| href(did, cx).ok()),
1100 };
1101
1102 if let Some((url, _, path)) = parent_href {
1103 write!(
1104 f,
1105 "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
1106 title=\"type {path}::{name}\">{name}</a>",
1107 shortty = ItemType::AssocType,
1108 name = assoc.name,
1109 path = join_with_double_colon(&path),
1110 )
1111 } else {
1112 write!(f, "{}", assoc.name)
1113 }
1114 } else {
1115 write!(f, "{}", assoc.name)
1116 }?;
1117
1118 assoc.args.print(cx).fmt(f)
1119 }
1120 }
1121}
1122
1123impl clean::Type {
1124 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1125 &'a self,
1126 cx: &'a Context<'tcx>,
1127 ) -> impl Display + 'b + Captures<'tcx> {
1128 fmt::from_fn(move |f| fmt_type(self, f, false, cx))
1129 }
1130}
1131
1132impl clean::Path {
1133 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1134 &'a self,
1135 cx: &'a Context<'tcx>,
1136 ) -> impl Display + 'b + Captures<'tcx> {
1137 fmt::from_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1138 }
1139}
1140
1141impl clean::Impl {
1142 pub(crate) fn print<'a, 'tcx: 'a>(
1143 &'a self,
1144 use_absolute: bool,
1145 cx: &'a Context<'tcx>,
1146 ) -> impl Display + 'a + Captures<'tcx> {
1147 fmt::from_fn(move |f| {
1148 f.write_str("impl")?;
1149 self.generics.print(cx).fmt(f)?;
1150 f.write_str(" ")?;
1151
1152 if let Some(ref ty) = self.trait_ {
1153 if self.is_negative_trait_impl() {
1154 write!(f, "!")?;
1155 }
1156 if self.kind.is_fake_variadic()
1157 && let generics = ty.generics()
1158 && let &[inner_type] = generics.as_ref().map_or(&[][..], |v| &v[..])
1159 {
1160 let last = ty.last();
1161 if f.alternate() {
1162 write!(f, "{}<", last)?;
1163 self.print_type(inner_type, f, use_absolute, cx)?;
1164 write!(f, ">")?;
1165 } else {
1166 write!(f, "{}<", anchor(ty.def_id(), last, cx))?;
1167 self.print_type(inner_type, f, use_absolute, cx)?;
1168 write!(f, ">")?;
1169 }
1170 } else {
1171 ty.print(cx).fmt(f)?;
1172 }
1173 write!(f, " for ")?;
1174 }
1175
1176 if let Some(ty) = self.kind.as_blanket_ty() {
1177 fmt_type(ty, f, use_absolute, cx)?;
1178 } else {
1179 self.print_type(&self.for_, f, use_absolute, cx)?;
1180 }
1181
1182 print_where_clause(&self.generics, cx, 0, Ending::Newline).fmt(f)
1183 })
1184 }
1185 fn print_type<'a, 'tcx: 'a>(
1186 &self,
1187 type_: &clean::Type,
1188 f: &mut fmt::Formatter<'_>,
1189 use_absolute: bool,
1190 cx: &'a Context<'tcx>,
1191 ) -> Result<(), fmt::Error> {
1192 if let clean::Type::Tuple(types) = type_
1193 && let [clean::Type::Generic(name)] = &types[..]
1194 && (self.kind.is_fake_variadic() || self.kind.is_auto())
1195 {
1196 primitive_link_fragment(
1199 f,
1200 PrimitiveType::Tuple,
1201 format_args!("({name}₁, {name}₂, …, {name}ₙ)"),
1202 "#trait-implementations-1",
1203 cx,
1204 )?;
1205 } else if let clean::Type::Array(ty, len) = type_
1206 && let clean::Type::Generic(name) = &**ty
1207 && &len[..] == "1"
1208 && (self.kind.is_fake_variadic() || self.kind.is_auto())
1209 {
1210 primitive_link(f, PrimitiveType::Array, format_args!("[{name}; N]"), cx)?;
1211 } else if let clean::BareFunction(bare_fn) = &type_
1212 && let [clean::Argument { type_: clean::Type::Generic(name), .. }] =
1213 &bare_fn.decl.inputs.values[..]
1214 && (self.kind.is_fake_variadic() || self.kind.is_auto())
1215 {
1216 print_higher_ranked_params_with_space(&bare_fn.generic_params, cx, "for").fmt(f)?;
1220 bare_fn.safety.print_with_space().fmt(f)?;
1221 print_abi_with_space(bare_fn.abi).fmt(f)?;
1222 let ellipsis = if bare_fn.decl.c_variadic { ", ..." } else { "" };
1223 primitive_link_fragment(
1224 f,
1225 PrimitiveType::Tuple,
1226 format_args!("fn({name}₁, {name}₂, …, {name}ₙ{ellipsis})"),
1227 "#trait-implementations-1",
1228 cx,
1229 )?;
1230 if !bare_fn.decl.output.is_unit() {
1232 write!(f, " -> ")?;
1233 fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
1234 }
1235 } else if let clean::Type::Path { path } = type_
1236 && let Some(generics) = path.generics()
1237 && generics.len() == 1
1238 && self.kind.is_fake_variadic()
1239 {
1240 let ty = generics[0];
1241 let wrapper = anchor(path.def_id(), path.last(), cx);
1242 if f.alternate() {
1243 write!(f, "{wrapper:#}<")?;
1244 } else {
1245 write!(f, "{wrapper}<")?;
1246 }
1247 self.print_type(ty, f, use_absolute, cx)?;
1248 if f.alternate() {
1249 write!(f, ">")?;
1250 } else {
1251 write!(f, ">")?;
1252 }
1253 } else {
1254 fmt_type(type_, f, use_absolute, cx)?;
1255 }
1256 Ok(())
1257 }
1258}
1259
1260impl clean::Arguments {
1261 pub(crate) fn print<'a, 'tcx: 'a>(
1262 &'a self,
1263 cx: &'a Context<'tcx>,
1264 ) -> impl Display + 'a + Captures<'tcx> {
1265 fmt::from_fn(move |f| {
1266 self.values
1267 .iter()
1268 .map(|input| {
1269 fmt::from_fn(|f| {
1270 if !input.name.is_empty() {
1271 write!(f, "{}: ", input.name)?;
1272 }
1273 input.type_.print(cx).fmt(f)
1274 })
1275 })
1276 .joined(", ", f)
1277 })
1278 }
1279}
1280
1281struct WriteCounter(usize);
1283
1284impl std::fmt::Write for WriteCounter {
1285 fn write_str(&mut self, s: &str) -> fmt::Result {
1286 self.0 += s.len();
1287 Ok(())
1288 }
1289}
1290
1291struct Indent(usize);
1293
1294impl Display for Indent {
1295 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1296 (0..self.0).for_each(|_| {
1297 f.write_char(' ').unwrap();
1298 });
1299 Ok(())
1300 }
1301}
1302
1303impl clean::FnDecl {
1304 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1305 &'a self,
1306 cx: &'a Context<'tcx>,
1307 ) -> impl Display + 'b + Captures<'tcx> {
1308 fmt::from_fn(move |f| {
1309 let ellipsis = if self.c_variadic { ", ..." } else { "" };
1310 if f.alternate() {
1311 write!(
1312 f,
1313 "({args:#}{ellipsis}){arrow:#}",
1314 args = self.inputs.print(cx),
1315 ellipsis = ellipsis,
1316 arrow = self.print_output(cx)
1317 )
1318 } else {
1319 write!(
1320 f,
1321 "({args}{ellipsis}){arrow}",
1322 args = self.inputs.print(cx),
1323 ellipsis = ellipsis,
1324 arrow = self.print_output(cx)
1325 )
1326 }
1327 })
1328 }
1329
1330 pub(crate) fn full_print<'a, 'tcx: 'a>(
1337 &'a self,
1338 header_len: usize,
1339 indent: usize,
1340 cx: &'a Context<'tcx>,
1341 ) -> impl Display + 'a + Captures<'tcx> {
1342 fmt::from_fn(move |f| {
1343 let mut counter = WriteCounter(0);
1345 write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
1346 .unwrap();
1347 let line_wrapping_indent =
1349 if header_len + counter.0 > 80 { Some(indent) } else { None };
1350 self.inner_full_print(line_wrapping_indent, f, cx)
1353 })
1354 }
1355
1356 fn inner_full_print(
1357 &self,
1358 line_wrapping_indent: Option<usize>,
1361 f: &mut fmt::Formatter<'_>,
1362 cx: &Context<'_>,
1363 ) -> fmt::Result {
1364 let amp = if f.alternate() { "&" } else { "&" };
1365
1366 write!(f, "(")?;
1367 if let Some(n) = line_wrapping_indent
1368 && !self.inputs.values.is_empty()
1369 {
1370 write!(f, "\n{}", Indent(n + 4))?;
1371 }
1372
1373 let last_input_index = self.inputs.values.len().checked_sub(1);
1374 for (i, input) in self.inputs.values.iter().enumerate() {
1375 if let Some(selfty) = input.to_receiver() {
1376 match selfty {
1377 clean::SelfTy => {
1378 write!(f, "self")?;
1379 }
1380 clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1381 write!(f, "{amp}")?;
1382 if let Some(lt) = lifetime {
1383 write!(f, "{lt} ", lt = lt.print())?;
1384 }
1385 write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
1386 }
1387 _ => {
1388 write!(f, "self: ")?;
1389 selfty.print(cx).fmt(f)?;
1390 }
1391 }
1392 } else {
1393 if input.is_const {
1394 write!(f, "const ")?;
1395 }
1396 write!(f, "{}: ", input.name)?;
1397 input.type_.print(cx).fmt(f)?;
1398 }
1399 match (line_wrapping_indent, last_input_index) {
1400 (_, None) => (),
1401 (None, Some(last_i)) if i != last_i => write!(f, ", ")?,
1402 (None, Some(_)) => (),
1403 (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?,
1404 (Some(_), Some(_)) => writeln!(f, ",")?,
1405 }
1406 }
1407
1408 if self.c_variadic {
1409 match line_wrapping_indent {
1410 None => write!(f, ", ...")?,
1411 Some(n) => writeln!(f, "{}...", Indent(n + 4))?,
1412 };
1413 }
1414
1415 match line_wrapping_indent {
1416 None => write!(f, ")")?,
1417 Some(n) => write!(f, "{})", Indent(n))?,
1418 };
1419
1420 self.print_output(cx).fmt(f)
1421 }
1422
1423 fn print_output<'a, 'tcx: 'a>(
1424 &'a self,
1425 cx: &'a Context<'tcx>,
1426 ) -> impl Display + 'a + Captures<'tcx> {
1427 fmt::from_fn(move |f| match &self.output {
1428 clean::Tuple(tys) if tys.is_empty() => Ok(()),
1429 ty if f.alternate() => {
1430 write!(f, " -> {:#}", ty.print(cx))
1431 }
1432 ty => write!(f, " -> {}", ty.print(cx)),
1433 })
1434 }
1435}
1436
1437pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
1438 item: &clean::Item,
1439 cx: &'a Context<'tcx>,
1440) -> impl Display + 'a + Captures<'tcx> {
1441 use std::fmt::Write as _;
1442 let vis: Cow<'static, str> = match item.visibility(cx.tcx()) {
1443 None => "".into(),
1444 Some(ty::Visibility::Public) => "pub ".into(),
1445 Some(ty::Visibility::Restricted(vis_did)) => {
1446 let parent_module = find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
1450
1451 if vis_did.is_crate_root() {
1452 "pub(crate) ".into()
1453 } else if parent_module == Some(vis_did) {
1454 "".into()
1457 } else if parent_module.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1458 == Some(vis_did)
1459 {
1460 "pub(super) ".into()
1461 } else {
1462 let path = cx.tcx().def_path(vis_did);
1463 debug!("path={path:?}");
1464 let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1466 let anchor = anchor(vis_did, last_name, cx);
1467
1468 let mut s = "pub(in ".to_owned();
1469 for seg in &path.data[..path.data.len() - 1] {
1470 let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
1471 }
1472 let _ = write!(s, "{anchor}) ");
1473 s.into()
1474 }
1475 }
1476 };
1477
1478 let is_doc_hidden = item.is_doc_hidden();
1479 fmt::from_fn(move |f| {
1480 if is_doc_hidden {
1481 f.write_str("#[doc(hidden)] ")?;
1482 }
1483
1484 f.write_str(&vis)
1485 })
1486}
1487
1488pub(crate) trait PrintWithSpace {
1489 fn print_with_space(&self) -> &str;
1490}
1491
1492impl PrintWithSpace for hir::Safety {
1493 fn print_with_space(&self) -> &str {
1494 self.prefix_str()
1495 }
1496}
1497
1498impl PrintWithSpace for hir::HeaderSafety {
1499 fn print_with_space(&self) -> &str {
1500 match self {
1501 hir::HeaderSafety::SafeTargetFeatures => "",
1502 hir::HeaderSafety::Normal(safety) => safety.print_with_space(),
1503 }
1504 }
1505}
1506
1507impl PrintWithSpace for hir::IsAsync {
1508 fn print_with_space(&self) -> &str {
1509 match self {
1510 hir::IsAsync::Async(_) => "async ",
1511 hir::IsAsync::NotAsync => "",
1512 }
1513 }
1514}
1515
1516impl PrintWithSpace for hir::Mutability {
1517 fn print_with_space(&self) -> &str {
1518 match self {
1519 hir::Mutability::Not => "",
1520 hir::Mutability::Mut => "mut ",
1521 }
1522 }
1523}
1524
1525pub(crate) fn print_constness_with_space(
1526 c: &hir::Constness,
1527 overall_stab: Option<StableSince>,
1528 const_stab: Option<ConstStability>,
1529) -> &'static str {
1530 match c {
1531 hir::Constness::Const => match (overall_stab, const_stab) {
1532 (_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }))
1534 | (_, None)
1536 | (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => {
1538 "const "
1539 }
1540 (Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "",
1542 },
1543 hir::Constness::NotConst => "",
1545 }
1546}
1547
1548impl clean::Import {
1549 pub(crate) fn print<'a, 'tcx: 'a>(
1550 &'a self,
1551 cx: &'a Context<'tcx>,
1552 ) -> impl Display + 'a + Captures<'tcx> {
1553 fmt::from_fn(move |f| match self.kind {
1554 clean::ImportKind::Simple(name) => {
1555 if name == self.source.path.last() {
1556 write!(f, "use {};", self.source.print(cx))
1557 } else {
1558 write!(f, "use {source} as {name};", source = self.source.print(cx))
1559 }
1560 }
1561 clean::ImportKind::Glob => {
1562 if self.source.path.segments.is_empty() {
1563 write!(f, "use *;")
1564 } else {
1565 write!(f, "use {}::*;", self.source.print(cx))
1566 }
1567 }
1568 })
1569 }
1570}
1571
1572impl clean::ImportSource {
1573 pub(crate) fn print<'a, 'tcx: 'a>(
1574 &'a self,
1575 cx: &'a Context<'tcx>,
1576 ) -> impl Display + 'a + Captures<'tcx> {
1577 fmt::from_fn(move |f| match self.did {
1578 Some(did) => resolved_path(f, did, &self.path, true, false, cx),
1579 _ => {
1580 for seg in &self.path.segments[..self.path.segments.len() - 1] {
1581 write!(f, "{}::", seg.name)?;
1582 }
1583 let name = self.path.last();
1584 if let hir::def::Res::PrimTy(p) = self.path.res {
1585 primitive_link(f, PrimitiveType::from(p), format_args!("{name}"), cx)?;
1586 } else {
1587 f.write_str(name.as_str())?;
1588 }
1589 Ok(())
1590 }
1591 })
1592 }
1593}
1594
1595impl clean::AssocItemConstraint {
1596 pub(crate) fn print<'a, 'tcx: 'a>(
1597 &'a self,
1598 cx: &'a Context<'tcx>,
1599 ) -> impl Display + 'a + Captures<'tcx> {
1600 fmt::from_fn(move |f| {
1601 f.write_str(self.assoc.name.as_str())?;
1602 self.assoc.args.print(cx).fmt(f)?;
1603 match self.kind {
1604 clean::AssocItemConstraintKind::Equality { ref term } => {
1605 f.write_str(" = ")?;
1606 term.print(cx).fmt(f)?;
1607 }
1608 clean::AssocItemConstraintKind::Bound { ref bounds } => {
1609 if !bounds.is_empty() {
1610 f.write_str(": ")?;
1611 print_generic_bounds(bounds, cx).fmt(f)?;
1612 }
1613 }
1614 }
1615 Ok(())
1616 })
1617 }
1618}
1619
1620pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display {
1621 fmt::from_fn(move |f| {
1622 let quot = if f.alternate() { "\"" } else { """ };
1623 match abi {
1624 ExternAbi::Rust => Ok(()),
1625 abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1626 }
1627 })
1628}
1629
1630pub(crate) fn print_default_space<'a>(v: bool) -> &'a str {
1631 if v { "default " } else { "" }
1632}
1633
1634impl clean::GenericArg {
1635 pub(crate) fn print<'a, 'tcx: 'a>(
1636 &'a self,
1637 cx: &'a Context<'tcx>,
1638 ) -> impl Display + 'a + Captures<'tcx> {
1639 fmt::from_fn(move |f| match self {
1640 clean::GenericArg::Lifetime(lt) => lt.print().fmt(f),
1641 clean::GenericArg::Type(ty) => ty.print(cx).fmt(f),
1642 clean::GenericArg::Const(ct) => ct.print(cx.tcx()).fmt(f),
1643 clean::GenericArg::Infer => Display::fmt("_", f),
1644 })
1645 }
1646}
1647
1648impl clean::Term {
1649 pub(crate) fn print<'a, 'tcx: 'a>(
1650 &'a self,
1651 cx: &'a Context<'tcx>,
1652 ) -> impl Display + 'a + Captures<'tcx> {
1653 fmt::from_fn(move |f| match self {
1654 clean::Term::Type(ty) => ty.print(cx).fmt(f),
1655 clean::Term::Constant(ct) => ct.print(cx.tcx()).fmt(f),
1656 })
1657 }
1658}