Skip to content

Commit 3466d80

Browse files
Rollup merge of rust-lang#53941 - kzys:sort-impls, r=GuillaumeGomez
rustdoc: Sort implementors Fixes rust-lang#53812
2 parents f481987 + 2fe4503 commit 3466d80

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/librustdoc/html/render.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,17 +2301,21 @@ fn document_non_exhaustive(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::R
23012301
}
23022302

23032303
fn name_key(name: &str) -> (&str, u64, usize) {
2304+
let end = name.bytes()
2305+
.rposition(|b| b.is_ascii_digit()).map_or(name.len(), |i| i + 1);
2306+
23042307
// find number at end
2305-
let split = name.bytes().rposition(|b| b < b'0' || b'9' < b).map_or(0, |s| s + 1);
2308+
let split = name[0..end].bytes()
2309+
.rposition(|b| !b.is_ascii_digit()).map_or(0, |i| i + 1);
23062310

23072311
// count leading zeroes
23082312
let after_zeroes =
2309-
name[split..].bytes().position(|b| b != b'0').map_or(name.len(), |extra| split + extra);
2313+
name[split..end].bytes().position(|b| b != b'0').map_or(name.len(), |extra| split + extra);
23102314

23112315
// sort leading zeroes last
23122316
let num_zeroes = after_zeroes - split;
23132317

2314-
match name[split..].parse() {
2318+
match name[split..end].parse() {
23152319
Ok(n) => (&name[..split], n, num_zeroes),
23162320
Err(_) => (name, 0, num_zeroes),
23172321
}
@@ -2702,6 +2706,14 @@ fn bounds(t_bounds: &[clean::GenericBound]) -> String {
27022706
bounds
27032707
}
27042708

2709+
fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl) -> Ordering {
2710+
let lhs = format!("{}", lhs.inner_impl());
2711+
let rhs = format!("{}", rhs.inner_impl());
2712+
2713+
// lhs and rhs are formatted as HTML, which may be unnecessary
2714+
name_key(&lhs).cmp(&name_key(&rhs))
2715+
}
2716+
27052717
fn item_trait(
27062718
w: &mut fmt::Formatter,
27072719
cx: &Context,
@@ -2905,9 +2917,12 @@ fn item_trait(
29052917
.map_or(true, |d| cache.paths.contains_key(&d)));
29062918

29072919

2908-
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter()
2920+
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter()
29092921
.partition(|i| i.inner_impl().synthetic);
29102922

2923+
synthetic.sort_by(compare_impl);
2924+
concrete.sort_by(compare_impl);
2925+
29112926
if !foreign.is_empty() {
29122927
write!(w, "
29132928
<h2 id='foreign-impls' class='small-section-header'>
@@ -4716,6 +4731,7 @@ fn test_name_sorting() {
47164731
"Fruit1", "Fruit01",
47174732
"Fruit2", "Fruit02",
47184733
"Fruit20",
4734+
"Fruit30x",
47194735
"Fruit100",
47204736
"Pear"];
47214737
let mut sorted = names.to_owned();

src/test/rustdoc/issue-53812.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub trait MyIterator {
12+
}
13+
14+
pub struct MyStruct<T>(T);
15+
16+
macro_rules! array_impls {
17+
($($N:expr)+) => {
18+
$(
19+
impl<'a, T> MyIterator for &'a MyStruct<[T; $N]> {
20+
}
21+
)+
22+
}
23+
}
24+
25+
// @has issue_53812/trait.MyIterator.html '//*[@id="implementors-list"]//h3[1]' 'MyStruct<[T; 0]>'
26+
// @has - '//*[@id="implementors-list"]//h3[2]' 'MyStruct<[T; 1]>'
27+
// @has - '//*[@id="implementors-list"]//h3[3]' 'MyStruct<[T; 2]>'
28+
// @has - '//*[@id="implementors-list"]//h3[4]' 'MyStruct<[T; 3]>'
29+
// @has - '//*[@id="implementors-list"]//h3[5]' 'MyStruct<[T; 10]>'
30+
array_impls! { 10 3 2 1 0 }

0 commit comments

Comments
 (0)