Skip to content

Commit

Permalink
rustdoc: Sort negative impls to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Nov 27, 2020
1 parent 72da5a9 commit 1bfb64b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ use rustc_span::symbol::{sym, Symbol};
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};

use crate::clean::{self, AttributesExt, Deprecation, GetDefId, RenderedLink, SelfTy, TypeKind};
use crate::clean::{
self, AttributesExt, Deprecation, GetDefId, ImplPolarity, RenderedLink, SelfTy, TypeKind,
};
use crate::config::{RenderInfo, RenderOptions};
use crate::docfs::{DocFS, PathError};
use crate::doctree;
Expand Down Expand Up @@ -2532,6 +2534,16 @@ fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl) -> Ordering {
compare_names(&lhs, &rhs)
}

fn compare_impl_polarity(a: &Impl, b: &Impl) -> Ordering {
match (a.inner_impl().polarity.as_ref(), b.inner_impl().polarity.as_ref()) {
(Some(ImplPolarity::Positive), Some(ImplPolarity::Negative)) => Ordering::Greater,
(Some(ImplPolarity::Negative), Some(ImplPolarity::Positive)) => Ordering::Less,
(Some(ImplPolarity::Positive), Some(ImplPolarity::Positive))
| (Some(ImplPolarity::Negative), Some(ImplPolarity::Negative)) => Ordering::Equal,
(None, _) | (_, None) => Ordering::Equal,
}
}

fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, cache: &Cache) {
let bounds = bounds(&t.bounds, false);
let types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
Expand Down Expand Up @@ -2718,6 +2730,10 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
}
}

let mut implementors = implementors.clone();

implementors.sort_by(compare_impl_polarity);

let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
i.inner_impl().for_.def_id().map_or(true, |d| cache.paths.contains_key(&d))
});
Expand Down

0 comments on commit 1bfb64b

Please sign in to comment.