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: allow moving methods from deref below trait impls via a new unstable command line argument #92273

4 changes: 4 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ crate struct RenderOptions {
crate call_locations: AllCallLocations,
/// If `true`, Context::init will not emit shared files.
crate no_emit_shared: bool,
/// If `true`, "Methods From Deref" will be displayed after all other sections.
crate show_deref_methods_last: bool,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -654,6 +656,7 @@ impl Options {
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
let extern_html_root_takes_precedence =
matches.opt_present("extern-html-root-takes-precedence");
let show_deref_methods_last = matches.opt_present("show-deref-methods-last");

if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
diag.struct_err(
Expand Down Expand Up @@ -731,6 +734,7 @@ impl Options {
generate_link_to_definition,
call_locations,
no_emit_shared: false,
show_deref_methods_last,
},
crate_name,
output_format,
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ crate struct SharedContext<'tcx> {
crate cache: Cache,

crate call_locations: AllCallLocations,
/// If `true`, "Methods From Deref" will be displayed after all other sections.
crate show_deref_methods_last: bool,
}

impl SharedContext<'_> {
Expand Down Expand Up @@ -393,6 +395,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
generate_link_to_definition,
call_locations,
no_emit_shared,
show_deref_methods_last,
..
} = options;

Expand Down Expand Up @@ -477,6 +480,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
span_correspondance_map: matches,
cache,
call_locations,
show_deref_methods_last,
};

// Add the default themes to the `Vec` of stylepaths
Expand Down
44 changes: 32 additions & 12 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,12 +1097,18 @@ fn render_assoc_items_inner(
}

if !traits.is_empty() {
let deref_impl =
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait());
if let Some(impl_) = deref_impl {
let has_deref_mut =
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs);
let mut deref_methods = |w: &mut Buffer| {
let deref_impl =
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait());
if let Some(impl_) = deref_impl {
let has_deref_mut =
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs);
}
};

if !cx.shared.show_deref_methods_last {
deref_methods(w);
}

// If we were already one level into rendering deref methods, we don't want to render
Expand Down Expand Up @@ -1153,6 +1159,10 @@ fn render_assoc_items_inner(
render_impls(cx, w, &blanket_impl, containing_item);
w.write_str("</div>");
}

if cx.shared.show_deref_methods_last {
deref_methods(w);
}
}
}

Expand Down Expand Up @@ -2002,12 +2012,18 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
}

if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
if let Some(impl_) =
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
{
let mut derefs = FxHashSet::default();
derefs.insert(did);
sidebar_deref_methods(cx, out, impl_, v, &mut derefs);
let deref_methods = |out: &mut Buffer| {
if let Some(impl_) =
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
{
let mut derefs = FxHashSet::default();
derefs.insert(did);
sidebar_deref_methods(cx, out, impl_, v, &mut derefs);
}
};

if !cx.shared.show_deref_methods_last {
deref_methods(out);
}

let format_impls = |impls: Vec<&Impl>| {
Expand Down Expand Up @@ -2076,6 +2092,10 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
);
write_sidebar_links(out, blanket_format);
}

if cx.shared.show_deref_methods_last {
deref_methods(out);
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,13 @@ fn opts() -> Vec<RustcOptGroup> {
"path to function call information (for displaying examples in the documentation)",
)
}),
unstable("show-deref-methods-last", |o| {
o.optflagmulti(
"",
"show-deref-methods-last",
"display trait implementations before methods from deref",
)
}),
// deprecated / removed options
stable("plugin-path", |o| {
o.optmulti(
Expand Down