Skip to content

Commit bc0c816

Browse files
authored
Rollup merge of rust-lang#106855 - klensy:rd-s, r=notriddle
rustdoc: few small cleanups
2 parents 9db8e6d + 8998bd3 commit bc0c816

File tree

7 files changed

+33
-28
lines changed

7 files changed

+33
-28
lines changed

src/librustdoc/formats/cache.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
242242
}
243243

244244
// Index this method for searching later on.
245-
if let Some(ref s) = item.name.or_else(|| {
245+
if let Some(s) = item.name.or_else(|| {
246246
if item.is_stripped() {
247247
None
248248
} else if let clean::ImportItem(ref i) = *item.kind &&
@@ -317,14 +317,15 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
317317
short_markdown_summary(x.as_str(), &item.link_names(self.cache))
318318
});
319319
let ty = item.type_();
320-
let name = s.to_string();
321-
if ty != ItemType::StructField || u16::from_str_radix(&name, 10).is_err() {
320+
if ty != ItemType::StructField
321+
|| u16::from_str_radix(s.as_str(), 10).is_err()
322+
{
322323
// In case this is a field from a tuple struct, we don't add it into
323324
// the search index because its name is something like "0", which is
324325
// not useful for rustdoc search.
325326
self.cache.search_index.push(IndexItem {
326327
ty,
327-
name,
328+
name: s,
328329
path: join_with_double_colon(path),
329330
desc,
330331
parent,

src/librustdoc/html/format.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ fn generate_macro_def_id_path(
569569
root_path: Option<&str>,
570570
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
571571
let tcx = cx.shared.tcx;
572-
let crate_name = tcx.crate_name(def_id.krate).to_string();
572+
let crate_name = tcx.crate_name(def_id.krate);
573573
let cache = cx.cache();
574574

575575
let fqp: Vec<Symbol> = tcx
@@ -584,7 +584,7 @@ fn generate_macro_def_id_path(
584584
}
585585
})
586586
.collect();
587-
let mut relative = fqp.iter().map(|elem| elem.to_string());
587+
let mut relative = fqp.iter().copied();
588588
let cstore = CStore::from_tcx(tcx);
589589
// We need this to prevent a `panic` when this function is used from intra doc links...
590590
if !cstore.has_crate_data(def_id.krate) {
@@ -602,9 +602,9 @@ fn generate_macro_def_id_path(
602602
};
603603

604604
let mut path = if is_macro_2 {
605-
once(crate_name.clone()).chain(relative).collect()
605+
once(crate_name).chain(relative).collect()
606606
} else {
607-
vec![crate_name.clone(), relative.next_back().unwrap()]
607+
vec![crate_name, relative.next_back().unwrap()]
608608
};
609609
if path.len() < 2 {
610610
// The minimum we can have is the crate name followed by the macro name. If shorter, then
@@ -614,17 +614,22 @@ fn generate_macro_def_id_path(
614614
}
615615

616616
if let Some(last) = path.last_mut() {
617-
*last = format!("macro.{}.html", last);
617+
*last = Symbol::intern(&format!("macro.{}.html", last.as_str()));
618618
}
619619

620620
let url = match cache.extern_locations[&def_id.krate] {
621621
ExternalLocation::Remote(ref s) => {
622622
// `ExternalLocation::Remote` always end with a `/`.
623-
format!("{}{}", s, path.join("/"))
623+
format!("{}{}", s, path.iter().map(|p| p.as_str()).join("/"))
624624
}
625625
ExternalLocation::Local => {
626626
// `root_path` always end with a `/`.
627-
format!("{}{}/{}", root_path.unwrap_or(""), crate_name, path.join("/"))
627+
format!(
628+
"{}{}/{}",
629+
root_path.unwrap_or(""),
630+
crate_name,
631+
path.iter().map(|p| p.as_str()).join("/")
632+
)
628633
}
629634
ExternalLocation::Unknown => {
630635
debug!("crate {} not in cache when linkifying macros", crate_name);
@@ -1050,7 +1055,7 @@ fn fmt_type<'cx>(
10501055
_ => String::new(),
10511056
};
10521057
let m = mutability.print_with_space();
1053-
let amp = if f.alternate() { "&".to_string() } else { "&amp;".to_string() };
1058+
let amp = if f.alternate() { "&" } else { "&amp;" };
10541059
match **ty {
10551060
clean::DynTrait(ref bounds, ref trait_lt)
10561061
if bounds.len() > 1 || trait_lt.is_some() =>

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_hir::def_id::DefId;
3030
use rustc_hir::HirId;
3131
use rustc_middle::ty::TyCtxt;
3232
use rustc_span::edition::Edition;
33-
use rustc_span::Span;
33+
use rustc_span::{Span, Symbol};
3434

3535
use once_cell::sync::Lazy;
3636
use std::borrow::Cow;
@@ -198,7 +198,7 @@ fn slugify(c: char) -> Option<char> {
198198

199199
#[derive(Clone, Debug)]
200200
pub struct Playground {
201-
pub crate_name: Option<String>,
201+
pub crate_name: Option<Symbol>,
202202
pub url: String,
203203
}
204204

@@ -290,7 +290,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
290290
.map(|l| map_line(l).for_code())
291291
.intersperse("\n".into())
292292
.collect::<String>();
293-
let krate = krate.as_ref().map(|s| &**s);
293+
let krate = krate.as_ref().map(|s| s.as_str());
294294
let (test, _, _) =
295295
doctest::make_test(&test, krate, false, &Default::default(), edition, None);
296296
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };

src/librustdoc/html/render/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
464464
// If user passed in `--playground-url` arg, we fill in crate name here
465465
let mut playground = None;
466466
if let Some(url) = playground_url {
467-
playground =
468-
Some(markdown::Playground { crate_name: Some(krate.name(tcx).to_string()), url });
467+
playground = Some(markdown::Playground { crate_name: Some(krate.name(tcx)), url });
469468
}
470469
let mut layout = layout::Layout {
471470
logo: String::new(),
@@ -491,7 +490,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
491490
}
492491
(sym::html_playground_url, Some(s)) => {
493492
playground = Some(markdown::Playground {
494-
crate_name: Some(krate.name(tcx).to_string()),
493+
crate_name: Some(krate.name(tcx)),
495494
url: s.to_string(),
496495
});
497496
}

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
100100
#[derive(Debug)]
101101
pub(crate) struct IndexItem {
102102
pub(crate) ty: ItemType,
103-
pub(crate) name: String,
103+
pub(crate) name: Symbol,
104104
pub(crate) path: String,
105105
pub(crate) desc: String,
106106
pub(crate) parent: Option<DefId>,
@@ -2769,8 +2769,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
27692769
let mut work = VecDeque::new();
27702770

27712771
let mut process_path = |did: DefId| {
2772-
let get_extern = || cache.external_paths.get(&did).map(|s| s.0.clone());
2773-
let fqp = cache.exact_paths.get(&did).cloned().or_else(get_extern);
2772+
let get_extern = || cache.external_paths.get(&did).map(|s| &s.0);
2773+
let fqp = cache.exact_paths.get(&did).or_else(get_extern);
27742774

27752775
if let Some(path) = fqp {
27762776
out.push(join_with_double_colon(&path));

src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
10271027
.chain(std::iter::once("implementors"))
10281028
.collect();
10291029
if let Some(did) = it.item_id.as_def_id() &&
1030-
let get_extern = { || cache.external_paths.get(&did).map(|s| s.0.clone()) } &&
1031-
let Some(fqp) = cache.exact_paths.get(&did).cloned().or_else(get_extern) {
1030+
let get_extern = { || cache.external_paths.get(&did).map(|s| &s.0) } &&
1031+
let Some(fqp) = cache.exact_paths.get(&did).or_else(get_extern) {
10321032
js_src_path.extend(fqp[..fqp.len() - 1].iter().copied());
10331033
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap()));
10341034
} else {

src/librustdoc/html/render/search_index.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn build_index<'tcx>(
3535
.map_or_else(String::new, |s| short_markdown_summary(&s, &item.link_names(cache)));
3636
cache.search_index.push(IndexItem {
3737
ty: item.type_(),
38-
name: item.name.unwrap().to_string(),
38+
name: item.name.unwrap(),
3939
path: join_with_double_colon(&fqp[..fqp.len() - 1]),
4040
desc,
4141
parent: Some(parent),
@@ -58,8 +58,8 @@ pub(crate) fn build_index<'tcx>(
5858
// Sort search index items. This improves the compressibility of the search index.
5959
cache.search_index.sort_unstable_by(|k1, k2| {
6060
// `sort_unstable_by_key` produces lifetime errors
61-
let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent);
62-
let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent);
61+
let k1 = (&k1.path, k1.name.as_str(), &k1.ty, &k1.parent);
62+
let k2 = (&k2.path, k2.name.as_str(), &k2.ty, &k2.parent);
6363
std::cmp::Ord::cmp(&k1, &k2)
6464
});
6565

@@ -240,7 +240,7 @@ pub(crate) fn build_index<'tcx>(
240240
)?;
241241
crate_data.serialize_field(
242242
"n",
243-
&self.items.iter().map(|item| &item.name).collect::<Vec<_>>(),
243+
&self.items.iter().map(|item| item.name.as_str()).collect::<Vec<_>>(),
244244
)?;
245245
crate_data.serialize_field(
246246
"q",
@@ -299,7 +299,7 @@ pub(crate) fn build_index<'tcx>(
299299
)?;
300300
crate_data.serialize_field(
301301
"p",
302-
&self.paths.iter().map(|(it, s)| (it, s.to_string())).collect::<Vec<_>>(),
302+
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(),
303303
)?;
304304
if has_aliases {
305305
crate_data.serialize_field("a", &self.aliases)?;

0 commit comments

Comments
 (0)