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
|
2013-10-03 10:24:40 -07:00
|
|
|
//! various types in `rustdoc::clean`. These implementations all currently
|
|
|
|
//! assume that HTML output is desired, although it may be possible to redesign
|
|
|
|
//! them in the future to instead emit any format desired.
|
|
|
|
|
2019-08-12 14:36:09 -04:00
|
|
|
use std::cell::Cell;
|
2013-09-18 22:18:38 -07:00
|
|
|
use std::fmt;
|
2021-04-17 22:34:58 -07:00
|
|
|
use std::iter;
|
2013-09-18 22:18:38 -07:00
|
|
|
|
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-04-29 21:36:54 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-12-16 18:10:04 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-04-29 21:36:54 +02:00
|
|
|
use rustc_span::def_id::CRATE_DEF_INDEX;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2013-09-18 22:18:38 -07:00
|
|
|
|
2021-04-29 21:36:54 +02:00
|
|
|
use crate::clean::{
|
|
|
|
self, utils::find_nearest_parent_module, ExternalCrate, FakeDefId, 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;
|
2020-06-24 08:16:21 -05:00
|
|
|
use crate::html::render::cache::ExternalLocation;
|
2021-03-17 11:41:01 -07:00
|
|
|
use crate::html::render::Context;
|
2019-02-23 16:40:07 +09:00
|
|
|
|
2020-11-14 17:59:58 -05:00
|
|
|
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
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Buffer),
|
2019-08-31 11:22:51 -04:00
|
|
|
{
|
|
|
|
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)]
|
2020-11-14 17:59:58 -05:00
|
|
|
crate struct Buffer {
|
2019-08-27 17:09:13 -04:00
|
|
|
for_html: bool,
|
|
|
|
buffer: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Buffer {
|
|
|
|
crate fn empty_from(v: &Buffer) -> Buffer {
|
2019-12-22 17:42:04 -05:00
|
|
|
Buffer { for_html: v.for_html, buffer: String::new() }
|
2019-08-27 17:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
crate fn html() -> Buffer {
|
2019-12-22 17:42:04 -05:00
|
|
|
Buffer { for_html: true, buffer: String::new() }
|
2019-08-27 17:09:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-08 13:56:26 +01:00
|
|
|
crate fn new() -> Buffer {
|
2019-12-22 17:42:04 -05:00
|
|
|
Buffer { for_html: false, buffer: String::new() }
|
2019-12-08 13:56:26 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 12:53:44 -07:00
|
|
|
crate fn is_empty(&self) -> bool {
|
|
|
|
self.buffer.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:09:13 -04:00
|
|
|
crate fn into_inner(self) -> String {
|
|
|
|
self.buffer
|
|
|
|
}
|
|
|
|
|
2020-07-06 12:53:44 -07:00
|
|
|
crate fn insert_str(&mut self, idx: usize, s: &str) {
|
|
|
|
self.buffer.insert_str(idx, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn push_str(&mut self, s: &str) {
|
|
|
|
self.buffer.push_str(s);
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:26:14 +02:00
|
|
|
crate fn push_buffer(&mut self, other: Buffer) {
|
|
|
|
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).
|
|
|
|
crate fn write_str(&mut self, s: &str) {
|
|
|
|
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).
|
|
|
|
crate fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
|
|
|
|
use fmt::Write;
|
|
|
|
self.buffer.write_fmt(v).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-08-31 11:22:51 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:56:26 +01:00
|
|
|
crate fn is_for_html(&self) -> bool {
|
|
|
|
self.for_html
|
|
|
|
}
|
2021-03-06 23:30:49 -05:00
|
|
|
|
|
|
|
crate fn reserve(&mut self, additional: usize) {
|
|
|
|
self.buffer.reserve(additional)
|
|
|
|
}
|
2019-08-27 17:09:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn comma_sep<T: fmt::Display>(items: impl Iterator<Item = T>) -> 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() {
|
2019-12-22 17:42:04 -05:00
|
|
|
if i != 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-04-16 11:21:17 -07:00
|
|
|
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();
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
for (i, bound) in
|
2021-03-17 11:41:01 -07:00
|
|
|
bounds.iter().filter(|b| bounds_dup.insert(b.print(cx).to_string())).enumerate()
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05:00
|
|
|
display_fn(move |f| match self.kind {
|
|
|
|
clean::GenericParamDefKind::Lifetime => write!(f, "{}", self.name),
|
|
|
|
clean::GenericParamDefKind::Type { ref bounds, ref default, .. } => {
|
2020-12-16 17:21:08 +01:00
|
|
|
f.write_str(&*self.name.as_str())?;
|
2019-09-12 19:59:14 -04:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if !bounds.is_empty() {
|
|
|
|
if f.alternate() {
|
2021-04-16 12:29:35 -07:00
|
|
|
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
|
2019-12-22 17:42:04 -05:00
|
|
|
} else {
|
2021-04-16 12:29:35 -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-12-22 17:42:04 -05: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 {
|
2021-03-17 11:41:01 -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(())
|
|
|
|
}
|
|
|
|
clean::GenericParamDefKind::Const { ref ty, .. } => {
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "const {}: {:#}", self.name, ty.print(cx))
|
2019-12-22 17:42:04 -05:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "const {}: {}", self.name, ty.print(cx))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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| {
|
2019-12-22 17:42:04 -05:00
|
|
|
let real_params =
|
|
|
|
self.params.iter().filter(|p| !p.is_synthetic_type_param()).collect::<Vec<_>>();
|
2019-09-12 19:59:14 -04:00
|
|
|
if real_params.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "<{:#}>", comma_sep(real_params.iter().map(|g| g.print(cx))))
|
2019-09-12 19:59:14 -04:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "<{}>", comma_sep(real_params.iter().map(|g| g.print(cx))))
|
2019-09-12 19:59:14 -04:00
|
|
|
}
|
|
|
|
})
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2021-04-16 11:21:17 -07:00
|
|
|
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,
|
|
|
|
end_newline: bool,
|
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| {
|
|
|
|
if gens.where_predicates.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let mut clause = String::new();
|
|
|
|
if f.alternate() {
|
|
|
|
clause.push_str(" where");
|
|
|
|
} else {
|
|
|
|
if end_newline {
|
|
|
|
clause.push_str(" <span class=\"where fmt-newline\">where");
|
|
|
|
} else {
|
|
|
|
clause.push_str(" <span class=\"where\">where");
|
2017-03-31 18:04:42 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
|
|
|
for (i, pred) in gens.where_predicates.iter().enumerate() {
|
2017-03-31 18:04:42 -05:00
|
|
|
if f.alternate() {
|
2021-03-07 18:09:35 +01:00
|
|
|
clause.push(' ');
|
2017-03-31 18:04:42 -05:00
|
|
|
} else {
|
2021-03-07 18:09:35 +01:00
|
|
|
clause.push_str("<br>");
|
2014-09-25 02:01:42 -07:00
|
|
|
}
|
2017-03-31 18:04:42 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
match pred {
|
|
|
|
clean::WherePredicate::BoundPredicate { ty, bounds } => {
|
|
|
|
let bounds = bounds;
|
|
|
|
if f.alternate() {
|
|
|
|
clause.push_str(&format!(
|
|
|
|
"{:#}: {:#}",
|
2021-03-17 11:41:01 -07:00
|
|
|
ty.print(cx),
|
2021-04-16 12:29:35 -07:00
|
|
|
print_generic_bounds(bounds, cx)
|
2021-03-07 18:09:35 +01:00
|
|
|
));
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
clause.push_str(&format!(
|
|
|
|
"{}: {}",
|
2021-03-17 11:41:01 -07:00
|
|
|
ty.print(cx),
|
2021-04-16 12:29:35 -07:00
|
|
|
print_generic_bounds(bounds, cx)
|
2019-12-22 17:42:04 -05:00
|
|
|
));
|
2016-09-26 16:02:21 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
|
|
|
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
|
|
|
|
clause.push_str(&format!(
|
|
|
|
"{}: {}",
|
|
|
|
lifetime.print(),
|
|
|
|
bounds
|
|
|
|
.iter()
|
2021-03-17 11:41:01 -07:00
|
|
|
.map(|b| b.print(cx).to_string())
|
2021-03-07 18:09:35 +01:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(" + ")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
clean::WherePredicate::EqPredicate { lhs, rhs } => {
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
clause.push_str(&format!("{:#} == {:#}", lhs.print(cx), rhs.print(cx),));
|
2021-03-07 18:09:35 +01:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
clause.push_str(&format!("{} == {}", lhs.print(cx), rhs.print(cx),));
|
2016-09-26 16:02:21 -05:00
|
|
|
}
|
2014-12-23 01:08:00 -08:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
2017-03-31 18:04:42 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
if i < gens.where_predicates.len() - 1 || end_newline {
|
|
|
|
clause.push(',');
|
2017-03-31 18:04:42 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
2017-04-06 18:36:14 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
if end_newline {
|
|
|
|
// add a space so stripping <br> tags and breaking spaces still renders properly
|
|
|
|
if f.alternate() {
|
|
|
|
clause.push(' ');
|
|
|
|
} else {
|
|
|
|
clause.push_str(" ");
|
2017-04-06 18:36:14 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
2017-04-06 18:36:14 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
if !f.alternate() {
|
|
|
|
clause.push_str("</span>");
|
|
|
|
let padding = " ".repeat(indent + 4);
|
|
|
|
clause = clause.replace("<br>", &format!("<br>{}", padding));
|
|
|
|
clause.insert_str(0, &" ".repeat(indent.saturating_sub(1)));
|
|
|
|
if !end_newline {
|
|
|
|
clause.insert_str(0, "<br>");
|
2016-10-13 10:17:25 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
|
|
|
write!(f, "{}", clause)
|
|
|
|
})
|
2014-09-25 02:01:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::Lifetime {
|
2020-12-17 14:02:09 +01:00
|
|
|
crate fn print(&self) -> impl fmt::Display + '_ {
|
2019-09-12 19:59:14 -04:00
|
|
|
self.get_ref()
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::Constant {
|
2021-03-07 18:09:35 +01:00
|
|
|
crate fn print(&self, tcx: TyCtxt<'_>) -> impl fmt::Display + '_ {
|
|
|
|
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() {
|
2019-12-22 17:42:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"for<{:#}> ",
|
2021-03-17 11:41:01 -07:00
|
|
|
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
|
2019-12-22 17:42:04 -05:00
|
|
|
)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"for<{}> ",
|
2021-03-17 11:41:01 -07:00
|
|
|
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
|
2019-12-22 17:42:04 -05:00
|
|
|
)?;
|
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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05: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 => "?",
|
2020-01-13 20:30:27 -08:00
|
|
|
hir::TraitBoundModifier::MaybeConst => "?const",
|
2019-12-22 17:42:04 -05:00
|
|
|
};
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "{}{:#}", modifier_str, ty.print(cx))
|
2019-12-22 17:42:04 -05: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;
|
|
|
|
for arg in args {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
for binding in bindings {
|
|
|
|
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;
|
|
|
|
for ty in inputs {
|
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-03-17 11:41:01 -07:00
|
|
|
crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<String>)> {
|
|
|
|
let cache = &cx.cache();
|
|
|
|
let relative_to = &cx.current;
|
|
|
|
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
|
|
|
|
if shortty == ItemType::Module { &fqp[..] } else { &fqp[..fqp.len() - 1] }
|
|
|
|
}
|
|
|
|
|
2020-05-30 11:35:35 -04:00
|
|
|
if !did.is_local() && !cache.access_levels.is_public(did) && !cache.document_private {
|
2019-12-22 17:42:04 -05:00
|
|
|
return None;
|
2016-04-24 14:11:26 +02:00
|
|
|
}
|
|
|
|
|
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, {
|
|
|
|
let module_fqp = to_module_fqp(shortty, fqp);
|
|
|
|
href_relative_parts(module_fqp, relative_to)
|
|
|
|
}),
|
2017-12-08 17:32:04 -08:00
|
|
|
None => {
|
|
|
|
let &(ref fqp, shortty) = cache.external_paths.get(&did)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
let module_fqp = to_module_fqp(shortty, fqp);
|
2019-12-22 17:42:04 -05:00
|
|
|
(
|
|
|
|
fqp,
|
|
|
|
shortty,
|
|
|
|
match cache.extern_locations[&did.krate] {
|
2021-04-29 19:14:29 +02:00
|
|
|
ExternalLocation::Remote(ref s) => {
|
2021-03-17 11:41:01 -07:00
|
|
|
let s = s.trim_end_matches('/');
|
|
|
|
let mut s = vec![&s[..]];
|
|
|
|
s.extend(module_fqp[..].iter().map(String::as_str));
|
|
|
|
s
|
|
|
|
}
|
2021-04-29 19:14:29 +02:00
|
|
|
ExternalLocation::Local => href_relative_parts(module_fqp, relative_to),
|
|
|
|
ExternalLocation::Unknown => return None,
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
)
|
2015-04-06 17:56:35 -07:00
|
|
|
}
|
|
|
|
};
|
2021-03-17 11:41:01 -07:00
|
|
|
let last = &fqp.last().unwrap()[..];
|
|
|
|
let filename;
|
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-03-17 11:41:01 -07:00
|
|
|
filename = format!("{}.{}.html", shortty.as_str(), last);
|
|
|
|
url_parts.push(&filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some((url_parts.join("/"), shortty, fqp.to_vec()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String]) -> Vec<&'a str> {
|
|
|
|
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;
|
|
|
|
let fqp_module = fqp[i..fqp.len()].iter().map(String::as_str);
|
2021-04-17 22:34:58 -07:00
|
|
|
return iter::repeat("..").take(dissimilar_part_count).chain(fqp_module).collect();
|
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() {
|
|
|
|
fqp[relative_to_fqp.len()..fqp.len()].iter().map(String::as_str).collect()
|
|
|
|
// 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();
|
2021-04-17 22:34:58 -07:00
|
|
|
iter::repeat("..").take(dissimilar_part_count).collect()
|
2021-03-17 11:41:01 -07:00
|
|
|
// linking to the same module
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
2015-04-06 17:56:35 -07:00
|
|
|
}
|
|
|
|
|
2013-10-03 10:24:40 -07:00
|
|
|
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
|
|
|
|
/// rendering function with the necessary arguments for linking to a local path.
|
2021-03-17 11:41:01 -07:00
|
|
|
fn resolved_path<'a, 'cx: 'a>(
|
2019-12-22 17:42:04 -05:00
|
|
|
w: &mut fmt::Formatter<'_>,
|
|
|
|
did: DefId,
|
|
|
|
path: &clean::Path,
|
|
|
|
print_all: bool,
|
|
|
|
use_absolute: bool,
|
2021-03-17 11:41:01 -07:00
|
|
|
cx: &'cx Context<'_>,
|
2019-12-22 17:42:04 -05: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] {
|
|
|
|
write!(w, "{}::", seg.name)?;
|
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-03-17 11:41:01 -07:00
|
|
|
if let Some((_, _, fqp)) = href(did, cx) {
|
2021-01-12 23:36:04 +01:00
|
|
|
format!(
|
|
|
|
"{}::{}",
|
|
|
|
fqp[..fqp.len() - 1].join("::"),
|
2021-03-17 11:41:01 -07:00
|
|
|
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-03-17 11:41:01 -07:00
|
|
|
anchor(did, &*last.name.as_str(), 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-12-22 17:42:04 -05:00
|
|
|
fn primitive_link(
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
|
|
|
prim: clean::PrimitiveType,
|
|
|
|
name: &str,
|
2021-04-17 22:34:58 -07:00
|
|
|
cx: &Context<'_>,
|
2019-12-22 17:42:04 -05: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();
|
2019-12-22 17:42:04 -05:00
|
|
|
let len = if len == 0 { 0 } else { len - 1 };
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"<a class=\"primitive\" href=\"{}primitive.{}.html\">",
|
|
|
|
"../".repeat(len),
|
|
|
|
prim.to_url_str()
|
|
|
|
)?;
|
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) => {
|
2021-04-17 22:34:58 -07:00
|
|
|
let cname_str;
|
2016-11-29 08:15:16 +02:00
|
|
|
let loc = match m.extern_locations[&def_id.krate] {
|
2021-04-29 19:14:29 +02:00
|
|
|
ExternalLocation::Remote(ref s) => {
|
|
|
|
cname_str =
|
|
|
|
ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()).as_str();
|
2021-04-17 22:34:58 -07:00
|
|
|
Some(vec![s.trim_end_matches('/'), &cname_str[..]])
|
|
|
|
}
|
2021-04-29 19:14:29 +02:00
|
|
|
ExternalLocation::Local => {
|
|
|
|
cname_str =
|
|
|
|
ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()).as_str();
|
2021-04-17 22:34:58 -07:00
|
|
|
Some(if cx.current.first().map(|x| &x[..]) == Some(&cname_str[..]) {
|
|
|
|
iter::repeat("..").take(cx.current.len() - 1).collect()
|
|
|
|
} else {
|
|
|
|
let cname = iter::once(&cname_str[..]);
|
|
|
|
iter::repeat("..").take(cx.current.len()).chain(cname).collect()
|
|
|
|
})
|
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-04-17 22:34:58 -07:00
|
|
|
if let Some(loc) = loc {
|
2019-12-22 17:42:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
2021-04-17 22:34:58 -07:00
|
|
|
"<a class=\"primitive\" href=\"{}/primitive.{}.html\">",
|
|
|
|
loc.join("/"),
|
2019-12-22 17:42:04 -05:00
|
|
|
prim.to_url_str()
|
|
|
|
)?;
|
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-01-12 23:36:04 +01:00
|
|
|
param_names: &'a Option<Vec<clean::GenericBound>>,
|
2021-04-16 11:21:17 -07:00
|
|
|
cx: &'a Context<'tcx>,
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
display_fn(move |f| match *param_names {
|
|
|
|
Some(ref params) => {
|
|
|
|
for param in params {
|
|
|
|
write!(f, " + ")?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(¶m.print(cx), f)?;
|
2013-10-02 15:39:32 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(())
|
2013-10-02 15:39:32 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
None => Ok(()),
|
2019-08-12 14:36:09 -04:00
|
|
|
})
|
2016-04-25 08:24:50 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 11:41:01 -07:00
|
|
|
crate fn anchor<'a, 'cx: 'a>(
|
|
|
|
did: DefId,
|
|
|
|
text: &'a str,
|
|
|
|
cx: &'cx Context<'_>,
|
|
|
|
) -> impl fmt::Display + 'a {
|
2021-04-29 21:36:54 +02:00
|
|
|
let parts = href(did.into(), cx);
|
2019-08-12 14:36:09 -04:00
|
|
|
display_fn(move |f| {
|
2021-03-17 11:41:01 -07:00
|
|
|
if let Some((url, short_ty, fqp)) = parts {
|
2019-12-22 17:42:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
|
|
|
|
short_ty,
|
|
|
|
url,
|
|
|
|
short_ty,
|
|
|
|
fqp.join("::"),
|
|
|
|
text
|
|
|
|
)
|
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 {
|
2020-12-12 11:33:45 -08:00
|
|
|
debug!("fmt_type(t = {:?})", t);
|
|
|
|
|
2016-12-15 23:13:00 -08:00
|
|
|
match *t {
|
2020-12-16 17:21:08 +01:00
|
|
|
clean::Generic(name) => write!(f, "{}", name),
|
2019-12-22 17:42:04 -05:00
|
|
|
clean::ResolvedPath { did, ref param_names, ref path, is_generic } => {
|
2019-03-13 23:37:02 +00:00
|
|
|
if param_names.is_some() {
|
2018-10-15 00:48:57 +01:00
|
|
|
f.write_str("dyn ")?;
|
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
|
2021-03-17 11:41:01 -07:00
|
|
|
resolved_path(f, did, path, is_generic, use_absolute, cx)?;
|
|
|
|
fmt::Display::fmt(&tybounds(param_names, cx), f)
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
|
|
|
clean::Infer => write!(f, "_"),
|
2021-04-17 22:34:58 -07:00
|
|
|
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str(), cx),
|
2016-12-15 23:13:00 -08:00
|
|
|
clean::BareFunction(ref decl) => {
|
|
|
|
if f.alternate() {
|
2019-12-22 17:42:04 -05: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-12-22 17:42:04 -05: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-12-22 17:42:04 -05: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-12-22 17:42:04 -05: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),
|
2017-05-31 18:02:35 +01:00
|
|
|
&[ref one] => {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
|
2019-02-28 22:43:53 +00:00
|
|
|
// Carry `f.alternate()` into this display w/o branching manually.
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&one.print(cx), f)?;
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Tuple, ",)", cx)
|
2016-09-26 16:02:21 -05:00
|
|
|
}
|
2017-05-31 18:02:35 +01:00
|
|
|
many => {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
for (i, item) in many.iter().enumerate() {
|
2019-12-22 17:42:04 -05:00
|
|
|
if i != 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&item.print(cx), f)?;
|
2019-09-12 19:59:14 -04:00
|
|
|
}
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Tuple, ")", cx)
|
2016-01-22 23:15:47 +05:30
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
2017-05-31 14:39:30 +01:00
|
|
|
clean::Slice(ref t) => {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Slice, "[", cx)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&t.print(cx), f)?;
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Slice, "]", cx)
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
2017-08-05 12:27:28 +03:00
|
|
|
clean::Array(ref t, ref n) => {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Array, "[", cx)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&t.print(cx), f)?;
|
2020-01-05 23:19:42 +00:00
|
|
|
if f.alternate() {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
|
2020-01-05 23:19:42 +00:00
|
|
|
} else {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
|
2020-01-05 23:19:42 +00:00
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
2021-04-17 22:34:58 -07:00
|
|
|
clean::Never => primitive_link(f, PrimitiveType::Never, "!", cx),
|
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
|
|
|
};
|
2016-12-15 23:13:00 -08:00
|
|
|
match **t {
|
2019-12-22 17:42:04 -05:00
|
|
|
clean::Generic(_) | clean::ResolvedPath { is_generic: true, .. } => {
|
2016-12-15 23:13:00 -08:00
|
|
|
if f.alternate() {
|
2019-12-22 17:42:04 -05:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
clean::PrimitiveType::RawPointer,
|
2021-03-17 11:41:01 -07:00
|
|
|
&format!("*{} {:#}", m, t.print(cx)),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2016-12-15 23:13:00 -08:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
clean::PrimitiveType::RawPointer,
|
2021-03-17 11:41:01 -07:00
|
|
|
&format!("*{} {}", m, t.print(cx)),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2016-01-23 15:01:17 +05:30
|
|
|
}
|
|
|
|
}
|
2017-05-31 18:02:35 +01:00
|
|
|
_ => {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&t.print(cx), f)
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
2016-12-15 23:13:00 -08:00
|
|
|
}
|
2019-12-22 17:42:04 -05: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()),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => String::new(),
|
2016-12-15 23:13:00 -08:00
|
|
|
};
|
2019-09-13 08:36:00 -04:00
|
|
|
let m = mutability.print_with_space();
|
2019-12-22 17:42:04 -05:00
|
|
|
let amp = if f.alternate() { "&".to_string() } else { "&".to_string() };
|
2016-12-15 23:13:00 -08:00
|
|
|
match **ty {
|
2019-12-22 17:42:04 -05:00
|
|
|
clean::Slice(ref bt) => {
|
|
|
|
// `BorrowedRef{ ... Slice(T) }` is `&[T]`
|
2016-12-15 23:13:00 -08:00
|
|
|
match **bt {
|
2017-05-31 18:02:35 +01:00
|
|
|
clean::Generic(_) => {
|
2016-12-15 23:13:00 -08:00
|
|
|
if f.alternate() {
|
2019-12-22 17:42:04 -05:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
PrimitiveType::Slice,
|
2021-03-17 11:41:01 -07:00
|
|
|
&format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cx)),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2016-12-15 23:13:00 -08:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
PrimitiveType::Slice,
|
2021-03-17 11:41:01 -07:00
|
|
|
&format!("{}{}{}[{}]", amp, lt, m, bt.print(cx)),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2017-02-26 18:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 18:02:35 +01:00
|
|
|
_ => {
|
2019-12-22 17:42:04 -05:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
PrimitiveType::Slice,
|
|
|
|
&format!("{}{}{}[", amp, lt, m),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2019-12-22 17:42:04 -05:00
|
|
|
)?;
|
2016-12-15 23:13:00 -08:00
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "{:#}", bt.print(cx))?;
|
2016-12-15 23:13:00 -08:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, "{}", bt.print(cx))?;
|
2014-09-28 00:15:31 +08:00
|
|
|
}
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::Slice, "]", cx)
|
2016-09-26 16:02:21 -05:00
|
|
|
}
|
2014-09-28 00:15:31 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 23:37:02 +00:00
|
|
|
clean::ResolvedPath { param_names: Some(ref v), .. } if !v.is_empty() => {
|
2017-07-30 14:59:08 -05:00
|
|
|
write!(f, "{}{}{}(", amp, lt, m)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt_type(&ty, f, use_absolute, cx)?;
|
2017-05-31 18:02:35 +01:00
|
|
|
write!(f, ")")
|
|
|
|
}
|
2017-07-30 14:59:08 -05:00
|
|
|
clean::Generic(..) => {
|
2021-01-12 23:36:04 +01:00
|
|
|
primitive_link(
|
|
|
|
f,
|
|
|
|
PrimitiveType::Reference,
|
|
|
|
&format!("{}{}{}", amp, lt, m),
|
2021-04-17 22:34:58 -07:00
|
|
|
cx,
|
2021-01-12 23:36:04 +01:00
|
|
|
)?;
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt_type(&ty, f, use_absolute, 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-03-17 11:41:01 -07: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
|
|
|
}
|
|
|
|
clean::QPath { ref name, ref self_type, ref trait_ } => {
|
2017-04-12 18:14:54 +02:00
|
|
|
let should_show_cast = match *trait_ {
|
2017-06-11 18:20:48 +01:00
|
|
|
box clean::ResolvedPath { ref path, .. } => {
|
|
|
|
!path.segments.is_empty() && !self_type.is_self_type()
|
2017-04-12 18:14:54 +02:00
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
match *trait_ {
|
|
|
|
// 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).
|
2019-03-13 23:37:02 +00:00
|
|
|
box clean::ResolvedPath { did, ref param_names, .. } => {
|
2021-04-29 21:36:54 +02:00
|
|
|
match href(did.into(), cx) {
|
2017-06-11 18:20:48 +01:00
|
|
|
Some((ref url, _, ref path)) if !f.alternate() => {
|
2019-12-22 17:42:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"<a class=\"type\" href=\"{url}#{shortty}.{name}\" \
|
2020-08-31 13:16:50 +02:00
|
|
|
title=\"type {path}::{name}\">{name}</a>",
|
2019-12-22 17:42:04 -05:00
|
|
|
url = url,
|
|
|
|
shortty = ItemType::AssocType,
|
|
|
|
name = name,
|
|
|
|
path = path.join("::")
|
|
|
|
)?;
|
2017-06-11 18:20:48 +01:00
|
|
|
}
|
|
|
|
_ => write!(f, "{}", name)?,
|
|
|
|
}
|
2017-04-12 18:14:54 +02:00
|
|
|
|
2019-03-13 23:37:02 +00:00
|
|
|
// FIXME: `param_names` are not rendered, and this seems bad?
|
|
|
|
drop(param_names);
|
2017-04-12 18:14:54 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => write!(f, "{}", name),
|
2014-07-25 09:31:20 -07:00
|
|
|
}
|
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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::Impl {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-01-08 22:54:35 +01:00
|
|
|
if self.negative_polarity {
|
2019-09-12 19:59:14 -04:00
|
|
|
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
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
if let Some(ref ty) = self.blanket_impl {
|
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
|
|
|
|
2021-03-17 11:41:01 -07:00
|
|
|
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, true), 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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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() {
|
|
|
|
if !input.name.is_empty() {
|
|
|
|
write!(f, "{}: ", input.name)?;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2019-12-22 17:42:04 -05: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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05: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-12-22 17:42:04 -05:00
|
|
|
clean::DefaultReturn => Ok(()),
|
2019-09-12 19:59:14 -04:00
|
|
|
})
|
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-03-17 11:41:01 -07:00
|
|
|
write!(f, "for<{}> ", comma_sep(self.generic_params.iter().map(|g| g.print(cx))))
|
2020-12-12 11:33:45 -08:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})
|
2013-11-28 02:23:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::FnDecl {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05:00
|
|
|
let ellipsis = if self.c_variadic { ", ..." } else { "" };
|
2019-09-12 19:59:14 -04:00
|
|
|
if f.alternate() {
|
2019-12-22 17:42:04 -05:00
|
|
|
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-12-22 17:42:04 -05: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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
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-12-22 17:42:04 -05: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.
|
|
|
|
/// <br>Used to determine line-wrapping.
|
|
|
|
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
|
|
|
|
/// necessary.
|
|
|
|
/// * `asyncness`: Whether the function is async or not.
|
2021-04-16 11:21:17 -07:00
|
|
|
crate fn full_print<'a, 'tcx: 'a>(
|
2021-03-07 18:09:35 +01:00
|
|
|
&'a self,
|
|
|
|
header_len: usize,
|
|
|
|
indent: usize,
|
|
|
|
asyncness: hir::IsAsync,
|
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> {
|
2021-04-16 12:29:35 -07:00
|
|
|
display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, 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,
|
|
|
|
header_len: usize,
|
|
|
|
indent: usize,
|
|
|
|
asyncness: hir::IsAsync,
|
|
|
|
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 { "&" };
|
|
|
|
let mut args = String::new();
|
|
|
|
let mut args_plain = String::new();
|
|
|
|
for (i, input) in self.inputs.values.iter().enumerate() {
|
|
|
|
if i == 0 {
|
|
|
|
args.push_str("<br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(selfty) = input.to_self() {
|
|
|
|
match selfty {
|
|
|
|
clean::SelfValue => {
|
|
|
|
args.push_str("self");
|
|
|
|
args_plain.push_str("self");
|
2016-09-26 16:02:21 -05:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
clean::SelfBorrowed(Some(ref lt), mtbl) => {
|
|
|
|
args.push_str(&format!(
|
|
|
|
"{}{} {}self",
|
|
|
|
amp,
|
|
|
|
lt.print(),
|
|
|
|
mtbl.print_with_space()
|
|
|
|
));
|
|
|
|
args_plain.push_str(&format!(
|
|
|
|
"&{} {}self",
|
|
|
|
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) => {
|
|
|
|
args.push_str(&format!("{}{}self", amp, mtbl.print_with_space()));
|
|
|
|
args_plain.push_str(&format!("&{}self", mtbl.print_with_space()));
|
2016-05-08 21:19:29 +03:00
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
clean::SelfExplicit(ref typ) => {
|
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
args.push_str(&format!("self: {:#}", typ.print(cx)));
|
2021-03-07 18:09:35 +01:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
args.push_str(&format!("self: {}", typ.print(cx)));
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
args_plain.push_str(&format!("self: {:#}", typ.print(cx)));
|
2016-05-08 21:19:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 18:09:35 +01:00
|
|
|
} else {
|
|
|
|
if i > 0 {
|
|
|
|
args.push_str(" <br>");
|
|
|
|
args_plain.push(' ');
|
|
|
|
}
|
|
|
|
if !input.name.is_empty() {
|
|
|
|
args.push_str(&format!("{}: ", input.name));
|
|
|
|
args_plain.push_str(&format!("{}: ", input.name));
|
2016-05-08 21:19:29 +03:00
|
|
|
}
|
2019-08-10 14:38:17 +03:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
args.push_str(&format!("{:#}", input.type_.print(cx)));
|
2021-03-07 18:09:35 +01:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
args.push_str(&input.type_.print(cx).to_string());
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
2021-03-17 11:41:01 -07:00
|
|
|
args_plain.push_str(&format!("{:#}", input.type_.print(cx)));
|
2021-03-07 18:09:35 +01:00
|
|
|
}
|
|
|
|
if i + 1 < self.inputs.values.len() {
|
|
|
|
args.push(',');
|
|
|
|
args_plain.push(',');
|
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
|
|
|
let mut args_plain = format!("({})", args_plain);
|
2019-02-05 10:12:43 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
if self.c_variadic {
|
|
|
|
args.push_str(",<br> ...");
|
|
|
|
args_plain.push_str(", ...");
|
|
|
|
}
|
2016-09-26 16:02:21 -05:00
|
|
|
|
2021-03-07 18:09:35 +01:00
|
|
|
let arrow_plain;
|
|
|
|
let arrow = if let hir::IsAsync::Async = asyncness {
|
|
|
|
let output = self.sugared_async_return_type();
|
2021-03-17 11:41:01 -07:00
|
|
|
arrow_plain = format!("{:#}", output.print(cx));
|
|
|
|
if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) }
|
2021-03-07 18:09:35 +01:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
arrow_plain = format!("{:#}", self.output.print(cx));
|
|
|
|
if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }
|
2021-03-07 18:09:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
|
|
|
|
let output = if declaration_len > 80 {
|
|
|
|
let full_pad = format!("<br>{}", " ".repeat(indent + 4));
|
|
|
|
let close_pad = format!("<br>{}", " ".repeat(indent));
|
|
|
|
format!(
|
|
|
|
"({args}{close}){arrow}",
|
|
|
|
args = args.replace("<br>", &full_pad),
|
|
|
|
close = close_pad,
|
|
|
|
arrow = arrow
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if f.alternate() {
|
|
|
|
write!(f, "{}", output.replace("<br>", "\n"))
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", output)
|
|
|
|
}
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::Visibility {
|
2021-04-16 11:21:17 -07:00
|
|
|
crate fn print_with_space<'a, 'tcx: 'a>(
|
2020-12-25 11:48:12 -08:00
|
|
|
self,
|
2021-04-29 21:36:54 +02:00
|
|
|
item_did: FakeDefId,
|
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> {
|
2021-01-12 23:36:04 +01:00
|
|
|
let to_print = match self {
|
|
|
|
clean::Public => "pub ".to_owned(),
|
|
|
|
clean::Inherited => String::new(),
|
2020-12-25 15:38:46 -08:00
|
|
|
clean::Visibility::Restricted(vis_did) => {
|
2020-12-31 12:00:23 -08:00
|
|
|
// 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.
|
2021-04-29 21:36:54 +02:00
|
|
|
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_real());
|
2020-12-25 15:54:04 -08:00
|
|
|
|
2020-12-25 16:16:40 -08:00
|
|
|
if vis_did.index == CRATE_DEF_INDEX {
|
2021-01-12 23:36:04 +01:00
|
|
|
"pub(crate) ".to_owned()
|
2020-12-25 16:16:40 -08:00
|
|
|
} else if parent_module == Some(vis_did) {
|
2020-12-25 15:38:46 -08:00
|
|
|
// `pub(in foo)` where `foo` is the parent module
|
|
|
|
// is the same as no visibility modifier
|
2021-01-12 23:36:04 +01:00
|
|
|
String::new()
|
2020-12-25 15:54:04 -08:00
|
|
|
} else if parent_module
|
2021-03-17 11:41:01 -07:00
|
|
|
.map(|parent| find_nearest_parent_module(cx.tcx(), parent))
|
2020-12-25 15:54:04 -08:00
|
|
|
.flatten()
|
|
|
|
== Some(vis_did)
|
|
|
|
{
|
2021-01-12 23:36:04 +01:00
|
|
|
"pub(super) ".to_owned()
|
2020-12-25 15:38:46 -08:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
let path = cx.tcx().def_path(vis_did);
|
2020-12-25 15:38:46 -08:00
|
|
|
debug!("path={:?}", path);
|
2021-01-12 23:36:04 +01:00
|
|
|
// modified from `resolved_path()` to work with `DefPathData`
|
|
|
|
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
|
2021-03-17 11:41:01 -07:00
|
|
|
let anchor = anchor(vis_did, &last_name.as_str(), cx).to_string();
|
2021-01-12 23:36:04 +01:00
|
|
|
|
2021-04-10 14:22:06 -07:00
|
|
|
let mut s = "pub(in ".to_owned();
|
2020-12-25 15:38:46 -08:00
|
|
|
for seg in &path.data[..path.data.len() - 1] {
|
2021-01-12 23:36:04 +01:00
|
|
|
s.push_str(&format!("{}::", seg.data.get_opt_name().unwrap()));
|
2020-12-25 15:38:46 -08:00
|
|
|
}
|
2021-01-12 23:36:04 +01:00
|
|
|
s.push_str(&format!("{}) ", anchor));
|
|
|
|
s
|
2020-11-14 01:51:05 -05:00
|
|
|
}
|
2018-05-12 18:25:09 +01:00
|
|
|
}
|
2021-01-12 23:36:04 +01:00
|
|
|
};
|
|
|
|
display_fn(move |f| f.write_str(&to_print))
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
2021-04-10 14:22:06 -07: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.
|
|
|
|
crate fn to_src_with_space<'a, 'tcx: 'a>(
|
|
|
|
self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
item_did: DefId,
|
|
|
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
|
|
|
let to_print = match self {
|
|
|
|
clean::Public => "pub ".to_owned(),
|
|
|
|
clean::Inherited => String::new(),
|
|
|
|
clean::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.index == CRATE_DEF_INDEX {
|
|
|
|
"pub(crate) ".to_owned()
|
|
|
|
} else if parent_module == Some(vis_did) {
|
|
|
|
// `pub(in foo)` where `foo` is the parent module
|
|
|
|
// is the same as no visibility modifier
|
|
|
|
String::new()
|
|
|
|
} else if parent_module
|
|
|
|
.map(|parent| find_nearest_parent_module(tcx, parent))
|
|
|
|
.flatten()
|
|
|
|
== Some(vis_did)
|
|
|
|
{
|
|
|
|
"pub(super) ".to_owned()
|
|
|
|
} else {
|
|
|
|
format!("pub(in {}) ", tcx.def_path_str(vis_did))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
|
2019-09-13 08:36:00 -04:00
|
|
|
crate trait PrintWithSpace {
|
|
|
|
fn print_with_space(&self) -> &str;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrintWithSpace for hir::Unsafety {
|
|
|
|
fn print_with_space(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
hir::Unsafety::Unsafe => "unsafe ",
|
2019-12-22 17:42:04 -05:00
|
|
|
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::Constness {
|
|
|
|
fn print_with_space(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
hir::Constness::Const => "const ",
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::Constness::NotConst => "",
|
2015-02-25 22:05:07 +02: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 ",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:59:14 -04:00
|
|
|
impl clean::Import {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05: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-12-22 17:42:04 -05: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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05: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-12-22 17:42:04 -05:00
|
|
|
_ => {
|
2020-01-26 21:28:09 +00:00
|
|
|
for seg in &self.path.segments[..self.path.segments.len() - 1] {
|
|
|
|
write!(f, "{}::", seg.name)?;
|
|
|
|
}
|
|
|
|
let name = self.path.last_name();
|
|
|
|
if let hir::def::Res::PrimTy(p) = self.path.res {
|
2021-04-17 22:34:58 -07:00
|
|
|
primitive_link(f, PrimitiveType::from(p), &*name, 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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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| {
|
2020-12-16 17:21:08 +01:00
|
|
|
f.write_str(&*self.name.as_str())?;
|
2019-09-12 19:59:14 -04:00
|
|
|
match self.kind {
|
|
|
|
clean::TypeBindingKind::Equality { ref ty } => {
|
2019-05-08 15:57:06 -04:00
|
|
|
if f.alternate() {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, " = {:#}", ty.print(cx))?;
|
2019-05-08 15:57:06 -04:00
|
|
|
} else {
|
2021-03-17 11:41:01 -07:00
|
|
|
write!(f, " = {}", ty.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 {
|
2021-04-16 12:29:35 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:36:00 -04:00
|
|
|
crate fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
|
|
|
|
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
|
|
|
|
2019-09-13 08:36:00 -04:00
|
|
|
crate fn print_default_space<'a>(v: bool) -> &'a str {
|
2019-12-22 17:42:04 -05: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 {
|
2021-04-16 11:21:17 -07:00
|
|
|
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-12-22 17:42:04 -05: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),
|
2019-09-12 19:59:14 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
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
|
|
|
}
|