Skip to content

Commit d97e822

Browse files
committed
rustdoc: trait bound formatting
1 parent 8681d4c commit d97e822

9 files changed

+53
-19
lines changed

src/librustdoc/html/render/print_item.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
660660
if count_consts != 0 && count_methods != 0 {
661661
w.write_str("\n");
662662
}
663+
664+
if !required_methods.is_empty() {
665+
write!(w, " # Required method{}\n", pluralize(required_methods.len()));
666+
}
663667
for (pos, m) in required_methods.iter().enumerate() {
664668
render_assoc_item(
665669
w,
@@ -678,6 +682,10 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
678682
if !required_methods.is_empty() && !provided_methods.is_empty() {
679683
w.write_str("\n");
680684
}
685+
686+
if !provided_methods.is_empty() {
687+
write!(w, " # Provided method{}\n", pluralize(provided_methods.len()));
688+
}
681689
for (pos, m) in provided_methods.iter().enumerate() {
682690
render_assoc_item(
683691
w,
@@ -687,16 +695,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
687695
cx,
688696
RenderMode::Normal,
689697
);
690-
match *m.kind {
691-
clean::MethodItem(ref inner, _)
692-
if !inner.generics.where_predicates.is_empty() =>
693-
{
694-
w.write_str(",\n { ... }\n");
695-
}
696-
_ => {
697-
w.write_str(" { ... }\n");
698-
}
699-
}
698+
699+
w.write_str(";\n");
700700

701701
if pos < provided_methods.len() - 1 {
702702
w.write_str("<span class=\"item-spacer\"></span>");

src/test/rustdoc/auxiliary/inline-default-methods.rs

+12
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@ pub trait Foo {
44
fn bar(&self);
55
fn foo(&mut self) {}
66
}
7+
8+
pub trait Bar {
9+
fn bar(&self);
10+
fn foo1(&mut self) {}
11+
fn foo2(&mut self) {}
12+
}
13+
14+
pub trait Baz {
15+
fn bar1(&self);
16+
fn bar2(&self);
17+
fn foo(&mut self) {}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<code>pub trait Write {
2+
# Required methods
23
fn <a href="#tymethod.poll_write" class="fnname">poll_write</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;buf: &amp;mut [<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
34
<span class="item-spacer" /> fn <a href="#tymethod.poll_flush" class="fnname">poll_flush</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
45
<span class="item-spacer" /> fn <a href="#tymethod.poll_close" class="fnname">poll_close</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
56

6-
fn <a href="#method.poll_write_vectored" class="fnname">poll_write_vectored</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bufs: &amp;[<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt; { ... }
7+
# Provided method
8+
fn <a href="#method.poll_write_vectored" class="fnname">poll_write_vectored</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bufs: &amp;[<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
79
}</code>

src/test/rustdoc/inline-default-methods.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
extern crate inline_default_methods;
55

66
// @has inline_default_methods/trait.Foo.html
7-
// @has - '//*[@class="rust trait"]' 'fn bar(&self);'
8-
// @has - '//*[@class="rust trait"]' 'fn foo(&mut self) { ... }'
7+
// @has - '//*[@class="rust trait"]' '# Required method fn bar(&self);'
8+
// @has - '//*[@class="rust trait"]' '# Provided method fn foo(&mut self);'
99
pub use inline_default_methods::Foo;
10+
11+
// @has inline_default_methods/trait.Bar.html
12+
// @has - '//*[@class="rust trait"]' '# Required method fn bar(&self);'
13+
// @has - '//*[@class="rust trait"]' '# Provided methods fn foo1(&mut self);'
14+
// @has - '//*[@class="rust trait"]' 'fn foo2(&mut self);'
15+
pub use inline_default_methods::Bar;
16+
17+
// @has inline_default_methods/trait.Baz.html
18+
// @has - '//*[@class="rust trait"]' '# Required methods fn bar1(&self);'
19+
// @has - '//*[@class="rust trait"]' 'fn bar2(&self);'
20+
// @has - '//*[@class="rust trait"]' '# Provided method fn foo(&mut self);'
21+
pub use inline_default_methods::Baz;

src/test/rustdoc/issue-85454.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern crate issue_85454;
66

77
// @has foo/trait.FromResidual.html
8-
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
8+
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { # Required method fn from_residual(residual: R) -> Self; }'
99
pub trait FromResidual<R = <Self as Try>::Residual> {
1010
fn from_residual(residual: R) -> Self;
1111
}
@@ -24,6 +24,6 @@ pub enum ControlFlow<B, C = ()> {
2424

2525
pub mod reexport {
2626
// @has foo/reexport/trait.FromResidual.html
27-
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
27+
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { # Required method fn from_residual(residual: R) -> Self; }'
2828
pub use issue_85454::*;
2929
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="item-decl"><pre class="rust trait"><code>pub trait TraitWhere {
22
type <a href="#associatedtype.Item" class="associatedtype">Item</a>&lt;'a&gt;<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: 'a</span>;
33

4-
fn <a href="#method.func" class="fnname">func</a>(self)<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
5-
{ ... }
6-
<span class="item-spacer" /> fn <a href="#method.lines" class="fnname">lines</a>(self) -&gt; <a class="struct" href="{{channel}}/std/io/struct.Lines.html" title="struct std::io::Lines">Lines</a>&lt;Self&gt;<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
7-
{ ... }
4+
# Provided methods
5+
fn <a href="#method.func" class="fnname">func</a>(self)<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
6+
<span class="item-spacer" /> fn <a href="#method.lines" class="fnname">lines</a>(self) -&gt; <a class="struct" href="{{channel}}/std/io/struct.Lines.html" title="struct std::io::Lines">Lines</a>&lt;Self&gt;<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
7+
<span class="item-spacer" /> fn <a href="#method.merge" class="fnname">merge</a>&lt;T&gt;(self, a: T)<br />&#160;&#160;&#160;&#160;<span class="where">where<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;T: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
88
}</code></pre></div>

src/test/rustdoc/where.rs

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ pub trait TraitWhere {
4141
where
4242
Self: Sized,
4343
{ todo!() }
44+
45+
fn merge<T>(self, a: T)
46+
where
47+
Self: Sized,
48+
T: Sized,
49+
{ todo!() }
4450
}
4551

4652
// @has foo/struct.Echo.html '//*[@class="impl has-srclink"]//h3[@class="code-header"]' \
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="item-decl"><pre class="rust trait"><code>pub trait ToOwned&lt;T&gt;<span class="where fmt-newline">where<br />&#160;&#160;&#160;&#160;T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span>{
22
type <a href="#associatedtype.Owned" class="associatedtype">Owned</a>;
33

4+
# Required methods
45
fn <a href="#tymethod.to_owned" class="fnname">to_owned</a>(&amp;self) -&gt; Self::<a class="associatedtype" href="trait.ToOwned.html#associatedtype.Owned" title="type foo::ToOwned::Owned">Owned</a>;
56
<span class="item-spacer" /> fn <a href="#tymethod.whatever" class="fnname">whatever</a>(&amp;self) -&gt; T;
67
}</code></pre></div>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="item-decl"><pre class="rust trait"><code>pub trait ToOwned2&lt;T:&#160;<a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; {
22
type <a href="#associatedtype.Owned" class="associatedtype">Owned</a>;
33

4+
# Required methods
45
fn <a href="#tymethod.to_owned" class="fnname">to_owned</a>(&amp;self) -&gt; Self::<a class="associatedtype" href="trait.ToOwned2.html#associatedtype.Owned" title="type foo::ToOwned2::Owned">Owned</a>;
56
<span class="item-spacer" /> fn <a href="#tymethod.whatever" class="fnname">whatever</a>(&amp;self) -&gt; T;
67
}</code></pre></div>

0 commit comments

Comments
 (0)