Skip to content

Commit 15b2168

Browse files
Unify generation of primitive links for associated types with the rest
1 parent a1e1dba commit 15b2168

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed

src/librustdoc/html/format.rs

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,24 @@ pub(crate) fn anchor<'a, 'cx: 'a>(
958958
})
959959
}
960960

961+
fn fmt_slice<'cx>(
962+
f: &mut fmt::Formatter<'_>,
963+
t: &clean::Type,
964+
extra: &str,
965+
cx: &'cx Context<'_>,
966+
) -> fmt::Result {
967+
match *t {
968+
clean::Generic(name) => {
969+
primitive_link(f, PrimitiveType::Slice, &format!("{extra}[{name}]"), cx)
970+
}
971+
_ => {
972+
write!(f, "{extra}[")?;
973+
fmt::Display::fmt(&t.print(cx), f)?;
974+
f.write_str("]")
975+
}
976+
}
977+
}
978+
961979
fn fmt_type<'cx>(
962980
t: &clean::Type,
963981
f: &mut fmt::Formatter<'_>,
@@ -1008,55 +1026,25 @@ fn fmt_type<'cx>(
10081026
match &typs[..] {
10091027
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
10101028
[one] => {
1011-
if let clean::Generic(name) = one {
1012-
primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
1013-
} else {
1014-
write!(f, "(")?;
1015-
// Carry `f.alternate()` into this display w/o branching manually.
1016-
fmt::Display::fmt(&one.print(cx), f)?;
1017-
write!(f, ",)")
1018-
}
1029+
write!(f, "(")?;
1030+
// Carry `f.alternate()` into this display w/o branching manually.
1031+
fmt::Display::fmt(&one.print(cx), f)?;
1032+
write!(f, ",)")
10191033
}
10201034
many => {
1021-
let generic_names: Vec<Symbol> = many
1022-
.iter()
1023-
.filter_map(|t| match t {
1024-
clean::Generic(name) => Some(*name),
1025-
_ => None,
1026-
})
1027-
.collect();
1028-
let is_generic = generic_names.len() == many.len();
1029-
if is_generic {
1030-
primitive_link(
1031-
f,
1032-
PrimitiveType::Tuple,
1033-
&format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
1034-
cx,
1035-
)
1036-
} else {
1037-
write!(f, "(")?;
1038-
for (i, item) in many.iter().enumerate() {
1039-
if i != 0 {
1040-
write!(f, ", ")?;
1041-
}
1042-
// Carry `f.alternate()` into this display w/o branching manually.
1043-
fmt::Display::fmt(&item.print(cx), f)?;
1035+
write!(f, "(")?;
1036+
for (i, item) in many.iter().enumerate() {
1037+
if i != 0 {
1038+
write!(f, ", ")?;
10441039
}
1045-
write!(f, ")")
1040+
// Carry `f.alternate()` into this display w/o branching manually.
1041+
fmt::Display::fmt(&item.print(cx), f)?;
10461042
}
1043+
write!(f, ")")
10471044
}
10481045
}
10491046
}
1050-
clean::Slice(ref t) => match **t {
1051-
clean::Generic(name) => {
1052-
primitive_link(f, PrimitiveType::Slice, &format!("[{name}]"), cx)
1053-
}
1054-
_ => {
1055-
write!(f, "[")?;
1056-
fmt::Display::fmt(&t.print(cx), f)?;
1057-
write!(f, "]")
1058-
}
1059-
},
1047+
clean::Slice(ref t) => fmt_slice(f, &**t, "", cx),
10601048
clean::Array(ref t, ref n) => match **t {
10611049
clean::Generic(name) if !f.alternate() => primitive_link(
10621050
f,
@@ -1070,10 +1058,9 @@ fn fmt_type<'cx>(
10701058
if f.alternate() {
10711059
write!(f, "; {n}")?;
10721060
} else {
1073-
write!(f, "; ")?;
1074-
primitive_link(f, PrimitiveType::Array, &format!("{n}", n = Escape(n)), cx)?;
1061+
write!(f, "; {}", Escape(n))?;
10751062
}
1076-
write!(f, "]")
1063+
f.write_str("]")
10771064
}
10781065
},
10791066
clean::RawPointer(m, ref t) => {
@@ -1082,36 +1069,34 @@ fn fmt_type<'cx>(
10821069
hir::Mutability::Not => "const",
10831070
};
10841071

1085-
if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
1086-
let text = if f.alternate() {
1087-
format!("*{m} {ty:#}", ty = t.print(cx))
1088-
} else {
1089-
format!("*{m} {ty}", ty = t.print(cx))
1090-
};
1091-
primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
1092-
} else {
1093-
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{m} "), cx)?;
1094-
fmt::Display::fmt(&t.print(cx), f)
1095-
}
1072+
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{m} "), cx)?;
1073+
fmt::Display::fmt(&t.print(cx), f)
10961074
}
10971075
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
1098-
let lt = match l {
1076+
let lifetime = match l {
10991077
Some(l) => format!("{} ", l.print()),
11001078
_ => String::new(),
11011079
};
11021080
let m = mutability.print_with_space();
11031081
let amp = if f.alternate() { "&" } else { "&amp;" };
11041082

1105-
if let clean::Generic(name) = **ty {
1106-
return primitive_link(
1107-
f,
1108-
PrimitiveType::Reference,
1109-
&format!("{amp}{lt}{m}{name}"),
1110-
cx,
1111-
);
1083+
match **ty {
1084+
clean::Generic(name) => {
1085+
primitive_link(
1086+
f,
1087+
PrimitiveType::Reference,
1088+
&format!("{amp}{lifetime}{m}"),
1089+
cx,
1090+
)?;
1091+
return write!(f, "{name}");
1092+
}
1093+
clean::Slice(ref t) => {
1094+
return fmt_slice(f, &**t, &format!("{amp}{lifetime}{m}"), cx);
1095+
}
1096+
_ => {}
11121097
}
11131098

1114-
write!(f, "{amp}{lt}{m}")?;
1099+
write!(f, "{amp}{lifetime}{m}")?;
11151100

11161101
let needs_parens = match **ty {
11171102
clean::DynTrait(ref bounds, ref trait_lt)
@@ -1192,7 +1177,7 @@ fn fmt_type<'cx>(
11921177
write!(
11931178
f,
11941179
"<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
1195-
title=\"type {path}::{name}\">{name}</a>",
1180+
title=\"type {path}::{name}\">{name}</a>",
11961181
shortty = ItemType::AssocType,
11971182
name = assoc.name,
11981183
path = join_with_double_colon(&path),

0 commit comments

Comments
 (0)