librustdoc: more usages of Joined::joined
This commit is contained in:
parent
4a76615054
commit
c0b1c6eed5
3 changed files with 61 additions and 43 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use std::assert_matches::debug_assert_matches;
|
||||
use std::fmt::Write as _;
|
||||
use std::fmt::{self, Display, Write as _};
|
||||
use std::mem;
|
||||
use std::sync::LazyLock as Lazy;
|
||||
|
||||
|
|
@ -24,6 +24,7 @@ use crate::clean::{
|
|||
clean_middle_ty, inline,
|
||||
};
|
||||
use crate::core::DocContext;
|
||||
use crate::display::Joined as _;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -250,16 +251,20 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
|
|||
hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
|
||||
};
|
||||
|
||||
let mut s = String::new();
|
||||
for (i, seg) in segments.iter().enumerate() {
|
||||
if i > 0 {
|
||||
s.push_str("::");
|
||||
}
|
||||
if seg.ident.name != kw::PathRoot {
|
||||
s.push_str(seg.ident.as_str());
|
||||
}
|
||||
}
|
||||
s
|
||||
fmt::from_fn(|f| {
|
||||
segments
|
||||
.iter()
|
||||
.map(|seg| {
|
||||
fmt::from_fn(|f| {
|
||||
if seg.ident.name != kw::PathRoot {
|
||||
write!(f, "{}", seg.ident)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.joined("::", f)
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub(crate) fn build_deref_target_impls(
|
||||
|
|
@ -311,12 +316,11 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
|
|||
PatKind::TupleStruct(ref p, ..)
|
||||
| PatKind::Expr(PatExpr { kind: PatExprKind::Path(ref p), .. }) => qpath_to_string(p),
|
||||
PatKind::Or(pats) => {
|
||||
pats.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(" | ")
|
||||
fmt::from_fn(|f| pats.iter().map(|p| name_from_pat(p)).joined(" | ", f)).to_string()
|
||||
}
|
||||
PatKind::Tuple(elts, _) => {
|
||||
format!("({})", fmt::from_fn(|f| elts.iter().map(|p| name_from_pat(p)).joined(", ", f)))
|
||||
}
|
||||
PatKind::Tuple(elts, _) => format!(
|
||||
"({})",
|
||||
elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
|
||||
),
|
||||
PatKind::Deref(p) => format!("deref!({})", name_from_pat(p)),
|
||||
PatKind::Expr(..) => {
|
||||
warn!(
|
||||
|
|
@ -324,11 +328,25 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
|
|||
);
|
||||
return Symbol::intern("()");
|
||||
}
|
||||
PatKind::Slice(begin, ref mid, end) => {
|
||||
let begin = begin.iter().map(|p| name_from_pat(p).to_string());
|
||||
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(p))).into_iter();
|
||||
let end = end.iter().map(|p| name_from_pat(p).to_string());
|
||||
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
|
||||
PatKind::Slice(begin, mid, end) => {
|
||||
fn print_pat<'a>(pat: &'a Pat<'a>, wild: bool) -> impl Display + 'a {
|
||||
fmt::from_fn(move |f| {
|
||||
if wild {
|
||||
f.write_str("..")?;
|
||||
}
|
||||
name_from_pat(pat).fmt(f)
|
||||
})
|
||||
}
|
||||
|
||||
format!(
|
||||
"[{}]",
|
||||
fmt::from_fn(|f| {
|
||||
let begin = begin.iter().map(|p| print_pat(p, false));
|
||||
let mid = mid.map(|p| print_pat(p, true));
|
||||
let end = end.iter().map(|p| print_pat(p, false));
|
||||
begin.chain(mid).chain(end).joined(", ", f)
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
//! Logic for transforming the raw code given by the user into something actually
|
||||
//! runnable, e.g. by adding a `main` function if it doesn't already exist.
|
||||
|
||||
use std::fmt::{self, Write as _};
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
|
@ -17,6 +18,7 @@ use rustc_span::symbol::sym;
|
|||
use tracing::debug;
|
||||
|
||||
use super::GlobalTestOptions;
|
||||
use crate::display::Joined as _;
|
||||
use crate::html::markdown::LangString;
|
||||
|
||||
/// This struct contains information about the doctest itself which is then used to generate
|
||||
|
|
@ -232,13 +234,15 @@ impl DocTestBuilder {
|
|||
|
||||
// add extra 4 spaces for each line to offset the code block
|
||||
if opts.insert_indent_space {
|
||||
prog.push_str(
|
||||
&everything_else
|
||||
write!(
|
||||
prog,
|
||||
"{}",
|
||||
fmt::from_fn(|f| everything_else
|
||||
.lines()
|
||||
.map(|line| format!(" {}", line))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n"),
|
||||
);
|
||||
.map(|line| fmt::from_fn(move |f| write!(f, " {line}")))
|
||||
.joined("\n", f))
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
prog.push_str(everything_else);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ use std::cmp::Ordering;
|
|||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
|
||||
use itertools::Itertools;
|
||||
use rinja::Template;
|
||||
use rustc_abi::VariantIdx;
|
||||
use rustc_data_structures::captures::Captures;
|
||||
|
|
@ -514,11 +513,7 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl
|
|||
class = myitem.type_(),
|
||||
unsafety_flag = unsafety_flag,
|
||||
href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
|
||||
title = [myitem.type_().to_string(), full_path(cx, myitem)]
|
||||
.iter()
|
||||
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
title = format_args!("{} {}", myitem.type_(), full_path(cx, myitem)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
@ -915,7 +910,7 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
|
|||
w,
|
||||
format_args!(
|
||||
"<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
|
||||
list.iter().join("`, `")
|
||||
fmt::from_fn(|f| list.iter().joined("`, `", f))
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
@ -1168,17 +1163,18 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
|
|||
js_src_path.extend(cx.current.iter().copied());
|
||||
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
|
||||
}
|
||||
let extern_crates = extern_crates
|
||||
.into_iter()
|
||||
.map(|cnum| tcx.crate_name(cnum).to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let (extern_before, extern_after) =
|
||||
if extern_crates.is_empty() { ("", "") } else { (" data-ignore-extern-crates=\"", "\"") };
|
||||
let extern_crates = fmt::from_fn(|f| {
|
||||
if !extern_crates.is_empty() {
|
||||
f.write_str(" data-ignore-extern-crates=\"")?;
|
||||
extern_crates.iter().map(|&cnum| tcx.crate_name(cnum)).joined(",", f)?;
|
||||
f.write_str("\"")?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
write_str(
|
||||
w,
|
||||
format_args!(
|
||||
"<script src=\"{src}\"{extern_before}{extern_crates}{extern_after} async></script>",
|
||||
"<script src=\"{src}\"{extern_crates} async></script>",
|
||||
src = js_src_path.finish()
|
||||
),
|
||||
);
|
||||
|
|
@ -1400,7 +1396,7 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
|
|||
.collect();
|
||||
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
|
||||
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
|
||||
let self_path = self_fqp.iter().map(Symbol::as_str).collect::<Vec<&str>>().join("::");
|
||||
let self_path = fmt::from_fn(|f| self_fqp.iter().joined("::", f));
|
||||
write_str(
|
||||
w,
|
||||
format_args!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue