Rollup merge of #111946 - nicklimmm:askama-item-template-trait, r=GuillaumeGomez
rustdoc: Add `ItemTemplate` trait and related functions to avoid repetitively wrapping existing functions Context: https://github.com/rust-lang/rust/pull/111430#discussion_r1200672507 This trait will be used extensively in performing migrations to Askama templates (tracking issue: https://github.com/rust-lang/rust/issues/108868)
This commit is contained in:
commit
9a4fce978d
@ -9,6 +9,8 @@
|
|||||||
use rustc_middle::ty::{self, TyCtxt};
|
use rustc_middle::ty::{self, TyCtxt};
|
||||||
use rustc_span::hygiene::MacroKind;
|
use rustc_span::hygiene::MacroKind;
|
||||||
use rustc_span::symbol::{kw, sym, Symbol};
|
use rustc_span::symbol::{kw, sym, Symbol};
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
use std::cell::{RefCell, RefMut};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -216,6 +218,53 @@ fn toggle_close(mut w: impl fmt::Write) {
|
|||||||
w.write_str("</details>").unwrap();
|
w.write_str("</details>").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display {
|
||||||
|
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_template_document<'a: 'b, 'b, 'cx: 'a>(
|
||||||
|
templ: &'b impl ItemTemplate<'a, 'cx>,
|
||||||
|
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
||||||
|
display_fn(move |f| {
|
||||||
|
let (item, mut cx) = templ.item_and_mut_cx();
|
||||||
|
let v = document(*cx, item, None, HeadingOffset::H2);
|
||||||
|
write!(f, "{v}")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_template_document_type_layout<'a: 'b, 'b, 'cx: 'a>(
|
||||||
|
templ: &'b impl ItemTemplate<'a, 'cx>,
|
||||||
|
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
||||||
|
display_fn(move |f| {
|
||||||
|
let (item, cx) = templ.item_and_mut_cx();
|
||||||
|
let def_id = item.item_id.expect_def_id();
|
||||||
|
let v = document_type_layout(*cx, def_id);
|
||||||
|
write!(f, "{v}")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_template_render_attributes_in_pre<'a: 'b, 'b, 'cx: 'a>(
|
||||||
|
templ: &'b impl ItemTemplate<'a, 'cx>,
|
||||||
|
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
||||||
|
display_fn(move |f| {
|
||||||
|
let (item, cx) = templ.item_and_mut_cx();
|
||||||
|
let tcx = cx.tcx();
|
||||||
|
let v = render_attributes_in_pre(item, "", tcx);
|
||||||
|
write!(f, "{v}")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_template_render_assoc_items<'a: 'b, 'b, 'cx: 'a>(
|
||||||
|
templ: &'b impl ItemTemplate<'a, 'cx>,
|
||||||
|
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
||||||
|
display_fn(move |f| {
|
||||||
|
let (item, mut cx) = templ.item_and_mut_cx();
|
||||||
|
let def_id = item.item_id.expect_def_id();
|
||||||
|
let v = render_assoc_items(*cx, item, def_id, AssocItemRender::All);
|
||||||
|
write!(f, "{v}")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) {
|
fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) {
|
||||||
write!(w, "{}", document(cx, item, None, HeadingOffset::H2));
|
write!(w, "{}", document(cx, item, None, HeadingOffset::H2));
|
||||||
|
|
||||||
@ -1131,32 +1180,18 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
|
|||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "item_union.html")]
|
#[template(path = "item_union.html")]
|
||||||
struct ItemUnion<'a, 'cx> {
|
struct ItemUnion<'a, 'cx> {
|
||||||
cx: std::cell::RefCell<&'a mut Context<'cx>>,
|
cx: RefCell<&'a mut Context<'cx>>,
|
||||||
it: &'a clean::Item,
|
it: &'a clean::Item,
|
||||||
s: &'a clean::Union,
|
s: &'a clean::Union,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemUnion<'a, 'cx> {
|
||||||
|
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
|
||||||
|
(self.it, self.cx.borrow_mut())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
|
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
|
||||||
fn render_assoc_items<'b>(
|
|
||||||
&'b self,
|
|
||||||
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
|
||||||
display_fn(move |f| {
|
|
||||||
let def_id = self.it.item_id.expect_def_id();
|
|
||||||
let mut cx = self.cx.borrow_mut();
|
|
||||||
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
|
|
||||||
write!(f, "{v}")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn document_type_layout<'b>(
|
|
||||||
&'b self,
|
|
||||||
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
|
||||||
display_fn(move |f| {
|
|
||||||
let def_id = self.it.item_id.expect_def_id();
|
|
||||||
let cx = self.cx.borrow_mut();
|
|
||||||
let v = document_type_layout(*cx, def_id);
|
|
||||||
write!(f, "{v}")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
||||||
display_fn(move |f| {
|
display_fn(move |f| {
|
||||||
let cx = self.cx.borrow_mut();
|
let cx = self.cx.borrow_mut();
|
||||||
@ -1164,22 +1199,6 @@ fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Capture
|
|||||||
write!(f, "{v}")
|
write!(f, "{v}")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn render_attributes_in_pre<'b>(
|
|
||||||
&'b self,
|
|
||||||
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
|
||||||
display_fn(move |f| {
|
|
||||||
let tcx = self.cx.borrow().tcx();
|
|
||||||
let v = render_attributes_in_pre(self.it, "", tcx);
|
|
||||||
write!(f, "{v}")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
|
|
||||||
display_fn(move |f| {
|
|
||||||
let mut cx = self.cx.borrow_mut();
|
|
||||||
let v = document(*cx, self.it, None, HeadingOffset::H2);
|
|
||||||
write!(f, "{v}")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn document_field<'b>(
|
fn document_field<'b>(
|
||||||
&'b self,
|
&'b self,
|
||||||
field: &'a clean::Item,
|
field: &'a clean::Item,
|
||||||
@ -1219,7 +1238,7 @@ fn fields_iter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemUnion { cx: std::cell::RefCell::new(cx), it, s }.render_into(w).unwrap();
|
ItemUnion { cx: RefCell::new(cx), it, s }.render_into(w).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_tuple_struct_fields<'a, 'cx: 'a>(
|
fn print_tuple_struct_fields<'a, 'cx: 'a>(
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<pre class="rust item-decl"><code>
|
<pre class="rust item-decl"><code>
|
||||||
{{ self.render_attributes_in_pre() | safe }}
|
{{ self::item_template_render_attributes_in_pre(self.borrow()) | safe }}
|
||||||
{{ self.render_union() | safe }}
|
{{ self.render_union() | safe }}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
{{ self.document() | safe }}
|
{{ self::item_template_document(self.borrow()) | safe }}
|
||||||
{% if self.fields_iter().peek().is_some() %}
|
{% if self.fields_iter().peek().is_some() %}
|
||||||
<h2 id="fields" class="fields small-section-header">
|
<h2 id="fields" class="fields small-section-header">
|
||||||
Fields<a href="#fields" class="anchor">§</a>
|
Fields<a href="#fields" class="anchor">§</a>
|
||||||
@ -19,5 +19,5 @@
|
|||||||
{{ self.document_field(field) | safe }}
|
{{ self.document_field(field) | safe }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{ self.render_assoc_items() | safe }}
|
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
|
||||||
{{ self.document_type_layout() | safe }}
|
{{ self::item_template_document_type_layout(self.borrow()) | safe }}
|
||||||
|
Loading…
Reference in New Issue
Block a user