Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Sort implementors #53941

Merged
merged 3 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,17 +2300,21 @@ fn document_non_exhaustive(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::R
}

fn name_key(name: &str) -> (&str, u64, usize) {
let end = name.bytes()
.rposition(|b| b.is_ascii_digit()).map_or(name.len(), |i| i + 1);

// find number at end
let split = name.bytes().rposition(|b| b < b'0' || b'9' < b).map_or(0, |s| s + 1);
let split = name[0..end].bytes()
.rposition(|b| !b.is_ascii_digit()).map_or(0, |i| i + 1);

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

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

match name[split..].parse() {
match name[split..end].parse() {
Ok(n) => (&name[..split], n, num_zeroes),
Err(_) => (name, 0, num_zeroes),
}
Expand Down Expand Up @@ -2701,6 +2705,14 @@ fn bounds(t_bounds: &[clean::GenericBound]) -> String {
bounds
}

fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl) -> Ordering {
let lhs = format!("{}", lhs.inner_impl());
let rhs = format!("{}", rhs.inner_impl());

// lhs and rhs are formatted as HTML, which may be unnecessary
name_key(&lhs).cmp(&name_key(&rhs))
}

fn item_trait(
w: &mut fmt::Formatter,
cx: &Context,
Expand Down Expand Up @@ -2904,9 +2916,12 @@ fn item_trait(
.map_or(true, |d| cache.paths.contains_key(&d)));


let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter()
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter()
.partition(|i| i.inner_impl().synthetic);

synthetic.sort_by(compare_impl);
concrete.sort_by(compare_impl);

if !foreign.is_empty() {
write!(w, "
<h2 id='foreign-impls' class='small-section-header'>
Expand Down Expand Up @@ -4715,6 +4730,7 @@ fn test_name_sorting() {
"Fruit1", "Fruit01",
"Fruit2", "Fruit02",
"Fruit20",
"Fruit30x",
"Fruit100",
"Pear"];
let mut sorted = names.to_owned();
Expand Down
26 changes: 26 additions & 0 deletions src/test/rustdoc/issue-53812.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub trait MyIterator {
}

pub struct MyStruct<T>(T);

macro_rules! array_impls {
($($N:expr)+) => {
$(
impl<'a, T> MyIterator for &'a MyStruct<[T; $N]> {
}
)+
}
}

// @has issue_53812/trait.MyIterator.html '//*[@id="implementors-list"]//h3[1]' 'MyStruct<[T; 0]>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check h3[2], h3[3] and h3[4] too. Like that we're sure it's not just luck.

array_impls! { 10 3 2 1 0 }