Improve code
This commit is contained in:
parent
2b292d1b78
commit
3646a09811
4 changed files with 51 additions and 50 deletions
|
|
@ -764,7 +764,7 @@ impl Item {
|
|||
Some(tcx.visibility(def_id))
|
||||
}
|
||||
|
||||
pub(crate) fn attributes_witout_repr(&self, tcx: TyCtxt<'_>, is_json: bool) -> Vec<String> {
|
||||
pub(crate) fn attributes_without_repr(&self, tcx: TyCtxt<'_>, is_json: bool) -> Vec<String> {
|
||||
const ALLOWED_ATTRIBUTES: &[Symbol] =
|
||||
&[sym::export_name, sym::link_section, sym::no_mangle, sym::non_exhaustive];
|
||||
|
||||
|
|
@ -805,7 +805,7 @@ impl Item {
|
|||
cache: &Cache,
|
||||
is_json: bool,
|
||||
) -> Vec<String> {
|
||||
let mut attrs = self.attributes_witout_repr(tcx, is_json);
|
||||
let mut attrs = self.attributes_without_repr(tcx, is_json);
|
||||
|
||||
if let Some(repr_attr) = self.repr(tcx, cache) {
|
||||
attrs.push(repr_attr);
|
||||
|
|
@ -813,7 +813,7 @@ impl Item {
|
|||
attrs
|
||||
}
|
||||
|
||||
/// Returns a `#[repr(...)]` representation.
|
||||
/// Returns a stringified `#[repr(...)]` attribute.
|
||||
pub(crate) fn repr(&self, tcx: TyCtxt<'_>, cache: &Cache, is_json: bool) -> Option<String> {
|
||||
repr_attributes(tcx, cache, self.def_id()?, self.type_(), is_json)
|
||||
}
|
||||
|
|
@ -2370,8 +2370,9 @@ impl TypeAliasInnerType {
|
|||
fn has_stripped_entries(&self) -> Option<bool> {
|
||||
Some(match self {
|
||||
Self::Enum { variants, .. } => variants.iter().any(|v| v.is_stripped()),
|
||||
Self::Union { fields } => fields.iter().any(|f| f.is_stripped()),
|
||||
Self::Struct { fields, .. } => fields.iter().any(|f| f.is_stripped()),
|
||||
Self::Union { fields } | Self::Struct { fields, .. } => {
|
||||
fields.iter().any(|f| f.is_stripped())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1203,17 +1203,15 @@ fn render_attributes_in_pre(it: &clean::Item, prefix: &str, cx: &Context<'_>) ->
|
|||
|
||||
struct CodeAttribute(String);
|
||||
|
||||
impl CodeAttribute {
|
||||
fn render_into(self, w: &mut impl fmt::Write) {
|
||||
write!(w, "<div class=\"code-attribute\">{}</div>", self.0).unwrap();
|
||||
}
|
||||
fn render_code_attribute(code_attr: CodeAttribute, w: &mut impl fmt::Write) {
|
||||
write!(w, "<div class=\"code-attribute\">{}</div>", code_attr.0).unwrap();
|
||||
}
|
||||
|
||||
// When an attribute is rendered inside a <code> tag, it is formatted using
|
||||
// a div to produce a newline after it.
|
||||
fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Context<'_>) {
|
||||
for attr in it.attributes_and_repr(cx.tcx(), cx.cache(), false) {
|
||||
CodeAttribute(attr).render_into(w);
|
||||
render_code_attribute(CodeAttribute(attr), w);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1225,7 +1223,7 @@ fn render_repr_attributes_in_code(
|
|||
item_type: ItemType,
|
||||
) {
|
||||
if let Some(repr) = clean::repr_attributes(cx.tcx(), cx.cache(), def_id, item_type) {
|
||||
CodeAttribute(repr).render_into(w);
|
||||
render_code_attribute(CodeAttribute(repr), w);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1457,26 +1457,23 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
|
|||
ty.print(self.cx)
|
||||
}
|
||||
|
||||
fn fields_iter(
|
||||
&self,
|
||||
) -> iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
|
||||
self.fields
|
||||
.iter()
|
||||
.filter_map(|f| match f.kind {
|
||||
clean::StructFieldItem(ref ty) => Some((f, ty)),
|
||||
_ => None,
|
||||
})
|
||||
.peekable()
|
||||
// FIXME (GuillaumeGomez): When <https://github.com/askama-rs/askama/issues/452> is implemented,
|
||||
// we can replace the returned value with:
|
||||
//
|
||||
// `iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>>`
|
||||
//
|
||||
// And update `item_union.html`.
|
||||
fn fields_iter(&self) -> impl Iterator<Item = (&'a clean::Item, &'a clean::Type)> {
|
||||
self.fields.iter().filter_map(|f| match f.kind {
|
||||
clean::StructFieldItem(ref ty) => Some((f, ty)),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn render_attributes_in_pre(&self) -> impl fmt::Display {
|
||||
fmt::from_fn(move |f| {
|
||||
if !self.is_type_alias {
|
||||
for a in self.it.attributes_and_repr(self.cx.tcx(), self.cx.cache(), false) {
|
||||
writeln!(f, "{a}")?;
|
||||
}
|
||||
} else {
|
||||
// For now we only render `repr` attributes for type aliases.
|
||||
if self.is_type_alias {
|
||||
// For now the only attributes we render for type aliases are `repr` attributes.
|
||||
if let Some(repr) = clean::repr_attributes(
|
||||
self.cx.tcx(),
|
||||
self.cx.cache(),
|
||||
|
|
@ -1485,6 +1482,10 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
|
|||
) {
|
||||
writeln!(f, "{repr}")?;
|
||||
};
|
||||
} else {
|
||||
for a in self.it.attributes_and_repr(self.cx.tcx(), self.cx.cache(), false) {
|
||||
writeln!(f, "{a}")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
|
|
@ -1501,8 +1502,7 @@ fn item_union(cx: &Context<'_>, it: &clean::Item, s: &clean::Union) -> impl fmt:
|
|||
is_type_alias: false,
|
||||
def_id: it.def_id().unwrap(),
|
||||
}
|
||||
.render_into(w)
|
||||
.unwrap();
|
||||
.render_into(w)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
@ -1529,14 +1529,14 @@ fn print_tuple_struct_fields(cx: &Context<'_>, s: &[clean::Item]) -> impl Displa
|
|||
})
|
||||
}
|
||||
|
||||
struct DisplayEnum<'a> {
|
||||
variants: &'a IndexVec<VariantIdx, clean::Item>,
|
||||
generics: &'a clean::Generics,
|
||||
struct DisplayEnum<'clean> {
|
||||
variants: &'clean IndexVec<VariantIdx, clean::Item>,
|
||||
generics: &'clean clean::Generics,
|
||||
is_non_exhaustive: bool,
|
||||
def_id: DefId,
|
||||
}
|
||||
|
||||
impl<'a> DisplayEnum<'a> {
|
||||
impl<'clean> DisplayEnum<'clean> {
|
||||
fn render_into<W: fmt::Write>(
|
||||
self,
|
||||
cx: &Context<'_>,
|
||||
|
|
@ -1544,16 +1544,16 @@ impl<'a> DisplayEnum<'a> {
|
|||
is_type_alias: bool,
|
||||
w: &mut W,
|
||||
) -> fmt::Result {
|
||||
let variants_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
|
||||
let non_stripped_variant_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
|
||||
let variants_len = self.variants.len();
|
||||
let has_stripped_entries = variants_len != variants_count;
|
||||
let has_stripped_entries = variants_len != non_stripped_variant_count;
|
||||
|
||||
wrap_item(w, |w| {
|
||||
if !is_type_alias {
|
||||
render_attributes_in_code(w, it, cx);
|
||||
} else {
|
||||
// For now we only render `repr` attributes for type aliases.
|
||||
if is_type_alias {
|
||||
// For now the only attributes we render for type aliases are `repr` attributes.
|
||||
render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Enum);
|
||||
} else {
|
||||
render_attributes_in_code(w, it, cx);
|
||||
}
|
||||
write!(
|
||||
w,
|
||||
|
|
@ -1565,7 +1565,7 @@ impl<'a> DisplayEnum<'a> {
|
|||
cx,
|
||||
Some(self.generics),
|
||||
self.variants,
|
||||
variants_count,
|
||||
non_stripped_variant_count,
|
||||
has_stripped_entries,
|
||||
self.is_non_exhaustive,
|
||||
self.def_id,
|
||||
|
|
@ -1574,14 +1574,16 @@ impl<'a> DisplayEnum<'a> {
|
|||
})?;
|
||||
|
||||
let def_id = it.item_id.expect_def_id();
|
||||
let layout_def_id = if !is_type_alias {
|
||||
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
|
||||
def_id
|
||||
} else {
|
||||
let layout_def_id = if is_type_alias {
|
||||
self.def_id
|
||||
} else {
|
||||
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
|
||||
// We don't return the same `DefId` since the layout size of the type alias might be
|
||||
// different since we might have more information on the generics.
|
||||
def_id
|
||||
};
|
||||
|
||||
if variants_count != 0 {
|
||||
if non_stripped_variant_count != 0 {
|
||||
write!(w, "{}", item_variants(cx, it, self.variants, self.def_id))?;
|
||||
}
|
||||
write!(
|
||||
|
|
@ -2005,11 +2007,11 @@ impl<'a> DisplayStruct<'a> {
|
|||
w: &mut W,
|
||||
) -> fmt::Result {
|
||||
wrap_item(w, |w| {
|
||||
if !is_type_alias {
|
||||
render_attributes_in_code(w, it, cx);
|
||||
} else {
|
||||
// For now we only render `repr` attributes for type aliases.
|
||||
if is_type_alias {
|
||||
// For now the only attributes we render for type aliases are `repr` attributes.
|
||||
render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Struct);
|
||||
} else {
|
||||
render_attributes_in_code(w, it, cx);
|
||||
}
|
||||
write!(
|
||||
w,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
{% if !self.is_type_alias %}
|
||||
{{ self.document()|safe }}
|
||||
{% endif %}
|
||||
{% if self.fields_iter().peek().is_some() %}
|
||||
{% if self.fields_iter().next().is_some() %}
|
||||
<h2 id="fields" class="fields section-header"> {# #}
|
||||
Fields<a href="#fields" class="anchor">§</a> {# #}
|
||||
</h2>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue