Skip to content

Commit 3819cf7

Browse files
committed
rustdoc: templatize assoc_method and item_function
1 parent 5bfad69 commit 3819cf7

20 files changed

+102
-102
lines changed

src/librustdoc/html/format.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ impl Buffer {
140140
self.for_html
141141
}
142142

143-
pub(crate) fn reserve(&mut self, additional: usize) {
144-
self.buffer.reserve(additional)
145-
}
146-
147143
pub(crate) fn len(&self) -> usize {
148144
self.buffer.len()
149145
}
@@ -1268,7 +1264,7 @@ impl std::fmt::Write for WriteCounter {
12681264
}
12691265
}
12701266

1271-
// Implements Display by emitting the given number of spaces.
1267+
/// Implements Display by emitting the given number of spaces.
12721268
struct Indent(usize);
12731269

12741270
impl fmt::Display for Indent {
@@ -1297,28 +1293,24 @@ impl clean::FnDecl {
12971293
})
12981294
}
12991295

1300-
/// * `header_len`: The length of the function header and name. In other words, the number of
1301-
/// characters in the function declaration up to but not including the parentheses.
1302-
/// This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines
1303-
/// are preserved.
1296+
/// * `header`: Prints the function header and name (everything before the params).
13041297
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
13051298
/// necessary.
13061299
pub(crate) fn full_print<'a, 'tcx: 'a>(
13071300
&'a self,
1308-
header_len: usize,
1301+
header: impl fmt::Display + 'a,
13091302
indent: usize,
13101303
cx: &'a Context<'tcx>,
13111304
) -> impl fmt::Display + 'a + Captures<'tcx> {
13121305
display_fn(move |f| {
13131306
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
1307+
let header = Plain(header);
1308+
let decl = Plain(display_fn(|f| self.inner_full_print(None, f, cx)));
13141309
let mut counter = WriteCounter(0);
1315-
write!(&mut counter, "{:#}", display_fn(|f| { self.inner_full_print(None, f, cx) }))
1316-
.unwrap();
1310+
write!(&mut counter, "{header}{decl}")?;
13171311
// If the text form was over 80 characters wide, we will line-wrap our output.
1318-
let line_wrapping_indent =
1319-
if header_len + counter.0 > 80 { Some(indent) } else { None };
1320-
// Generate the final output. This happens to accept `{:#}` formatting to get textual
1321-
// output but in practice it is only formatted with `{}` to get HTML output.
1312+
let line_wrapping_indent = (counter.0 > 80).then_some(indent);
1313+
// Generate the final output.
13221314
self.inner_full_print(line_wrapping_indent, f, cx)
13231315
})
13241316
}
@@ -1582,12 +1574,9 @@ impl clean::TypeBinding {
15821574
}
15831575

15841576
pub(crate) fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
1585-
display_fn(move |f| {
1586-
let quot = "&quot;";
1587-
match abi {
1588-
Abi::Rust => Ok(()),
1589-
abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1590-
}
1577+
display_fn(move |f| match abi {
1578+
Abi::Rust => Ok(()),
1579+
abi => write!(f, "extern \"{}\" ", abi.name()),
15911580
})
15921581
}
15931582

src/librustdoc/html/render/mod.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ fn assoc_method(
809809
) {
810810
let tcx = cx.tcx();
811811
let header = meth.fn_header(tcx).expect("Trying to get header from a non-function item");
812-
let name = meth.name.as_ref().unwrap();
812+
let name = meth.name.as_ref().unwrap().as_str();
813813
let vis = visibility_print_with_space(meth.visibility(tcx), meth.item_id, cx).to_string();
814814
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
815815
// this condition.
@@ -825,48 +825,53 @@ fn assoc_method(
825825
let abi = print_abi_with_space(header.abi).to_string();
826826
let href = assoc_href_attr(meth, link, cx);
827827

828-
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
829-
let generics_len = format!("{:#}", g.print(cx)).len();
830-
let mut header_len = "fn ".len()
831-
+ vis.len()
832-
+ constness.len()
833-
+ asyncness.len()
834-
+ unsafety.len()
835-
+ defaultness.len()
836-
+ abi.len()
837-
+ name.as_str().len()
838-
+ generics_len;
839-
840-
let notable_traits = d.output.as_return().and_then(|output| notable_traits_button(output, cx));
828+
let notable_traits = d
829+
.output
830+
.as_return()
831+
.and_then(|output| notable_traits_button(output, cx))
832+
.unwrap_or_default();
841833

842834
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
843-
header_len += 4;
844835
let indent_str = " ";
845836
render_attributes_in_pre(w, meth, indent_str);
846837
(4, indent_str, Ending::NoNewline)
847838
} else {
848839
render_attributes_in_code(w, meth);
849840
(0, "", Ending::Newline)
850841
};
851-
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
852-
write!(
853-
w,
854-
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn <a{href} class=\"fn\">{name}</a>\
855-
{generics}{decl}{notable_traits}{where_clause}",
856-
indent = indent_str,
857-
vis = vis,
858-
constness = constness,
859-
asyncness = asyncness,
860-
unsafety = unsafety,
861-
defaultness = defaultness,
862-
abi = abi,
863-
href = href,
864-
name = name,
865-
generics = g.print(cx),
866-
decl = d.full_print(header_len, indent, cx),
867-
notable_traits = notable_traits.unwrap_or_default(),
868-
where_clause = print_where_clause(g, cx, indent, end_newline),
869-
);
842+
843+
let fn_header = FunctionHeader {
844+
indent_str,
845+
vis,
846+
constness,
847+
asyncness,
848+
unsafety,
849+
defaultness,
850+
abi,
851+
href,
852+
name,
853+
generics: g.print(cx).to_string(),
854+
};
855+
856+
let decl = d.full_print(&fn_header, indent, cx);
857+
let where_clause = print_where_clause(g, cx, indent, end_newline);
858+
859+
write!(w, "{fn_header}{decl}{notable_traits}{where_clause}");
860+
}
861+
862+
#[derive(Template)]
863+
#[template(path = "function_header.html")]
864+
struct FunctionHeader<'a> {
865+
indent_str: &'static str,
866+
vis: String,
867+
constness: &'a str,
868+
asyncness: &'a str,
869+
unsafety: &'a str,
870+
defaultness: &'a str,
871+
abi: String,
872+
href: String,
873+
name: &'a str,
874+
generics: String,
870875
}
871876

872877
/// Writes a span containing the versions at which an item became stable and/or const-stable. For

src/librustdoc/html/render/print_item.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -528,39 +528,34 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
528528
let abi = print_abi_with_space(header.abi).to_string();
529529
let asyncness = header.asyncness.print_with_space();
530530
let visibility = visibility_print_with_space(it.visibility(tcx), it.item_id, cx).to_string();
531-
let name = it.name.unwrap();
532-
533-
let generics_len = format!("{:#}", f.generics.print(cx)).len();
534-
let header_len = "fn ".len()
535-
+ visibility.len()
536-
+ constness.len()
537-
+ asyncness.len()
538-
+ unsafety.len()
539-
+ abi.len()
540-
+ name.as_str().len()
541-
+ generics_len;
531+
let name = it.name.as_ref().unwrap().as_str();
532+
533+
let fn_header = super::FunctionHeader {
534+
indent_str: "",
535+
vis: visibility,
536+
constness,
537+
asyncness,
538+
unsafety,
539+
// standalone functions are never default, only associated functions.
540+
defaultness: "",
541+
abi,
542+
href: "".to_string(),
543+
name,
544+
generics: f.generics.print(cx).to_string(),
545+
};
542546

543-
let notable_traits =
544-
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));
547+
let notable_traits = f
548+
.decl
549+
.output
550+
.as_return()
551+
.and_then(|output| notable_traits_button(output, cx))
552+
.unwrap_or_default();
545553

546554
wrap_item(w, |w| {
547555
render_attributes_in_pre(w, it, "");
548-
w.reserve(header_len);
549-
write!(
550-
w,
551-
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
552-
{name}{generics}{decl}{notable_traits}{where_clause}",
553-
vis = visibility,
554-
constness = constness,
555-
asyncness = asyncness,
556-
unsafety = unsafety,
557-
abi = abi,
558-
name = name,
559-
generics = f.generics.print(cx),
560-
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
561-
decl = f.decl.full_print(header_len, 0, cx),
562-
notable_traits = notable_traits.unwrap_or_default(),
563-
);
556+
let decl = f.decl.full_print(&fn_header, 0, cx);
557+
let where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline);
558+
write!(w, "{fn_header}{decl}{notable_traits}{where_clause}");
564559
});
565560
document(w, cx, it, None, HeadingOffset::H2);
566561
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{# All of the parts of a function or method declaration that
2+
come before the parameters and return value #}
3+
{{indent_str}}
4+
{{vis|safe}}
5+
{{constness}}
6+
{{asyncness}}
7+
{{unsafety}}
8+
{{defaultness}}
9+
{{abi|safe}}
10+
fn <a{{href|safe +}} class="fn">{{name}}</a>
11+
{{generics|safe}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn delta&lt;T&gt;() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a>&gt;</code>
1+
<code>pub fn <a class="fn">delta</a>&lt;T&gt;() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a>&gt;</code>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn gamma() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;[<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]&gt;</code>
1+
<code>pub fn <a class="fn">gamma</a>() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;[<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]&gt;</code>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a></code>
1+
<code>pub fn <a class="fn">beta</a>&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a></code>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn alpha() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]</code>
1+
<code>pub fn <a class="fn">alpha</a>() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]</code>

tests/rustdoc/nested-modules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ mod a_module {
77

88
pub mod a_nested_module {
99
// @has aCrate/a_nested_module/index.html '//a[@href="fn.a_nested_public_function.html"]' 'a_nested_public_function'
10-
// @hasraw aCrate/a_nested_module/fn.a_nested_public_function.html 'pub fn a_nested_public_function()'
10+
// @has aCrate/a_nested_module/fn.a_nested_public_function.html '//' 'pub fn a_nested_public_function()'
1111
pub fn a_nested_public_function() {}
1212

1313
// @has aCrate/a_nested_module/index.html '//a[@href="fn.another_nested_public_function.html"]' 'another_nested_public_function'
14-
// @hasraw aCrate/a_nested_module/fn.another_nested_public_function.html 'pub fn another_nested_public_function()'
14+
// @has aCrate/a_nested_module/fn.another_nested_public_function.html '//' 'pub fn another_nested_public_function()'
1515
pub use a_nested_module::a_nested_public_function as another_nested_public_function;
1616
}
1717

tests/rustdoc/reexports-priv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub mod outer {
9898
pub use reexports::foo;
9999
// @has 'foo/outer/inner/fn.foo_crate.html' '//pre[@class="rust item-decl"]' 'pub(crate) fn foo_crate()'
100100
pub(crate) use reexports::foo_crate;
101-
// @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super( )'
101+
// @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super()'
102102
pub(super) use::reexports::foo_super;
103103
// @!has 'foo/outer/inner/fn.foo_self.html'
104104
pub(self) use reexports::foo_self;
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn delta&lt;T&gt;() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.slice.html">[T]</a>&gt;</code>
1+
<code>pub fn <a class="fn">delta</a>&lt;T&gt;() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.slice.html">[T]</a>&gt;</code>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn gamma() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;[<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]&gt;</code>
1+
<code>pub fn <a class="fn">gamma</a>() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;[<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]&gt;</code>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.slice.html">[T]</a></code>
1+
<code>pub fn <a class="fn">beta</a>&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.slice.html">[T]</a></code>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn alpha() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]</code>
1+
<code>pub fn <a class="fn">alpha</a>() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]</code>

tests/rustdoc/tuples.link1_i32.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple1(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)</code>
1+
<code>pub fn <a class="fn">tuple1</a>(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)</code>

tests/rustdoc/tuples.link1_t.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple1_t&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a></code>
1+
<code>pub fn <a class="fn">tuple1_t</a>&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a></code>

tests/rustdoc/tuples.link2_i32.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple2(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)</code>
1+
<code>pub fn <a class="fn">tuple2</a>(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)</code>

tests/rustdoc/tuples.link2_t.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple2_t&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a></code>
1+
<code>pub fn <a class="fn">tuple2_t</a>&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a></code>

tests/rustdoc/tuples.link2_tu.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple2_tu&lt;T, U&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a></code>
1+
<code>pub fn <a class="fn">tuple2_tu</a>&lt;T, U&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a></code>

tests/rustdoc/tuples.link_unit.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<code>pub fn tuple0(x: <a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>)</code>
1+
<code>pub fn <a class="fn">tuple0</a>(x: <a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>)</code>

0 commit comments

Comments
 (0)