-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Put negative implementors first and apply same ordering logic to foreign implementors #142380
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
//! or contains "invocation-specific". | ||
|
||
use std::cell::RefCell; | ||
use std::cmp::Ordering; | ||
use std::ffi::OsString; | ||
use std::fs::File; | ||
use std::io::{self, Write as _}; | ||
|
@@ -46,6 +47,7 @@ use crate::formats::Impl; | |
use crate::formats::item_type::ItemType; | ||
use crate::html::layout; | ||
use crate::html::render::ordered_json::{EscapedJson, OrderedJson}; | ||
use crate::html::render::print_item::compare_names; | ||
use crate::html::render::search_index::{SerializedSearchIndex, build_index}; | ||
use crate::html::render::sorted_template::{self, FileFormat, SortedTemplate}; | ||
use crate::html::render::{AssocItemLink, ImplRenderingParameters, StylePath}; | ||
|
@@ -695,7 +697,7 @@ impl TraitAliasPart { | |
fn blank() -> SortedTemplate<<Self as CciPart>::FileFormat> { | ||
SortedTemplate::from_before_after( | ||
r"(function() { | ||
var implementors = Object.fromEntries([", | ||
const implementors = Object.fromEntries([", | ||
r"]); | ||
if (window.register_implementors) { | ||
window.register_implementors(implementors); | ||
|
@@ -748,10 +750,12 @@ impl TraitAliasPart { | |
{ | ||
None | ||
} else { | ||
let impl_ = imp.inner_impl(); | ||
Some(Implementor { | ||
text: imp.inner_impl().print(false, cx).to_string(), | ||
text: impl_.print(false, cx).to_string(), | ||
synthetic: imp.inner_impl().kind.is_auto(), | ||
types: collect_paths_for_type(&imp.inner_impl().for_, cache), | ||
is_negative: impl_.is_negative_trait_impl(), | ||
}) | ||
} | ||
}) | ||
|
@@ -770,19 +774,28 @@ impl TraitAliasPart { | |
} | ||
path.push(format!("{remote_item_type}.{}.js", remote_path[remote_path.len() - 1])); | ||
|
||
let part = OrderedJson::array_sorted( | ||
implementors.map(|implementor| OrderedJson::serialize(implementor).unwrap()), | ||
let mut implementors = implementors.collect::<Vec<_>>(); | ||
implementors.sort_unstable(); | ||
|
||
let part = OrderedJson::array_unsorted( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely an improvement, as it eliminates an allocation, but we're still allocating an intermediate string for each element.
I can open an issue for improving the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An issue would be welcome indeed. |
||
implementors | ||
.iter() | ||
.map(OrderedJson::serialize) | ||
.collect::<Result<Vec<_>, _>>() | ||
.unwrap(), | ||
); | ||
path_parts.push(path, OrderedJson::array_unsorted([crate_name_json, &part])); | ||
} | ||
Ok(path_parts) | ||
} | ||
} | ||
|
||
#[derive(Eq)] | ||
struct Implementor { | ||
text: String, | ||
synthetic: bool, | ||
types: Vec<String>, | ||
is_negative: bool, | ||
} | ||
|
||
impl Serialize for Implementor { | ||
|
@@ -792,6 +805,7 @@ impl Serialize for Implementor { | |
{ | ||
let mut seq = serializer.serialize_seq(None)?; | ||
seq.serialize_element(&self.text)?; | ||
seq.serialize_element(if self.is_negative { &1 } else { &0 })?; | ||
if self.synthetic { | ||
seq.serialize_element(&1)?; | ||
seq.serialize_element(&self.types)?; | ||
|
@@ -800,6 +814,29 @@ impl Serialize for Implementor { | |
} | ||
} | ||
|
||
impl PartialEq for Implementor { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cmp(other) == Ordering::Equal | ||
} | ||
} | ||
|
||
impl PartialOrd for Implementor { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(Ord::cmp(self, other)) | ||
} | ||
} | ||
|
||
impl Ord for Implementor { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
// We sort negative impls first. | ||
match (self.is_negative, other.is_negative) { | ||
(false, true) => Ordering::Greater, | ||
(true, false) => Ordering::Less, | ||
_ => compare_names(&self.text, &other.text), | ||
} | ||
} | ||
} | ||
|
||
/// Collect the list of aliased types and their aliases. | ||
/// <https://github.com/search?q=repo%3Arust-lang%2Frust+[RUSTDOCIMPL]+type.impl&type=code> | ||
/// | ||
|
Uh oh!
There was an error while loading. Please reload this page.