2013-10-03 10:24:40 -07:00
|
|
|
|
//! HTML formatting module
|
|
|
|
|
//!
|
2015-02-05 15:04:07 +03:00
|
|
|
|
//! This module contains a large number of `fmt::Display` implementations for
|
2023-03-09 14:23:56 -08:00
|
|
|
|
//! various types in `rustdoc::clean`.
|
|
|
|
|
//!
|
|
|
|
|
//! These implementations all emit HTML. As an internal implementation detail,
|
|
|
|
|
//! some of them support an alternate format that emits text, but that should
|
|
|
|
|
//! not be used external to this module.
|
2013-10-03 10:24:40 -07:00
|
|
|
|
|
2022-05-24 13:35:54 -04:00
|
|
|
|
use std::borrow::Cow;
|
2019-08-12 14:36:09 -04:00
|
|
|
|
use std::cell::Cell;
|
2023-03-09 14:23:56 -08:00
|
|
|
|
use std::fmt::{self, Write};
|
2022-06-20 22:32:49 +02:00
|
|
|
|
use std::iter::{self, once};
|
2013-09-18 22:18:38 -07:00
|
|
|
|
|
2022-06-20 22:32:49 +02:00
|
|
|
|
use rustc_ast as ast;
|
2021-06-20 08:39:54 +08:00
|
|
|
|
use rustc_attr::{ConstStability, StabilityLevel};
|
2021-03-07 18:09:35 +01:00
|
|
|
|
use rustc_data_structures::captures::Captures;
|
2019-12-24 05:02:53 +01:00
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 02:37:57 +01:00
|
|
|
|
use rustc_hir as hir;
|
2021-11-11 15:05:25 -08:00
|
|
|
|
use rustc_hir::def::DefKind;
|
2021-04-29 21:36:54 +02:00
|
|
|
|
use rustc_hir::def_id::DefId;
|
2022-06-20 22:32:49 +02:00
|
|
|
|
use rustc_metadata::creader::{CStore, LoadedMacro};
|
2021-11-07 08:57:33 -08:00
|
|
|
|
use rustc_middle::ty;
|
2020-12-16 18:10:04 -05:00
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2022-04-25 15:38:43 -07:00
|
|
|
|
use rustc_span::symbol::kw;
|
2021-12-15 06:18:18 +11:00
|
|
|
|
use rustc_span::{sym, Symbol};
|
2018-04-25 19:30:39 +03:00
|
|
|
|
use rustc_target::spec::abi::Abi;
|
2013-09-18 22:18:38 -07:00
|
|
|
|
|
2022-06-14 12:10:34 -07:00
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
2021-12-27 18:57:07 -08:00
|
|
|
|
use crate::clean::{
|
|
|
|
|
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
|
|
|
|
|
PrimitiveType,
|
|
|
|
|
};
|
2020-06-24 08:16:21 -05:00
|
|
|
|
use crate::formats::item_type::ItemType;
|
2020-01-05 23:19:42 +00:00
|
|
|
|
use crate::html::escape::Escape;
|
2021-03-17 11:41:01 -07:00
|
|
|
|
use crate::html::render::Context;
|
2023-02-13 22:57:28 -07:00
|
|
|
|
use crate::passes::collect_intra_doc_links::UrlFragment;
|
2019-02-23 16:40:07 +09:00
|
|
|
|
|
2022-01-10 12:23:58 -08:00
|
|
|
|
use super::url_parts_builder::estimate_item_path_byte_length;
|
2021-12-13 12:42:01 -08:00
|
|
|
|
use super::url_parts_builder::UrlPartsBuilder;
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) trait Print {
|
2019-08-31 11:22:51 -04:00
|
|
|
|
fn print(self, buffer: &mut Buffer);
|
2019-08-27 17:09:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 11:22:51 -04:00
|
|
|
|
impl<F> Print for F
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(&mut Buffer),
|
|
|
|
|
{
|
|
|
|
|
fn print(self, buffer: &mut Buffer) {
|
|
|
|
|
(self)(buffer)
|
2019-08-31 09:07:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Print for String {
|
2019-08-31 11:22:51 -04:00
|
|
|
|
fn print(self, buffer: &mut Buffer) {
|
|
|
|
|
buffer.write_str(&self);
|
2019-08-31 09:07:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 11:22:51 -04:00
|
|
|
|
impl Print for &'_ str {
|
|
|
|
|
fn print(self, buffer: &mut Buffer) {
|
2019-08-31 09:07:29 -04:00
|
|
|
|
buffer.write_str(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 17:09:13 -04:00
|
|
|
|
#[derive(Debug, Clone)]
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) struct Buffer {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
for_html: bool,
|
|
|
|
|
buffer: String,
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 13:09:05 +05:30
|
|
|
|
impl core::fmt::Write for Buffer {
|
2022-01-11 19:28:11 +05:30
|
|
|
|
#[inline]
|
2022-01-11 13:09:05 +05:30
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
|
self.buffer.write_str(s)
|
|
|
|
|
}
|
2022-01-11 19:28:11 +05:30
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
|
|
|
self.buffer.write_char(c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2022-02-03 22:28:19 +01:00
|
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
|
2022-01-11 19:28:11 +05:30
|
|
|
|
self.buffer.write_fmt(args)
|
|
|
|
|
}
|
2022-01-11 13:09:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 17:09:13 -04:00
|
|
|
|
impl Buffer {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn empty_from(v: &Buffer) -> Buffer {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
Buffer { for_html: v.for_html, buffer: String::new() }
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn html() -> Buffer {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
Buffer { for_html: true, buffer: String::new() }
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn new() -> Buffer {
|
2019-12-08 13:56:26 +01:00
|
|
|
|
Buffer { for_html: false, buffer: String::new() }
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn is_empty(&self) -> bool {
|
2020-07-06 12:53:44 -07:00
|
|
|
|
self.buffer.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn into_inner(self) -> String {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
self.buffer
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn push_str(&mut self, s: &str) {
|
2020-07-06 12:53:44 -07:00
|
|
|
|
self.buffer.push_str(s);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn push_buffer(&mut self, other: Buffer) {
|
2021-04-27 15:26:14 +02:00
|
|
|
|
self.buffer.push_str(&other.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 17:09:13 -04:00
|
|
|
|
// Intended for consumption by write! and writeln! (std::fmt) but without
|
|
|
|
|
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
|
|
|
|
|
// import).
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn write_str(&mut self, s: &str) {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
self.buffer.push_str(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Intended for consumption by write! and writeln! (std::fmt) but without
|
|
|
|
|
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
|
|
|
|
|
// import).
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
|
2019-08-27 17:09:13 -04:00
|
|
|
|
self.buffer.write_fmt(v).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn to_display<T: Print>(mut self, t: T) -> String {
|
2019-08-31 09:07:29 -04:00
|
|
|
|
t.print(&mut self);
|
|
|
|
|
self.into_inner()
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn reserve(&mut self, additional: usize) {
|
2021-03-06 23:30:49 -05:00
|
|
|
|
self.buffer.reserve(additional)
|
|
|
|
|
}
|
2022-06-19 16:36:58 +02:00
|
|
|
|
|
|
|
|
|
pub(crate) fn len(&self) -> usize {
|
|
|
|
|
self.buffer.len()
|
|
|
|
|
}
|
2019-08-27 17:09:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 08:56:09 -07:00
|
|
|
|
pub(crate) fn comma_sep<T: fmt::Display>(
|
2022-01-23 00:13:42 +08:00
|
|
|
|
items: impl Iterator<Item = T>,
|
|
|
|
|
space_after_comma: bool,
|
|
|
|
|
) -> impl fmt::Display {
|
2019-08-12 14:36:09 -04:00
|
|
|
|
display_fn(move |f| {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
for (i, item) in items.enumerate() {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
if i != 0 {
|
2022-01-23 00:13:42 +08:00
|
|
|
|
write!(f, ",{}", if space_after_comma { " " } else { "" })?;
|
2016-03-22 22:01:37 -05:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
fmt::Display::fmt(&item, f)?;
|
2015-01-07 14:58:31 -08:00
|
|
|
|
}
|
|
|
|
|
Ok(())
|
2019-08-12 14:36:09 -04:00
|
|
|
|
})
|
2015-01-07 14:58:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
|
2021-01-12 23:36:04 +01:00
|
|
|
|
bounds: &'a [clean::GenericBound],
|
2021-04-16 12:29:35 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
2019-03-09 02:27:03 +01:00
|
|
|
|
let mut bounds_dup = FxHashSet::default();
|
|
|
|
|
|
2021-12-25 19:41:19 -05:00
|
|
|
|
for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(b.clone())).enumerate() {
|
2014-09-25 02:01:42 -07:00
|
|
|
|
if i > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
f.write_str(" + ")?;
|
2014-09-25 02:01:42 -07:00
|
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
|
fmt::Display::fmt(&bound.print(cx), f)?;
|
2014-09-25 02:01:42 -07:00
|
|
|
|
}
|
|
|
|
|
Ok(())
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2014-09-25 02:01:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::GenericParamDef {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2021-09-01 16:20:14 -07:00
|
|
|
|
display_fn(move |f| match &self.kind {
|
|
|
|
|
clean::GenericParamDefKind::Lifetime { outlives } => {
|
|
|
|
|
write!(f, "{}", self.name)?;
|
|
|
|
|
|
|
|
|
|
if !outlives.is_empty() {
|
|
|
|
|
f.write_str(": ")?;
|
|
|
|
|
for (i, lt) in outlives.iter().enumerate() {
|
|
|
|
|
if i != 0 {
|
|
|
|
|
f.write_str(" + ")?;
|
|
|
|
|
}
|
|
|
|
|
write!(f, "{}", lt.print())?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
clean::GenericParamDefKind::Type { bounds, default, .. } => {
|
2021-12-15 14:39:23 +11:00
|
|
|
|
f.write_str(self.name.as_str())?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
|
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
|
if f.alternate() {
|
2021-04-16 12:29:35 -07:00
|
|
|
|
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
write!(f, ": {}", print_generic_bounds(bounds, cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2014-06-21 05:03:33 -07:00
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if let Some(ref ty) = default {
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, " = {:#}", ty.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
} else {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
write!(f, " = {}", ty.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-09-01 16:20:14 -07:00
|
|
|
|
clean::GenericParamDefKind::Const { ty, default, .. } => {
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if f.alternate() {
|
2021-06-03 09:01:25 +01:00
|
|
|
|
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
|
2019-12-22 17:42:04 -05:00
|
|
|
|
} else {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
write!(f, "const {}: {}", self.name, ty.print(cx))?;
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2021-06-03 09:01:25 +01:00
|
|
|
|
|
|
|
|
|
if let Some(default) = default {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(f, " = {:#}", default)?;
|
|
|
|
|
} else {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
write!(f, " = {}", default)?;
|
2021-06-03 09:01:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2019-02-15 22:24:00 +00:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2017-10-16 21:07:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Generics {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
2021-12-25 21:35:05 -05:00
|
|
|
|
let mut real_params =
|
|
|
|
|
self.params.iter().filter(|p| !p.is_synthetic_type_param()).peekable();
|
|
|
|
|
if real_params.peek().is_none() {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2021-12-25 21:35:05 -05:00
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if f.alternate() {
|
2022-01-23 00:13:42 +08:00
|
|
|
|
write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx)), true))
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2022-01-23 00:13:42 +08:00
|
|
|
|
write!(f, "<{}>", comma_sep(real_params.map(|g| g.print(cx)), true))
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
|
|
|
|
})
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 16:16:35 +02:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub(crate) enum Ending {
|
|
|
|
|
Newline,
|
|
|
|
|
NoNewline,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
|
/// * The Generics from which to emit a where-clause.
|
|
|
|
|
/// * The number of spaces to indent each line with.
|
|
|
|
|
/// * Whether the where-clause needs to add a comma and newline after the last bound.
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
gens: &'a clean::Generics,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
2021-03-07 18:09:35 +01:00
|
|
|
|
indent: usize,
|
2022-07-07 16:16:35 +02:00
|
|
|
|
ending: Ending,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2021-03-07 18:09:35 +01:00
|
|
|
|
display_fn(move |f| {
|
2022-01-23 00:13:42 +08:00
|
|
|
|
let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
|
|
|
|
|
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
|
|
|
|
|
}).map(|pred| {
|
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
f.write_str(" ")?;
|
|
|
|
|
} else {
|
2023-02-07 11:23:25 -07:00
|
|
|
|
f.write_str("\n")?;
|
2022-01-23 00:13:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match pred {
|
|
|
|
|
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
let ty_cx = ty.print(cx);
|
|
|
|
|
let generic_bounds = print_generic_bounds(bounds, cx);
|
|
|
|
|
|
|
|
|
|
if bound_params.is_empty() {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(f, "{ty_cx:#}: {generic_bounds:#}")
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{ty_cx}: {generic_bounds}")
|
|
|
|
|
}
|
2022-02-22 10:30:59 +11:00
|
|
|
|
} else {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"for<{:#}> {ty_cx:#}: {generic_bounds:#}",
|
2023-02-22 04:43:15 +00:00
|
|
|
|
comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true)
|
2022-04-20 20:45:30 -04:00
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"for<{}> {ty_cx}: {generic_bounds}",
|
2023-02-22 04:43:15 +00:00
|
|
|
|
comma_sep(bound_params.iter().map(|lt| lt.print(cx)), true)
|
2022-04-20 20:45:30 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
2022-01-23 00:13:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
let mut bounds_display = String::new();
|
|
|
|
|
for bound in bounds.iter().map(|b| b.print(cx)) {
|
|
|
|
|
write!(bounds_display, "{bound} + ")?;
|
|
|
|
|
}
|
|
|
|
|
bounds_display.truncate(bounds_display.len() - " + ".len());
|
|
|
|
|
write!(f, "{}: {bounds_display}", lifetime.print())
|
2022-01-23 00:13:42 +08:00
|
|
|
|
}
|
2022-10-04 14:08:25 +02:00
|
|
|
|
// FIXME(fmease): Render bound params.
|
|
|
|
|
clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => {
|
2022-01-23 00:13:42 +08:00
|
|
|
|
if f.alternate() {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
|
2022-01-23 00:13:42 +08:00
|
|
|
|
} else {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
write!(f, "{} == {}", lhs.print(cx), rhs.print(cx))
|
2022-01-23 00:13:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}).peekable();
|
|
|
|
|
|
|
|
|
|
if where_predicates.peek().is_none() {
|
2021-03-07 18:09:35 +01:00
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2022-01-23 00:13:42 +08:00
|
|
|
|
|
2022-04-20 20:45:30 -04:00
|
|
|
|
let where_preds = comma_sep(where_predicates, false);
|
|
|
|
|
let clause = if f.alternate() {
|
2022-07-07 16:16:35 +02:00
|
|
|
|
if ending == Ending::Newline {
|
2022-08-31 17:05:38 +02:00
|
|
|
|
format!(" where{where_preds},")
|
2021-03-07 18:09:35 +01:00
|
|
|
|
} else {
|
2022-04-20 20:45:30 -04:00
|
|
|
|
format!(" where{where_preds}")
|
2017-03-31 18:04:42 -05:00
|
|
|
|
}
|
2022-04-20 20:45:30 -04:00
|
|
|
|
} else {
|
|
|
|
|
let mut br_with_padding = String::with_capacity(6 * indent + 28);
|
2023-02-07 11:23:25 -07:00
|
|
|
|
br_with_padding.push_str("\n");
|
2022-11-26 01:51:50 +01:00
|
|
|
|
|
2023-04-10 21:02:29 +02:00
|
|
|
|
let padding_amount =
|
2022-11-26 01:51:50 +01:00
|
|
|
|
if ending == Ending::Newline { indent + 4 } else { indent + "fn where ".len() };
|
|
|
|
|
|
2023-04-10 21:02:29 +02:00
|
|
|
|
for _ in 0..padding_amount {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
br_with_padding.push_str(" ");
|
2017-04-06 18:36:14 -05:00
|
|
|
|
}
|
2023-02-15 22:44:51 +01:00
|
|
|
|
let where_preds = where_preds.to_string().replace('\n', &br_with_padding);
|
2017-04-06 18:36:14 -05:00
|
|
|
|
|
2022-07-07 16:16:35 +02:00
|
|
|
|
if ending == Ending::Newline {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
let mut clause = " ".repeat(indent.saturating_sub(1));
|
2022-08-31 17:05:38 +02:00
|
|
|
|
write!(clause, "<span class=\"where fmt-newline\">where{where_preds},</span>")?;
|
2022-04-20 20:45:30 -04:00
|
|
|
|
clause
|
|
|
|
|
} else {
|
2023-02-07 11:23:25 -07:00
|
|
|
|
// insert a newline after a single space but before multiple spaces at the start
|
2022-04-20 20:45:30 -04:00
|
|
|
|
if indent == 0 {
|
2023-02-07 11:23:25 -07:00
|
|
|
|
format!("\n<span class=\"where\">where{where_preds}</span>")
|
2022-04-20 20:45:30 -04:00
|
|
|
|
} else {
|
2022-11-26 01:51:50 +01:00
|
|
|
|
// put the first one on the same line as the 'where' keyword
|
|
|
|
|
let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
|
|
|
|
|
|
2022-04-20 20:45:30 -04:00
|
|
|
|
let mut clause = br_with_padding;
|
2022-11-26 01:51:50 +01:00
|
|
|
|
clause.truncate(clause.len() - "where ".len());
|
|
|
|
|
|
2022-08-31 17:05:38 +02:00
|
|
|
|
write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
|
2022-04-20 20:45:30 -04:00
|
|
|
|
clause
|
|
|
|
|
}
|
2016-10-13 10:17:25 -05:00
|
|
|
|
}
|
2022-04-20 20:45:30 -04:00
|
|
|
|
};
|
|
|
|
|
write!(f, "{clause}")
|
2021-03-07 18:09:35 +01:00
|
|
|
|
})
|
2014-09-25 02:01:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Lifetime {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print(&self) -> impl fmt::Display + '_ {
|
2021-12-03 20:08:11 -08:00
|
|
|
|
self.0.as_str()
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Constant {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl fmt::Display + '_ {
|
2021-03-07 18:09:35 +01:00
|
|
|
|
let expr = self.expr(tcx);
|
|
|
|
|
display_fn(
|
|
|
|
|
move |f| {
|
|
|
|
|
if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
|
|
|
|
|
},
|
|
|
|
|
)
|
2019-03-13 23:38:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::PolyTrait {
|
2021-04-16 11:21:17 -07:00
|
|
|
|
fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
if !self.generic_params.is_empty() {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"for<{:#}> ",
|
2022-01-23 00:13:42 +08:00
|
|
|
|
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
|
2019-09-12 19:59:14 -04:00
|
|
|
|
)?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"for<{}> ",
|
2022-01-23 00:13:42 +08:00
|
|
|
|
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
|
2019-09-12 19:59:14 -04:00
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}", self.trait_.print(cx))
|
2016-09-26 16:02:21 -05:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", self.trait_.print(cx))
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2014-12-16 08:50:52 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::GenericBound {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| match self {
|
|
|
|
|
clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
|
|
|
|
|
clean::GenericBound::TraitBound(ty, modifier) => {
|
|
|
|
|
let modifier_str = match modifier {
|
|
|
|
|
hir::TraitBoundModifier::None => "",
|
|
|
|
|
hir::TraitBoundModifier::Maybe => "?",
|
2022-01-16 13:05:29 +08:00
|
|
|
|
// ~const is experimental; do not display those bounds in rustdoc
|
|
|
|
|
hir::TraitBoundModifier::MaybeConst => "",
|
2019-09-12 19:59:14 -04:00
|
|
|
|
};
|
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}{:#}", modifier_str, ty.print(cx))
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}{}", modifier_str, ty.print(cx))
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::GenericArgs {
|
2021-04-16 11:21:17 -07:00
|
|
|
|
fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
2020-12-31 02:49:44 +01:00
|
|
|
|
match self {
|
|
|
|
|
clean::GenericArgs::AngleBracketed { args, bindings } => {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if !args.is_empty() || !bindings.is_empty() {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
f.write_str("<")?;
|
|
|
|
|
} else {
|
|
|
|
|
f.write_str("<")?;
|
|
|
|
|
}
|
|
|
|
|
let mut comma = false;
|
2022-05-22 15:03:51 -07:00
|
|
|
|
for arg in args.iter() {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if comma {
|
|
|
|
|
f.write_str(", ")?;
|
|
|
|
|
}
|
|
|
|
|
comma = true;
|
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}", arg.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", arg.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-22 15:03:51 -07:00
|
|
|
|
for binding in bindings.iter() {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if comma {
|
|
|
|
|
f.write_str(", ")?;
|
|
|
|
|
}
|
|
|
|
|
comma = true;
|
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}", binding.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", binding.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2014-12-16 12:40:43 -08:00
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if f.alternate() {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
f.write_str(">")?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
} else {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
f.write_str(">")?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2014-12-16 12:40:43 -08:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2020-12-31 02:49:44 +01:00
|
|
|
|
clean::GenericArgs::Parenthesized { inputs, output } => {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
f.write_str("(")?;
|
|
|
|
|
let mut comma = false;
|
2022-05-22 15:03:51 -07:00
|
|
|
|
for ty in inputs.iter() {
|
2015-01-07 16:10:40 -08:00
|
|
|
|
if comma {
|
2016-09-26 16:02:21 -05:00
|
|
|
|
f.write_str(", ")?;
|
2015-01-07 16:10:40 -08:00
|
|
|
|
}
|
|
|
|
|
comma = true;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}", ty.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", ty.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
f.write_str(")")?;
|
|
|
|
|
if let Some(ref ty) = *output {
|
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, " -> {:#}", ty.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, " -> {}", ty.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2014-12-16 12:40:43 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
})
|
2014-12-16 12:40:43 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 11:10:36 +02:00
|
|
|
|
// Possible errors when computing href link source for a `DefId`
|
2021-11-26 21:20:24 +01:00
|
|
|
|
#[derive(PartialEq, Eq)]
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) enum HrefError {
|
2021-07-05 01:33:51 +02:00
|
|
|
|
/// This item is known to rustdoc, but from a crate that does not have documentation generated.
|
|
|
|
|
///
|
|
|
|
|
/// This can only happen for non-local items.
|
2022-03-29 19:30:54 +02:00
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// Crate `a` defines a public trait and crate `b` – the target crate that depends on `a` –
|
|
|
|
|
/// implements it for a local type.
|
|
|
|
|
/// We document `b` but **not** `a` (we only _build_ the latter – with `rustc`):
|
|
|
|
|
///
|
|
|
|
|
/// ```sh
|
|
|
|
|
/// rustc a.rs --crate-type=lib
|
|
|
|
|
/// rustdoc b.rs --crate-type=lib --extern=a=liba.rlib
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Now, the associated items in the trait impl want to link to the corresponding item in the
|
|
|
|
|
/// trait declaration (see `html::render::assoc_href_attr`) but it's not available since their
|
|
|
|
|
/// *documentation (was) not built*.
|
2021-07-05 01:33:51 +02:00
|
|
|
|
DocumentationNotBuilt,
|
|
|
|
|
/// This can only happen for non-local items when `--document-private-items` is not passed.
|
|
|
|
|
Private,
|
2021-06-27 11:10:36 +02:00
|
|
|
|
// Not in external cache, href link should be in same page
|
|
|
|
|
NotInExternalCache,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 06:18:18 +11:00
|
|
|
|
// Panics if `syms` is empty.
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
|
2022-01-10 12:23:58 -08:00
|
|
|
|
let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
|
2022-05-21 14:07:18 -04:00
|
|
|
|
s.push_str(syms[0].as_str());
|
2021-12-15 06:18:18 +11:00
|
|
|
|
for sym in &syms[1..] {
|
|
|
|
|
s.push_str("::");
|
2022-05-21 14:07:18 -04:00
|
|
|
|
s.push_str(sym.as_str());
|
2021-12-15 06:18:18 +11:00
|
|
|
|
}
|
|
|
|
|
s
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 22:32:49 +02:00
|
|
|
|
/// This function is to get the external macro path because they are not in the cache used in
|
|
|
|
|
/// `href_with_root_path`.
|
|
|
|
|
fn generate_macro_def_id_path(
|
|
|
|
|
def_id: DefId,
|
|
|
|
|
cx: &Context<'_>,
|
|
|
|
|
root_path: Option<&str>,
|
2022-06-20 23:31:40 +02:00
|
|
|
|
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
|
2022-06-20 22:32:49 +02:00
|
|
|
|
let tcx = cx.shared.tcx;
|
2023-01-13 17:33:07 +03:00
|
|
|
|
let crate_name = tcx.crate_name(def_id.krate);
|
2022-06-20 22:32:49 +02:00
|
|
|
|
let cache = cx.cache();
|
|
|
|
|
|
|
|
|
|
let fqp: Vec<Symbol> = tcx
|
|
|
|
|
.def_path(def_id)
|
|
|
|
|
.data
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|elem| {
|
|
|
|
|
// extern blocks (and a few others things) have an empty name.
|
|
|
|
|
match elem.data.get_opt_name() {
|
|
|
|
|
Some(s) if !s.is_empty() => Some(s),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2023-01-13 17:33:07 +03:00
|
|
|
|
let mut relative = fqp.iter().copied();
|
2022-06-20 23:31:40 +02:00
|
|
|
|
let cstore = CStore::from_tcx(tcx);
|
|
|
|
|
// We need this to prevent a `panic` when this function is used from intra doc links...
|
|
|
|
|
if !cstore.has_crate_data(def_id.krate) {
|
|
|
|
|
debug!("No data for crate {}", crate_name);
|
|
|
|
|
return Err(HrefError::NotInExternalCache);
|
|
|
|
|
}
|
2022-06-20 22:32:49 +02:00
|
|
|
|
// Check to see if it is a macro 2.0 or built-in macro.
|
|
|
|
|
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
|
2022-06-20 23:31:40 +02:00
|
|
|
|
let is_macro_2 = match cstore.load_macro_untracked(def_id, tcx.sess) {
|
2022-06-20 22:32:49 +02:00
|
|
|
|
LoadedMacro::MacroDef(def, _) => {
|
|
|
|
|
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
|
|
|
|
|
matches!(&def.kind, ast::ItemKind::MacroDef(ast_def) if !ast_def.macro_rules)
|
|
|
|
|
}
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut path = if is_macro_2 {
|
2023-01-13 17:33:07 +03:00
|
|
|
|
once(crate_name).chain(relative).collect()
|
2022-06-20 22:32:49 +02:00
|
|
|
|
} else {
|
2023-01-13 17:33:07 +03:00
|
|
|
|
vec![crate_name, relative.next_back().unwrap()]
|
2022-06-20 22:32:49 +02:00
|
|
|
|
};
|
|
|
|
|
if path.len() < 2 {
|
|
|
|
|
// The minimum we can have is the crate name followed by the macro name. If shorter, then
|
2022-10-14 00:25:34 +08:00
|
|
|
|
// it means that `relative` was empty, which is an error.
|
2022-06-20 23:31:40 +02:00
|
|
|
|
debug!("macro path cannot be empty!");
|
|
|
|
|
return Err(HrefError::NotInExternalCache);
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(last) = path.last_mut() {
|
2023-01-13 17:33:07 +03:00
|
|
|
|
*last = Symbol::intern(&format!("macro.{}.html", last.as_str()));
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let url = match cache.extern_locations[&def_id.krate] {
|
|
|
|
|
ExternalLocation::Remote(ref s) => {
|
|
|
|
|
// `ExternalLocation::Remote` always end with a `/`.
|
2023-01-13 17:33:07 +03:00
|
|
|
|
format!("{}{}", s, path.iter().map(|p| p.as_str()).join("/"))
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
ExternalLocation::Local => {
|
|
|
|
|
// `root_path` always end with a `/`.
|
2023-01-13 17:33:07 +03:00
|
|
|
|
format!(
|
|
|
|
|
"{}{}/{}",
|
|
|
|
|
root_path.unwrap_or(""),
|
|
|
|
|
crate_name,
|
|
|
|
|
path.iter().map(|p| p.as_str()).join("/")
|
|
|
|
|
)
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
ExternalLocation::Unknown => {
|
2022-06-20 23:31:40 +02:00
|
|
|
|
debug!("crate {} not in cache when linkifying macros", crate_name);
|
|
|
|
|
return Err(HrefError::NotInExternalCache);
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
2022-06-20 23:31:40 +02:00
|
|
|
|
Ok((url, ItemType::Macro, fqp))
|
2022-06-20 22:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn href_with_root_path(
|
2021-06-12 22:08:43 +02:00
|
|
|
|
did: DefId,
|
|
|
|
|
cx: &Context<'_>,
|
|
|
|
|
root_path: Option<&str>,
|
2021-12-15 06:18:18 +11:00
|
|
|
|
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
|
2021-11-11 15:05:25 -08:00
|
|
|
|
let tcx = cx.tcx();
|
|
|
|
|
let def_kind = tcx.def_kind(did);
|
|
|
|
|
let did = match def_kind {
|
|
|
|
|
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
|
|
|
|
|
// documented on their parent's page
|
2022-04-25 22:08:45 +03:00
|
|
|
|
tcx.parent(did)
|
2021-11-11 15:05:25 -08:00
|
|
|
|
}
|
|
|
|
|
_ => did,
|
|
|
|
|
};
|
|
|
|
|
let cache = cx.cache();
|
2021-03-17 11:41:01 -07:00
|
|
|
|
let relative_to = &cx.current;
|
2021-12-15 06:18:18 +11:00
|
|
|
|
fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
|
2021-07-17 09:13:08 +02:00
|
|
|
|
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
|
2021-03-17 11:41:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 22:25:36 -04:00
|
|
|
|
if !did.is_local()
|
2022-10-19 22:37:59 +04:00
|
|
|
|
&& !cache.effective_visibilities.is_directly_public(tcx, did)
|
2021-07-10 22:25:36 -04:00
|
|
|
|
&& !cache.document_private
|
|
|
|
|
&& !cache.primitive_locations.values().any(|&id| id == did)
|
|
|
|
|
{
|
2021-07-05 01:33:51 +02:00
|
|
|
|
return Err(HrefError::Private);
|
2016-04-24 14:11:26 +02:00
|
|
|
|
}
|
2021-04-20 17:42:06 +02:00
|
|
|
|
|
2021-06-12 22:08:43 +02:00
|
|
|
|
let mut is_remote = false;
|
2021-03-17 11:41:01 -07:00
|
|
|
|
let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
|
|
|
|
|
Some(&(ref fqp, shortty)) => (fqp, shortty, {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
let module_fqp = to_module_fqp(shortty, fqp.as_slice());
|
2021-07-10 22:25:36 -04:00
|
|
|
|
debug!(?fqp, ?shortty, ?module_fqp);
|
2021-12-15 06:18:18 +11:00
|
|
|
|
href_relative_parts(module_fqp, relative_to).collect()
|
2021-03-17 11:41:01 -07:00
|
|
|
|
}),
|
2017-12-08 17:32:04 -08:00
|
|
|
|
None => {
|
2021-06-27 11:10:36 +02:00
|
|
|
|
if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&did) {
|
|
|
|
|
let module_fqp = to_module_fqp(shortty, fqp);
|
|
|
|
|
(
|
|
|
|
|
fqp,
|
|
|
|
|
shortty,
|
|
|
|
|
match cache.extern_locations[&did.krate] {
|
|
|
|
|
ExternalLocation::Remote(ref s) => {
|
2021-06-12 22:08:43 +02:00
|
|
|
|
is_remote = true;
|
2021-06-27 11:10:36 +02:00
|
|
|
|
let s = s.trim_end_matches('/');
|
2021-12-13 12:42:01 -08:00
|
|
|
|
let mut builder = UrlPartsBuilder::singleton(s);
|
2021-12-15 06:18:18 +11:00
|
|
|
|
builder.extend(module_fqp.iter().copied());
|
2021-12-13 12:42:01 -08:00
|
|
|
|
builder
|
2021-06-27 11:10:36 +02:00
|
|
|
|
}
|
2021-12-15 06:18:18 +11:00
|
|
|
|
ExternalLocation::Local => {
|
|
|
|
|
href_relative_parts(module_fqp, relative_to).collect()
|
|
|
|
|
}
|
2021-07-05 01:33:51 +02:00
|
|
|
|
ExternalLocation::Unknown => return Err(HrefError::DocumentationNotBuilt),
|
2021-06-27 11:10:36 +02:00
|
|
|
|
},
|
|
|
|
|
)
|
2022-06-20 22:32:49 +02:00
|
|
|
|
} else if matches!(def_kind, DefKind::Macro(_)) {
|
2022-06-20 23:31:40 +02:00
|
|
|
|
return generate_macro_def_id_path(did, cx, root_path);
|
2021-06-27 11:10:36 +02:00
|
|
|
|
} else {
|
|
|
|
|
return Err(HrefError::NotInExternalCache);
|
|
|
|
|
}
|
2015-04-06 17:56:35 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
2023-02-15 11:30:14 +01:00
|
|
|
|
if !is_remote && let Some(root_path) = root_path {
|
|
|
|
|
let root = root_path.trim_end_matches('/');
|
|
|
|
|
url_parts.push_front(root);
|
2021-06-12 22:08:43 +02:00
|
|
|
|
}
|
2021-07-10 22:25:36 -04:00
|
|
|
|
debug!(?url_parts);
|
2015-04-06 17:56:35 -07:00
|
|
|
|
match shortty {
|
|
|
|
|
ItemType::Module => {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
url_parts.push("index.html");
|
2015-04-06 17:56:35 -07:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
let prefix = shortty.as_str();
|
|
|
|
|
let last = fqp.last().unwrap();
|
|
|
|
|
url_parts.push_fmt(format_args!("{}.{}.html", prefix, last));
|
2021-03-17 11:41:01 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-13 12:42:01 -08:00
|
|
|
|
Ok((url_parts.finish(), shortty, fqp.to_vec()))
|
2021-03-17 11:41:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn href(
|
|
|
|
|
did: DefId,
|
|
|
|
|
cx: &Context<'_>,
|
|
|
|
|
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
|
2021-06-12 22:08:43 +02:00
|
|
|
|
href_with_root_path(did, cx, None)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 11:41:01 -07:00
|
|
|
|
/// Both paths should only be modules.
|
|
|
|
|
/// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
|
|
|
|
|
/// both need `../iter/trait.Iterator.html` to get at the iterator trait.
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn href_relative_parts<'fqp>(
|
2021-12-15 06:18:18 +11:00
|
|
|
|
fqp: &'fqp [Symbol],
|
|
|
|
|
relative_to_fqp: &[Symbol],
|
|
|
|
|
) -> Box<dyn Iterator<Item = Symbol> + 'fqp> {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
|
|
|
|
|
// e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
|
|
|
|
|
if f != r {
|
|
|
|
|
let dissimilar_part_count = relative_to_fqp.len() - i;
|
2021-12-15 06:18:18 +11:00
|
|
|
|
let fqp_module = &fqp[i..fqp.len()];
|
2022-07-11 00:16:01 +02:00
|
|
|
|
return Box::new(
|
|
|
|
|
iter::repeat(sym::dotdot)
|
|
|
|
|
.take(dissimilar_part_count)
|
|
|
|
|
.chain(fqp_module.iter().copied()),
|
|
|
|
|
);
|
2015-04-06 17:56:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
|
// e.g. linking to std::sync::atomic from std::sync
|
|
|
|
|
if relative_to_fqp.len() < fqp.len() {
|
2022-07-11 00:16:01 +02:00
|
|
|
|
Box::new(fqp[relative_to_fqp.len()..fqp.len()].iter().copied())
|
2021-03-17 11:41:01 -07:00
|
|
|
|
// e.g. linking to std::sync from std::sync::atomic
|
|
|
|
|
} else if fqp.len() < relative_to_fqp.len() {
|
|
|
|
|
let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
|
2022-07-11 00:16:01 +02:00
|
|
|
|
Box::new(iter::repeat(sym::dotdot).take(dissimilar_part_count))
|
2021-03-17 11:41:01 -07:00
|
|
|
|
// linking to the same module
|
|
|
|
|
} else {
|
2022-07-11 00:16:01 +02:00
|
|
|
|
Box::new(iter::empty())
|
2021-03-17 11:41:01 -07:00
|
|
|
|
}
|
2015-04-06 17:56:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 22:57:28 -07:00
|
|
|
|
pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Context<'_>) -> String {
|
|
|
|
|
let cache = cx.cache();
|
|
|
|
|
let Some((fqp, shortty)) = cache.paths.get(&did)
|
|
|
|
|
.or_else(|| cache.external_paths.get(&did))
|
|
|
|
|
else { return String::new() };
|
2023-02-15 14:44:31 -07:00
|
|
|
|
let mut buf = Buffer::new();
|
2023-03-15 11:34:37 -07:00
|
|
|
|
let fqp = if *shortty == ItemType::Primitive {
|
|
|
|
|
// primitives are documented in a crate, but not actually part of it
|
|
|
|
|
&fqp[fqp.len() - 1..]
|
|
|
|
|
} else {
|
|
|
|
|
&fqp
|
|
|
|
|
};
|
2023-02-13 22:57:28 -07:00
|
|
|
|
if let &Some(UrlFragment::Item(id)) = fragment {
|
2023-02-15 14:44:31 -07:00
|
|
|
|
write!(buf, "{} ", cx.tcx().def_descr(id));
|
|
|
|
|
for component in fqp {
|
|
|
|
|
write!(buf, "{component}::");
|
|
|
|
|
}
|
|
|
|
|
write!(buf, "{}", cx.tcx().item_name(id));
|
|
|
|
|
} else if !fqp.is_empty() {
|
|
|
|
|
let mut fqp_it = fqp.into_iter();
|
|
|
|
|
write!(buf, "{shortty} {}", fqp_it.next().unwrap());
|
|
|
|
|
for component in fqp_it {
|
|
|
|
|
write!(buf, "::{component}");
|
|
|
|
|
}
|
2023-02-13 22:57:28 -07:00
|
|
|
|
}
|
2023-02-15 14:44:31 -07:00
|
|
|
|
buf.into_inner()
|
2023-02-13 22:57:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 12:29:58 -08:00
|
|
|
|
/// Used to render a [`clean::Path`].
|
2021-10-01 17:12:39 +02:00
|
|
|
|
fn resolved_path<'cx>(
|
2019-02-23 16:40:07 +09:00
|
|
|
|
w: &mut fmt::Formatter<'_>,
|
|
|
|
|
did: DefId,
|
|
|
|
|
path: &clean::Path,
|
2017-05-31 18:02:35 +01:00
|
|
|
|
print_all: bool,
|
|
|
|
|
use_absolute: bool,
|
2021-03-17 11:41:01 -07:00
|
|
|
|
cx: &'cx Context<'_>,
|
2017-05-31 18:02:35 +01:00
|
|
|
|
) -> fmt::Result {
|
2017-06-11 18:20:48 +01:00
|
|
|
|
let last = path.segments.last().unwrap();
|
2013-09-24 13:56:52 -07:00
|
|
|
|
|
2014-04-28 20:36:08 -07:00
|
|
|
|
if print_all {
|
2017-12-28 17:49:36 +00:00
|
|
|
|
for seg in &path.segments[..path.segments.len() - 1] {
|
2022-04-25 15:38:43 -07:00
|
|
|
|
write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?;
|
2014-04-28 20:36:08 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if w.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(w, "{}{:#}", &last.name, last.args.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
} else {
|
2017-05-31 18:02:35 +01:00
|
|
|
|
let path = if use_absolute {
|
2021-06-27 11:10:36 +02:00
|
|
|
|
if let Ok((_, _, fqp)) = href(did, cx) {
|
2021-01-12 23:36:04 +01:00
|
|
|
|
format!(
|
|
|
|
|
"{}::{}",
|
2021-12-15 06:18:18 +11:00
|
|
|
|
join_with_double_colon(&fqp[..fqp.len() - 1]),
|
|
|
|
|
anchor(did, *fqp.last().unwrap(), cx)
|
2021-01-12 23:36:04 +01:00
|
|
|
|
)
|
2019-08-12 14:36:09 -04:00
|
|
|
|
} else {
|
|
|
|
|
last.name.to_string()
|
2017-05-31 18:02:35 +01:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
} else {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
anchor(did, last.name, cx).to_string()
|
2017-05-31 18:02:35 +01:00
|
|
|
|
};
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(w, "{}{}", path, last.args.print(cx))?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2014-04-28 20:36:08 -07:00
|
|
|
|
Ok(())
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-23 16:40:07 +09:00
|
|
|
|
fn primitive_link(
|
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
2014-09-11 17:07:49 +12:00
|
|
|
|
prim: clean::PrimitiveType,
|
2014-05-28 19:53:37 -07:00
|
|
|
|
name: &str,
|
2021-04-17 22:34:58 -07:00
|
|
|
|
cx: &Context<'_>,
|
2022-06-08 19:26:51 -07:00
|
|
|
|
) -> fmt::Result {
|
|
|
|
|
primitive_link_fragment(f, prim, name, "", cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn primitive_link_fragment(
|
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
|
|
|
|
prim: clean::PrimitiveType,
|
|
|
|
|
name: &str,
|
|
|
|
|
fragment: &str,
|
|
|
|
|
cx: &Context<'_>,
|
2014-05-28 19:53:37 -07:00
|
|
|
|
) -> fmt::Result {
|
2021-04-17 22:34:58 -07:00
|
|
|
|
let m = &cx.cache();
|
2014-05-28 19:53:37 -07:00
|
|
|
|
let mut needs_termination = false;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
if !f.alternate() {
|
|
|
|
|
match m.primitive_locations.get(&prim) {
|
2016-11-29 08:15:16 +02:00
|
|
|
|
Some(&def_id) if def_id.is_local() => {
|
2021-04-17 22:34:58 -07:00
|
|
|
|
let len = cx.current.len();
|
2016-09-26 16:02:21 -05:00
|
|
|
|
let len = if len == 0 { 0 } else { len - 1 };
|
2017-02-28 00:27:19 +01:00
|
|
|
|
write!(
|
|
|
|
|
f,
|
2022-06-08 19:26:51 -07:00
|
|
|
|
"<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
|
2018-04-01 13:48:15 +09:00
|
|
|
|
"../".repeat(len),
|
2021-04-22 22:04:50 -04:00
|
|
|
|
prim.as_sym()
|
2016-07-03 14:38:37 -07:00
|
|
|
|
)?;
|
|
|
|
|
needs_termination = true;
|
2014-05-28 19:53:37 -07:00
|
|
|
|
}
|
2016-11-29 08:15:16 +02:00
|
|
|
|
Some(&def_id) => {
|
|
|
|
|
let loc = match m.extern_locations[&def_id.krate] {
|
2021-04-29 19:14:29 +02:00
|
|
|
|
ExternalLocation::Remote(ref s) => {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
|
|
|
|
|
let builder: UrlPartsBuilder =
|
|
|
|
|
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect();
|
|
|
|
|
Some(builder)
|
2021-04-17 22:34:58 -07:00
|
|
|
|
}
|
2021-04-29 19:14:29 +02:00
|
|
|
|
ExternalLocation::Local => {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
|
|
|
|
|
Some(if cx.current.first() == Some(&cname_sym) {
|
|
|
|
|
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
|
2021-04-17 22:34:58 -07:00
|
|
|
|
} else {
|
2021-12-15 06:18:18 +11:00
|
|
|
|
iter::repeat(sym::dotdot)
|
|
|
|
|
.take(cx.current.len())
|
|
|
|
|
.chain(iter::once(cname_sym))
|
|
|
|
|
.collect()
|
2021-04-17 22:34:58 -07:00
|
|
|
|
})
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2021-04-29 19:14:29 +02:00
|
|
|
|
ExternalLocation::Unknown => None,
|
2016-09-26 16:02:21 -05:00
|
|
|
|
};
|
2021-12-15 06:18:18 +11:00
|
|
|
|
if let Some(mut loc) = loc {
|
|
|
|
|
loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
|
2022-06-08 19:26:51 -07:00
|
|
|
|
write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
needs_termination = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
2014-05-28 19:53:37 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(f, "{}", name)?;
|
2014-05-28 19:53:37 -07:00
|
|
|
|
if needs_termination {
|
2016-03-22 22:01:37 -05:00
|
|
|
|
write!(f, "</a>")?;
|
2014-05-28 19:53:37 -07:00
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 10:24:40 -07:00
|
|
|
|
/// Helper to render type parameters
|
2021-04-16 11:21:17 -07:00
|
|
|
|
fn tybounds<'a, 'tcx: 'a>(
|
2021-10-01 17:12:39 +02:00
|
|
|
|
bounds: &'a [clean::PolyTrait],
|
2021-06-18 21:47:42 +02:00
|
|
|
|
lt: &'a Option<clean::Lifetime>,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2021-06-18 21:47:42 +02:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
for (i, bound) in bounds.iter().enumerate() {
|
|
|
|
|
if i > 0 {
|
2019-08-12 14:36:09 -04:00
|
|
|
|
write!(f, " + ")?;
|
2013-10-02 15:39:32 -07:00
|
|
|
|
}
|
2021-06-18 21:47:42 +02:00
|
|
|
|
|
|
|
|
|
fmt::Display::fmt(&bound.print(cx), f)?;
|
2013-10-02 15:39:32 -07:00
|
|
|
|
}
|
2021-06-18 21:47:42 +02:00
|
|
|
|
|
|
|
|
|
if let Some(lt) = lt {
|
|
|
|
|
write!(f, " + ")?;
|
|
|
|
|
fmt::Display::fmt(<.print(), f)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
2019-08-12 14:36:09 -04:00
|
|
|
|
})
|
2016-04-25 08:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn anchor<'a, 'cx: 'a>(
|
2021-03-17 11:41:01 -07:00
|
|
|
|
did: DefId,
|
2021-12-15 06:18:18 +11:00
|
|
|
|
text: Symbol,
|
2021-03-17 11:41:01 -07:00
|
|
|
|
cx: &'cx Context<'_>,
|
|
|
|
|
) -> impl fmt::Display + 'a {
|
2021-08-03 07:24:31 +02:00
|
|
|
|
let parts = href(did, cx);
|
2019-08-12 14:36:09 -04:00
|
|
|
|
display_fn(move |f| {
|
2021-06-27 11:10:36 +02:00
|
|
|
|
if let Ok((url, short_ty, fqp)) = parts {
|
2019-08-12 11:18:25 -04:00
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
|
2019-08-12 14:36:09 -04:00
|
|
|
|
short_ty,
|
|
|
|
|
url,
|
|
|
|
|
short_ty,
|
2021-12-15 06:18:18 +11:00
|
|
|
|
join_with_double_colon(&fqp),
|
2021-12-15 11:14:21 +11:00
|
|
|
|
text.as_str()
|
2019-08-12 14:36:09 -04:00
|
|
|
|
)
|
2019-08-12 11:18:25 -04:00
|
|
|
|
} else {
|
2019-08-12 14:36:09 -04:00
|
|
|
|
write!(f, "{}", text)
|
2016-04-25 08:24:50 +02:00
|
|
|
|
}
|
2019-08-12 14:36:09 -04:00
|
|
|
|
})
|
2016-04-25 08:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 11:41:01 -07:00
|
|
|
|
fn fmt_type<'cx>(
|
2021-01-12 23:36:04 +01:00
|
|
|
|
t: &clean::Type,
|
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
|
|
|
|
use_absolute: bool,
|
2021-03-17 11:41:01 -07:00
|
|
|
|
cx: &'cx Context<'_>,
|
2021-01-12 23:36:04 +01:00
|
|
|
|
) -> fmt::Result {
|
2021-07-10 23:02:10 -04:00
|
|
|
|
trace!("fmt_type(t = {:?})", t);
|
2020-12-12 11:33:45 -08:00
|
|
|
|
|
2016-12-15 23:13:00 -08:00
|
|
|
|
match *t {
|
2020-12-16 17:21:08 +01:00
|
|
|
|
clean::Generic(name) => write!(f, "{}", name),
|
2021-11-24 12:29:58 -08:00
|
|
|
|
clean::Type::Path { ref path } => {
|
2019-02-28 22:43:53 +00:00
|
|
|
|
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
|
2021-11-11 15:45:45 -08:00
|
|
|
|
let did = path.def_id();
|
2021-08-30 20:53:57 -07:00
|
|
|
|
resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
|
2021-06-18 21:47:42 +02:00
|
|
|
|
}
|
|
|
|
|
clean::DynTrait(ref bounds, ref lt) => {
|
|
|
|
|
f.write_str("dyn ")?;
|
|
|
|
|
fmt::Display::fmt(&tybounds(bounds, lt, cx), f)
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
|
|
|
|
clean::Infer => write!(f, "_"),
|
2021-09-27 14:20:52 +02:00
|
|
|
|
clean::Primitive(clean::PrimitiveType::Never) => {
|
|
|
|
|
primitive_link(f, PrimitiveType::Never, "!", cx)
|
|
|
|
|
}
|
2021-12-15 14:39:23 +11:00
|
|
|
|
clean::Primitive(prim) => primitive_link(f, prim, prim.as_sym().as_str(), cx),
|
2016-12-15 23:13:00 -08:00
|
|
|
|
clean::BareFunction(ref decl) => {
|
|
|
|
|
if f.alternate() {
|
2017-11-28 12:05:53 +01:00
|
|
|
|
write!(
|
|
|
|
|
f,
|
2020-12-12 11:33:45 -08:00
|
|
|
|
"{:#}{}{:#}fn{:#}",
|
2021-03-17 11:41:01 -07:00
|
|
|
|
decl.print_hrtb_with_space(cx),
|
2019-09-13 08:36:00 -04:00
|
|
|
|
decl.unsafety.print_with_space(),
|
|
|
|
|
print_abi_with_space(decl.abi),
|
2021-03-17 11:41:01 -07:00
|
|
|
|
decl.decl.print(cx),
|
2019-12-22 17:42:04 -05:00
|
|
|
|
)
|
2016-12-15 23:13:00 -08:00
|
|
|
|
} else {
|
2019-09-13 08:36:00 -04:00
|
|
|
|
write!(
|
|
|
|
|
f,
|
2020-12-12 11:33:45 -08:00
|
|
|
|
"{}{}{}",
|
2021-03-17 11:41:01 -07:00
|
|
|
|
decl.print_hrtb_with_space(cx),
|
2019-09-13 08:36:00 -04:00
|
|
|
|
decl.unsafety.print_with_space(),
|
|
|
|
|
print_abi_with_space(decl.abi)
|
|
|
|
|
)?;
|
2021-04-17 22:34:58 -07:00
|
|
|
|
primitive_link(f, PrimitiveType::Fn, "fn", cx)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", decl.decl.print(cx))
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clean::Tuple(ref typs) => {
|
|
|
|
|
match &typs[..] {
|
2021-04-17 22:34:58 -07:00
|
|
|
|
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
|
2023-01-15 15:23:30 +01:00
|
|
|
|
[one] => {
|
2022-06-14 12:10:34 -07:00
|
|
|
|
if let clean::Generic(name) = one {
|
|
|
|
|
primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
// Carry `f.alternate()` into this display w/o branching manually.
|
|
|
|
|
fmt::Display::fmt(&one.print(cx), f)?;
|
|
|
|
|
write!(f, ",)")
|
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2017-05-31 18:02:35 +01:00
|
|
|
|
many => {
|
2022-06-14 12:10:34 -07:00
|
|
|
|
let generic_names: Vec<Symbol> = many
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|t| match t {
|
|
|
|
|
clean::Generic(name) => Some(*name),
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let is_generic = generic_names.len() == many.len();
|
|
|
|
|
if is_generic {
|
|
|
|
|
primitive_link(
|
|
|
|
|
f,
|
|
|
|
|
PrimitiveType::Tuple,
|
|
|
|
|
&format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
|
|
|
|
|
cx,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
for (i, item) in many.iter().enumerate() {
|
|
|
|
|
if i != 0 {
|
|
|
|
|
write!(f, ", ")?;
|
|
|
|
|
}
|
|
|
|
|
// Carry `f.alternate()` into this display w/o branching manually.
|
|
|
|
|
fmt::Display::fmt(&item.print(cx), f)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2022-06-14 12:10:34 -07:00
|
|
|
|
write!(f, ")")
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2016-01-22 23:15:47 +05:30
|
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
2022-06-02 14:28:18 -07:00
|
|
|
|
clean::Slice(ref t) => match **t {
|
|
|
|
|
clean::Generic(name) => {
|
|
|
|
|
primitive_link(f, PrimitiveType::Slice, &format!("[{name}]"), cx)
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2022-06-13 12:56:01 -07:00
|
|
|
|
write!(f, "[")?;
|
2022-06-02 14:28:18 -07:00
|
|
|
|
fmt::Display::fmt(&t.print(cx), f)?;
|
2022-06-13 12:56:01 -07:00
|
|
|
|
write!(f, "]")
|
2022-06-02 14:28:18 -07:00
|
|
|
|
}
|
|
|
|
|
},
|
2022-09-30 14:19:19 -07:00
|
|
|
|
clean::Array(ref t, ref n) => match **t {
|
|
|
|
|
clean::Generic(name) if !f.alternate() => primitive_link(
|
|
|
|
|
f,
|
|
|
|
|
PrimitiveType::Array,
|
|
|
|
|
&format!("[{name}; {n}]", n = Escape(n)),
|
|
|
|
|
cx,
|
|
|
|
|
),
|
|
|
|
|
_ => {
|
|
|
|
|
write!(f, "[")?;
|
|
|
|
|
fmt::Display::fmt(&t.print(cx), f)?;
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(f, "; {n}")?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "; ")?;
|
|
|
|
|
primitive_link(f, PrimitiveType::Array, &format!("{n}", n = Escape(n)), cx)?;
|
|
|
|
|
}
|
|
|
|
|
write!(f, "]")
|
2020-01-05 23:19:42 +00:00
|
|
|
|
}
|
2022-09-30 14:19:19 -07:00
|
|
|
|
},
|
2016-12-15 23:13:00 -08:00
|
|
|
|
clean::RawPointer(m, ref t) => {
|
2019-08-12 12:57:48 -04:00
|
|
|
|
let m = match m {
|
2019-12-21 15:47:27 +01:00
|
|
|
|
hir::Mutability::Mut => "mut",
|
|
|
|
|
hir::Mutability::Not => "const",
|
2019-08-12 12:57:48 -04:00
|
|
|
|
};
|
2021-08-22 20:08:48 -07:00
|
|
|
|
|
2021-08-30 20:53:57 -07:00
|
|
|
|
if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
|
2021-08-22 20:08:48 -07:00
|
|
|
|
let text = if f.alternate() {
|
|
|
|
|
format!("*{} {:#}", m, t.print(cx))
|
|
|
|
|
} else {
|
|
|
|
|
format!("*{} {}", m, t.print(cx))
|
|
|
|
|
};
|
|
|
|
|
primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
|
|
|
|
|
} else {
|
|
|
|
|
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
|
|
|
|
|
fmt::Display::fmt(&t.print(cx), f)
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
|
|
|
|
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
let lt = match l {
|
|
|
|
|
Some(l) => format!("{} ", l.print()),
|
|
|
|
|
_ => String::new(),
|
2016-12-15 23:13:00 -08:00
|
|
|
|
};
|
2019-09-13 08:36:00 -04:00
|
|
|
|
let m = mutability.print_with_space();
|
2023-01-13 17:54:16 +03:00
|
|
|
|
let amp = if f.alternate() { "&" } else { "&" };
|
2016-12-15 23:13:00 -08:00
|
|
|
|
match **ty {
|
2021-06-18 21:47:42 +02:00
|
|
|
|
clean::DynTrait(ref bounds, ref trait_lt)
|
|
|
|
|
if bounds.len() > 1 || trait_lt.is_some() =>
|
|
|
|
|
{
|
2017-07-30 14:59:08 -05:00
|
|
|
|
write!(f, "{}{}{}(", amp, lt, m)?;
|
2021-10-01 17:12:39 +02:00
|
|
|
|
fmt_type(ty, f, use_absolute, cx)?;
|
2017-05-31 18:02:35 +01:00
|
|
|
|
write!(f, ")")
|
|
|
|
|
}
|
2023-01-23 17:10:54 -07:00
|
|
|
|
clean::Generic(name) => {
|
|
|
|
|
primitive_link(f, PrimitiveType::Reference, &format!("{amp}{lt}{m}{name}"), cx)
|
2017-07-30 14:59:08 -05:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
_ => {
|
2017-07-30 14:59:08 -05:00
|
|
|
|
write!(f, "{}{}{}", amp, lt, m)?;
|
2021-10-01 17:12:39 +02:00
|
|
|
|
fmt_type(ty, f, use_absolute, cx)
|
2014-12-16 08:50:52 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
|
|
|
|
clean::ImplTrait(ref bounds) => {
|
2019-02-06 11:46:41 -05:00
|
|
|
|
if f.alternate() {
|
2021-04-16 12:29:35 -07:00
|
|
|
|
write!(f, "impl {:#}", print_generic_bounds(bounds, cx))
|
2019-02-06 11:46:41 -05:00
|
|
|
|
} else {
|
2021-04-16 12:29:35 -07:00
|
|
|
|
write!(f, "impl {}", print_generic_bounds(bounds, cx))
|
2019-02-06 11:46:41 -05:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
2022-08-16 12:48:04 -07:00
|
|
|
|
clean::QPath(box clean::QPathData {
|
|
|
|
|
ref assoc,
|
|
|
|
|
ref self_type,
|
|
|
|
|
ref trait_,
|
|
|
|
|
should_show_cast,
|
|
|
|
|
}) => {
|
2016-12-15 23:13:00 -08:00
|
|
|
|
if f.alternate() {
|
2017-05-31 18:02:35 +01:00
|
|
|
|
if should_show_cast {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
|
2017-02-28 00:27:19 +01:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}::", self_type.print(cx))?
|
2017-02-28 00:27:19 +01:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
} else {
|
2017-05-31 18:02:35 +01:00
|
|
|
|
if should_show_cast {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "<{} as {}>::", self_type.print(cx), trait_.print(cx))?
|
2017-02-26 18:33:42 +01:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}::", self_type.print(cx))?
|
2017-04-12 18:14:54 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-08-24 10:15:19 -07:00
|
|
|
|
// It's pretty unsightly to look at `<A as B>::C` in output, and
|
|
|
|
|
// we've got hyperlinking on our side, so try to avoid longer
|
|
|
|
|
// notation as much as possible by making `C` a hyperlink to trait
|
|
|
|
|
// `B` to disambiguate.
|
|
|
|
|
//
|
|
|
|
|
// FIXME: this is still a lossy conversion and there should probably
|
|
|
|
|
// be a better way of representing this in general? Most of
|
|
|
|
|
// the ugliness comes from inlining across crates where
|
|
|
|
|
// everything comes in as a fully resolved QPath (hard to
|
|
|
|
|
// look at).
|
2023-04-04 02:09:23 +02:00
|
|
|
|
if !f.alternate() && let Ok((url, _, path)) = href(trait_.def_id(), cx) {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
|
|
|
|
|
title=\"type {path}::{name}\">{name}</a>",
|
|
|
|
|
shortty = ItemType::AssocType,
|
|
|
|
|
name = assoc.name,
|
|
|
|
|
path = join_with_double_colon(&path),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{}", assoc.name)
|
|
|
|
|
}?;
|
|
|
|
|
|
|
|
|
|
// Carry `f.alternate()` into this display w/o branching manually.
|
|
|
|
|
fmt::Display::fmt(&assoc.args.print(cx), f)
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Type {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'b + Captures<'tcx> {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
display_fn(move |f| fmt_type(self, f, false, cx))
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:01:56 -07:00
|
|
|
|
impl clean::Path {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
|
2021-08-23 21:01:56 -07:00
|
|
|
|
&'a self,
|
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'b + Captures<'tcx> {
|
2021-08-28 20:46:53 -07:00
|
|
|
|
display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
|
2021-08-23 21:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Impl {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
|
|
|
|
use_absolute: bool,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "impl{:#} ", self.generics.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "impl{} ", self.generics.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2016-10-15 09:46:43 -05:00
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if let Some(ref ty) = self.trait_ {
|
2021-11-07 08:52:28 -08:00
|
|
|
|
match self.polarity {
|
2021-11-07 08:57:33 -08:00
|
|
|
|
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => {}
|
|
|
|
|
ty::ImplPolarity::Negative => write!(f, "!")?,
|
2016-05-12 18:23:11 +01:00
|
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
|
fmt::Display::fmt(&ty.print(cx), f)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
write!(f, " for ")?;
|
|
|
|
|
}
|
2016-10-15 09:46:43 -05:00
|
|
|
|
|
2022-06-08 19:26:51 -07:00
|
|
|
|
if let clean::Type::Tuple(types) = &self.for_ &&
|
|
|
|
|
let [clean::Type::Generic(name)] = &types[..] &&
|
2022-06-16 14:14:38 -07:00
|
|
|
|
(self.kind.is_fake_variadic() || self.kind.is_auto())
|
|
|
|
|
{
|
2022-06-11 09:37:40 -07:00
|
|
|
|
// Hardcoded anchor library/core/src/primitive_docs.rs
|
|
|
|
|
// Link should match `# Trait implementations`
|
2022-06-14 12:21:38 -07:00
|
|
|
|
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
|
2022-06-16 14:14:38 -07:00
|
|
|
|
} else if let clean::BareFunction(bare_fn) = &self.for_ &&
|
2022-06-16 09:50:57 -07:00
|
|
|
|
let [clean::Argument { type_: clean::Type::Generic(name), .. }] = &bare_fn.decl.inputs.values[..] &&
|
2022-06-16 14:14:38 -07:00
|
|
|
|
(self.kind.is_fake_variadic() || self.kind.is_auto())
|
|
|
|
|
{
|
2022-06-16 09:50:57 -07:00
|
|
|
|
// Hardcoded anchor library/core/src/primitive_docs.rs
|
|
|
|
|
// Link should match `# Trait implementations`
|
2022-06-16 14:14:38 -07:00
|
|
|
|
|
|
|
|
|
let hrtb = bare_fn.print_hrtb_with_space(cx);
|
|
|
|
|
let unsafety = bare_fn.unsafety.print_with_space();
|
|
|
|
|
let abi = print_abi_with_space(bare_fn.abi);
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"{hrtb:#}{unsafety}{abi:#}",
|
|
|
|
|
)?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"{hrtb}{unsafety}{abi}",
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
let ellipsis = if bare_fn.decl.c_variadic {
|
|
|
|
|
", ..."
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
};
|
|
|
|
|
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), "#trait-implementations-1", cx)?;
|
2022-06-16 09:50:57 -07:00
|
|
|
|
// Write output.
|
|
|
|
|
if let clean::FnRetTy::Return(ty) = &bare_fn.decl.output {
|
|
|
|
|
write!(f, " -> ")?;
|
|
|
|
|
fmt_type(ty, f, use_absolute, cx)?;
|
|
|
|
|
}
|
2022-06-08 19:26:51 -07:00
|
|
|
|
} else if let Some(ty) = self.kind.as_blanket_ty() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
fmt_type(ty, f, use_absolute, cx)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
fmt_type(&self.for_, f, use_absolute, cx)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2016-03-29 01:11:08 +09:00
|
|
|
|
|
2022-07-07 16:16:35 +02:00
|
|
|
|
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, Ending::Newline), f)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
})
|
2015-07-18 02:02:57 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Arguments {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
for (i, input) in self.values.iter().enumerate() {
|
2022-11-02 15:13:54 +01:00
|
|
|
|
write!(f, "{}: ", input.name)?;
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{:#}", input.type_.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "{}", input.type_.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
|
|
|
|
if i + 1 < self.values.len() {
|
|
|
|
|
write!(f, ", ")?;
|
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
})
|
2014-02-13 06:41:34 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 12:10:59 +09:00
|
|
|
|
impl clean::FnRetTy {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| match self {
|
|
|
|
|
clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
|
2021-03-17 11:41:01 -07:00
|
|
|
|
clean::Return(ty) if f.alternate() => {
|
|
|
|
|
write!(f, " -> {:#}", ty.print(cx))
|
|
|
|
|
}
|
|
|
|
|
clean::Return(ty) => write!(f, " -> {}", ty.print(cx)),
|
2019-09-12 19:59:14 -04:00
|
|
|
|
clean::DefaultReturn => Ok(()),
|
|
|
|
|
})
|
2014-11-09 16:14:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::BareFunctionDecl {
|
2021-04-16 11:21:17 -07:00
|
|
|
|
fn print_hrtb_with_space<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2020-12-12 11:33:45 -08:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
if !self.generic_params.is_empty() {
|
2021-10-08 20:11:02 +02:00
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"for<{}> ",
|
2022-01-23 00:13:42 +08:00
|
|
|
|
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
|
2021-10-08 20:11:02 +02:00
|
|
|
|
)
|
2020-12-12 11:33:45 -08:00
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
})
|
2013-11-28 02:23:12 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 14:23:56 -08:00
|
|
|
|
// Implements Write but only counts the bytes "written".
|
|
|
|
|
struct WriteCounter(usize);
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Write for WriteCounter {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
|
self.0 += s.len();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implements Display by emitting the given number of spaces.
|
|
|
|
|
struct Indent(usize);
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Indent {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
(0..self.0).for_each(|_| {
|
|
|
|
|
f.write_char(' ').unwrap();
|
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::FnDecl {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'b + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
2019-08-10 14:38:17 +03:00
|
|
|
|
let ellipsis = if self.c_variadic { ", ..." } else { "" };
|
2019-09-12 19:59:14 -04:00
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
2019-08-10 14:38:17 +03:00
|
|
|
|
"({args:#}{ellipsis}){arrow:#}",
|
2021-03-17 11:41:01 -07:00
|
|
|
|
args = self.inputs.print(cx),
|
2019-08-10 14:38:17 +03:00
|
|
|
|
ellipsis = ellipsis,
|
2021-03-17 11:41:01 -07:00
|
|
|
|
arrow = self.output.print(cx)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
)
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
2019-08-10 14:38:17 +03:00
|
|
|
|
"({args}{ellipsis}){arrow}",
|
2021-03-17 11:41:01 -07:00
|
|
|
|
args = self.inputs.print(cx),
|
2019-08-10 14:38:17 +03:00
|
|
|
|
ellipsis = ellipsis,
|
2021-03-17 11:41:01 -07:00
|
|
|
|
arrow = self.output.print(cx)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
)
|
2017-03-28 16:49:05 -05:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
|
/// * `header_len`: The length of the function header and name. In other words, the number of
|
|
|
|
|
/// characters in the function declaration up to but not including the parentheses.
|
2023-02-07 11:23:25 -07:00
|
|
|
|
/// This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines
|
|
|
|
|
/// are preserved.
|
2021-03-07 18:09:35 +01:00
|
|
|
|
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
|
|
|
|
|
/// necessary.
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn full_print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
|
|
|
|
header_len: usize,
|
|
|
|
|
indent: usize,
|
2021-04-16 12:29:35 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
display_fn(move |f| {
|
|
|
|
|
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
|
|
|
|
|
let mut counter = WriteCounter(0);
|
|
|
|
|
write!(&mut counter, "{:#}", display_fn(|f| { self.inner_full_print(None, f, cx) }))
|
|
|
|
|
.unwrap();
|
|
|
|
|
// If the text form was over 80 characters wide, we will line-wrap our output.
|
|
|
|
|
let line_wrapping_indent =
|
|
|
|
|
if header_len + counter.0 > 80 { Some(indent) } else { None };
|
|
|
|
|
// Generate the final output. This happens to accept `{:#}` formatting to get textual
|
|
|
|
|
// output but in practice it is only formatted with `{}` to get HTML output.
|
|
|
|
|
self.inner_full_print(line_wrapping_indent, f, cx)
|
|
|
|
|
})
|
2021-03-07 18:09:35 +01:00
|
|
|
|
}
|
2017-03-28 16:49:05 -05:00
|
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
|
fn inner_full_print(
|
|
|
|
|
&self,
|
2023-03-09 14:23:56 -08:00
|
|
|
|
// For None, the declaration will not be line-wrapped. For Some(n),
|
|
|
|
|
// the declaration will be line-wrapped, with an indent of n spaces.
|
|
|
|
|
line_wrapping_indent: Option<usize>,
|
2021-03-07 18:09:35 +01:00
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
2021-04-16 12:29:35 -07:00
|
|
|
|
cx: &Context<'_>,
|
2021-03-07 18:09:35 +01:00
|
|
|
|
) -> fmt::Result {
|
|
|
|
|
let amp = if f.alternate() { "&" } else { "&" };
|
2023-03-09 14:23:56 -08:00
|
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
if let Some(n) = line_wrapping_indent {
|
|
|
|
|
write!(f, "\n{}", Indent(n + 4))?;
|
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
for (i, input) in self.inputs.values.iter().enumerate() {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
if i > 0 {
|
|
|
|
|
match line_wrapping_indent {
|
|
|
|
|
None => write!(f, ", ")?,
|
|
|
|
|
Some(n) => write!(f, ",\n{}", Indent(n + 4))?,
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
if let Some(selfty) = input.to_self() {
|
|
|
|
|
match selfty {
|
|
|
|
|
clean::SelfValue => {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "self")?;
|
2016-09-26 16:02:21 -05:00
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
clean::SelfBorrowed(Some(ref lt), mtbl) => {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "{}{} {}self", amp, lt.print(), mtbl.print_with_space())?;
|
2016-05-08 21:19:29 +03:00
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
clean::SelfBorrowed(None, mtbl) => {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "{}{}self", amp, mtbl.print_with_space())?;
|
2016-05-08 21:19:29 +03:00
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
clean::SelfExplicit(ref typ) => {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "self: ")?;
|
|
|
|
|
fmt::Display::fmt(&typ.print(cx), f)?;
|
2016-05-08 21:19:29 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
} else {
|
2021-10-16 17:52:06 +02:00
|
|
|
|
if input.is_const {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "const ")?;
|
2021-03-07 18:09:35 +01:00
|
|
|
|
}
|
2023-03-09 14:23:56 -08:00
|
|
|
|
write!(f, "{}: ", input.name)?;
|
|
|
|
|
fmt::Display::fmt(&input.type_.print(cx), f)?;
|
2019-08-10 14:38:17 +03:00
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
|
}
|
2016-09-26 18:47:09 -05:00
|
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
|
if self.c_variadic {
|
2023-03-09 14:23:56 -08:00
|
|
|
|
match line_wrapping_indent {
|
|
|
|
|
None => write!(f, ", ...")?,
|
|
|
|
|
Some(n) => write!(f, "\n{}...", Indent(n + 4))?,
|
|
|
|
|
};
|
2021-03-07 18:09:35 +01:00
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
|
2023-03-09 14:23:56 -08:00
|
|
|
|
match line_wrapping_indent {
|
|
|
|
|
None => write!(f, ")")?,
|
|
|
|
|
Some(n) => write!(f, "\n{})", Indent(n))?,
|
2021-03-07 18:09:35 +01:00
|
|
|
|
};
|
|
|
|
|
|
2023-03-09 14:23:56 -08:00
|
|
|
|
fmt::Display::fmt(&self.output.print(cx), f)?;
|
|
|
|
|
Ok(())
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 18:15:24 +01:00
|
|
|
|
pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
|
|
|
|
|
visibility: Option<ty::Visibility<DefId>>,
|
|
|
|
|
item_did: ItemId,
|
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
|
|
|
|
use std::fmt::Write as _;
|
|
|
|
|
|
|
|
|
|
let to_print: Cow<'static, str> = match visibility {
|
|
|
|
|
None => "".into(),
|
|
|
|
|
Some(ty::Visibility::Public) => "pub ".into(),
|
|
|
|
|
Some(ty::Visibility::Restricted(vis_did)) => {
|
|
|
|
|
// FIXME(camelid): This may not work correctly if `item_did` is a module.
|
|
|
|
|
// However, rustdoc currently never displays a module's
|
|
|
|
|
// visibility, so it shouldn't matter.
|
|
|
|
|
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
|
|
|
|
|
|
|
|
|
|
if vis_did.is_crate_root() {
|
|
|
|
|
"pub(crate) ".into()
|
|
|
|
|
} else if parent_module == Some(vis_did) {
|
|
|
|
|
// `pub(in foo)` where `foo` is the parent module
|
|
|
|
|
// is the same as no visibility modifier
|
|
|
|
|
"".into()
|
|
|
|
|
} else if parent_module.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
|
|
|
|
|
== Some(vis_did)
|
|
|
|
|
{
|
|
|
|
|
"pub(super) ".into()
|
|
|
|
|
} else {
|
|
|
|
|
let path = cx.tcx().def_path(vis_did);
|
|
|
|
|
debug!("path={:?}", path);
|
|
|
|
|
// modified from `resolved_path()` to work with `DefPathData`
|
|
|
|
|
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
|
2023-03-16 19:06:34 +03:00
|
|
|
|
let anchor = anchor(vis_did, last_name, cx);
|
2022-11-03 18:15:24 +01:00
|
|
|
|
|
|
|
|
|
let mut s = "pub(in ".to_owned();
|
|
|
|
|
for seg in &path.data[..path.data.len() - 1] {
|
|
|
|
|
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
|
2020-11-14 01:51:05 -05:00
|
|
|
|
}
|
2022-11-03 18:15:24 +01:00
|
|
|
|
let _ = write!(s, "{}) ", anchor);
|
|
|
|
|
s.into()
|
2018-05-12 18:25:09 +01:00
|
|
|
|
}
|
2022-11-03 18:15:24 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
display_fn(move |f| write!(f, "{}", to_print))
|
|
|
|
|
}
|
2021-04-10 14:22:06 -07:00
|
|
|
|
|
2022-11-03 18:15:24 +01:00
|
|
|
|
/// This function is the same as print_with_space, except that it renders no links.
|
|
|
|
|
/// It's used for macros' rendered source view, which is syntax highlighted and cannot have
|
|
|
|
|
/// any HTML in it.
|
|
|
|
|
pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
|
|
|
|
|
visibility: Option<ty::Visibility<DefId>>,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
item_did: DefId,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2023-03-17 15:29:47 -07:00
|
|
|
|
let to_print: Cow<'static, str> = match visibility {
|
|
|
|
|
None => "".into(),
|
|
|
|
|
Some(ty::Visibility::Public) => "pub ".into(),
|
2022-11-03 18:15:24 +01:00
|
|
|
|
Some(ty::Visibility::Restricted(vis_did)) => {
|
|
|
|
|
// FIXME(camelid): This may not work correctly if `item_did` is a module.
|
|
|
|
|
// However, rustdoc currently never displays a module's
|
|
|
|
|
// visibility, so it shouldn't matter.
|
|
|
|
|
let parent_module = find_nearest_parent_module(tcx, item_did);
|
|
|
|
|
|
|
|
|
|
if vis_did.is_crate_root() {
|
2023-03-17 15:29:47 -07:00
|
|
|
|
"pub(crate) ".into()
|
2022-11-03 18:15:24 +01:00
|
|
|
|
} else if parent_module == Some(vis_did) {
|
|
|
|
|
// `pub(in foo)` where `foo` is the parent module
|
|
|
|
|
// is the same as no visibility modifier
|
2023-03-17 15:29:47 -07:00
|
|
|
|
"".into()
|
2022-11-03 18:15:24 +01:00
|
|
|
|
} else if parent_module.and_then(|parent| find_nearest_parent_module(tcx, parent))
|
|
|
|
|
== Some(vis_did)
|
|
|
|
|
{
|
2023-03-17 15:29:47 -07:00
|
|
|
|
"pub(super) ".into()
|
2022-11-03 18:15:24 +01:00
|
|
|
|
} else {
|
2023-03-17 15:29:47 -07:00
|
|
|
|
format!("pub(in {}) ", tcx.def_path_str(vis_did)).into()
|
2021-04-10 14:22:06 -07:00
|
|
|
|
}
|
2022-11-03 18:15:24 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
display_fn(move |f| f.write_str(&to_print))
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
2013-09-23 20:38:17 -07:00
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) trait PrintWithSpace {
|
2019-09-13 08:36:00 -04:00
|
|
|
|
fn print_with_space(&self) -> &str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrintWithSpace for hir::Unsafety {
|
|
|
|
|
fn print_with_space(&self) -> &str {
|
|
|
|
|
match self {
|
|
|
|
|
hir::Unsafety::Unsafe => "unsafe ",
|
|
|
|
|
hir::Unsafety::Normal => "",
|
2013-09-23 20:38:17 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 13:56:52 -07:00
|
|
|
|
|
2019-09-13 08:36:00 -04:00
|
|
|
|
impl PrintWithSpace for hir::IsAsync {
|
|
|
|
|
fn print_with_space(&self) -> &str {
|
|
|
|
|
match self {
|
|
|
|
|
hir::IsAsync::Async => "async ",
|
|
|
|
|
hir::IsAsync::NotAsync => "",
|
2018-05-17 14:47:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 15:47:27 +01:00
|
|
|
|
impl PrintWithSpace for hir::Mutability {
|
|
|
|
|
fn print_with_space(&self) -> &str {
|
|
|
|
|
match self {
|
|
|
|
|
hir::Mutability::Not => "",
|
|
|
|
|
hir::Mutability::Mut => "mut ",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print_constness_with_space(
|
|
|
|
|
c: &hir::Constness,
|
|
|
|
|
s: Option<ConstStability>,
|
|
|
|
|
) -> &'static str {
|
2021-06-20 08:39:54 +08:00
|
|
|
|
match (c, s) {
|
2021-06-21 20:21:37 +08:00
|
|
|
|
// const stable or when feature(staged_api) is not set
|
2021-06-20 08:39:54 +08:00
|
|
|
|
(
|
|
|
|
|
hir::Constness::Const,
|
|
|
|
|
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
|
|
|
|
|
)
|
|
|
|
|
| (hir::Constness::Const, None) => "const ",
|
|
|
|
|
// const unstable or not const
|
2021-06-21 20:15:27 +08:00
|
|
|
|
_ => "",
|
2021-06-20 08:39:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::Import {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2020-09-29 17:04:40 +02:00
|
|
|
|
display_fn(move |f| match self.kind {
|
2020-12-17 14:02:09 +01:00
|
|
|
|
clean::ImportKind::Simple(name) => {
|
|
|
|
|
if name == self.source.path.last() {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "use {};", self.source.print(cx))
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "use {} as {};", self.source.print(cx), name)
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2020-09-29 17:04:40 +02:00
|
|
|
|
clean::ImportKind::Glob => {
|
|
|
|
|
if self.source.path.segments.is_empty() {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
write!(f, "use *;")
|
|
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
write!(f, "use {}::*;", self.source.print(cx))
|
2017-06-24 18:16:39 +01:00
|
|
|
|
}
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::ImportSource {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| match self.did {
|
2021-03-17 11:41:01 -07:00
|
|
|
|
Some(did) => resolved_path(f, did, &self.path, true, false, cx),
|
2019-09-12 19:59:14 -04:00
|
|
|
|
_ => {
|
2020-01-26 21:28:09 +00:00
|
|
|
|
for seg in &self.path.segments[..self.path.segments.len() - 1] {
|
|
|
|
|
write!(f, "{}::", seg.name)?;
|
|
|
|
|
}
|
2021-12-15 08:32:21 +11:00
|
|
|
|
let name = self.path.last();
|
2020-01-26 21:28:09 +00:00
|
|
|
|
if let hir::def::Res::PrimTy(p) = self.path.res {
|
2021-12-15 08:32:21 +11:00
|
|
|
|
primitive_link(f, PrimitiveType::from(p), name.as_str(), cx)?;
|
2020-01-26 21:28:09 +00:00
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{}", name)?;
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
Ok(())
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
2013-09-24 13:56:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::TypeBinding {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| {
|
2022-02-14 19:01:56 -08:00
|
|
|
|
f.write_str(self.assoc.name.as_str())?;
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
write!(f, "{:#}", self.assoc.args.print(cx))?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{}", self.assoc.args.print(cx))?;
|
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
match self.kind {
|
2022-01-10 23:39:21 +00:00
|
|
|
|
clean::TypeBindingKind::Equality { ref term } => {
|
2019-05-08 15:57:06 -04:00
|
|
|
|
if f.alternate() {
|
2022-01-10 23:39:21 +00:00
|
|
|
|
write!(f, " = {:#}", term.print(cx))?;
|
2019-05-08 15:57:06 -04:00
|
|
|
|
} else {
|
2022-01-10 23:39:21 +00:00
|
|
|
|
write!(f, " = {}", term.print(cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clean::TypeBindingKind::Constraint { ref bounds } => {
|
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
|
if f.alternate() {
|
2021-04-16 12:29:35 -07:00
|
|
|
|
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
} else {
|
2023-02-02 15:10:41 -07:00
|
|
|
|
write!(f, ": {}", print_generic_bounds(bounds, cx))?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
}
|
2019-05-08 15:57:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-12 19:59:14 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
})
|
2015-01-07 16:10:40 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
|
2019-09-13 08:36:00 -04:00
|
|
|
|
display_fn(move |f| {
|
2016-09-26 16:02:21 -05:00
|
|
|
|
let quot = if f.alternate() { "\"" } else { """ };
|
2019-09-13 08:36:00 -04:00
|
|
|
|
match abi {
|
2015-04-07 14:22:55 -07:00
|
|
|
|
Abi::Rust => Ok(()),
|
2016-09-26 16:02:21 -05:00
|
|
|
|
abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
|
2015-04-07 14:22:55 -07:00
|
|
|
|
}
|
2019-09-13 08:36:00 -04:00
|
|
|
|
})
|
2015-04-07 14:22:55 -07:00
|
|
|
|
}
|
2019-03-05 02:29:21 +01:00
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print_default_space<'a>(v: bool) -> &'a str {
|
2019-09-13 08:36:00 -04:00
|
|
|
|
if v { "default " } else { "" }
|
2019-03-05 02:29:21 +01:00
|
|
|
|
}
|
2019-08-12 14:36:09 -04:00
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
|
impl clean::GenericArg {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
|
&'a self,
|
2021-04-16 11:21:17 -07:00
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-09-12 19:59:14 -04:00
|
|
|
|
display_fn(move |f| match self {
|
|
|
|
|
clean::GenericArg::Lifetime(lt) => fmt::Display::fmt(<.print(), f),
|
2021-03-17 11:41:01 -07:00
|
|
|
|
clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(cx), f),
|
|
|
|
|
clean::GenericArg::Const(ct) => fmt::Display::fmt(&ct.print(cx.tcx()), f),
|
2021-04-24 21:41:57 +00:00
|
|
|
|
clean::GenericArg::Infer => fmt::Display::fmt("_", f),
|
2019-09-12 19:59:14 -04:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 23:39:21 +00:00
|
|
|
|
impl clean::types::Term {
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn print<'a, 'tcx: 'a>(
|
2022-01-10 23:39:21 +00:00
|
|
|
|
&'a self,
|
|
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2022-12-21 23:54:29 +00:00
|
|
|
|
display_fn(move |f| match self {
|
|
|
|
|
clean::types::Term::Type(ty) => fmt::Display::fmt(&ty.print(cx), f),
|
|
|
|
|
clean::types::Term::Constant(ct) => fmt::Display::fmt(&ct.print(cx.tcx()), f),
|
|
|
|
|
})
|
2022-01-10 23:39:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
|
pub(crate) fn display_fn(
|
|
|
|
|
f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
|
|
|
|
|
) -> impl fmt::Display {
|
2021-01-28 19:04:29 -08:00
|
|
|
|
struct WithFormatter<F>(Cell<Option<F>>);
|
|
|
|
|
|
|
|
|
|
impl<F> fmt::Display for WithFormatter<F>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
(self.0.take()).unwrap()(f)
|
|
|
|
|
}
|
2019-08-12 14:36:09 -04:00
|
|
|
|
}
|
2021-01-28 19:04:29 -08:00
|
|
|
|
|
|
|
|
|
WithFormatter(Cell::new(Some(f)))
|
2019-08-12 14:36:09 -04:00
|
|
|
|
}
|