Skip to content

Commit 3646a09

Browse files
Improve code
1 parent 2b292d1 commit 3646a09

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ impl Item {
764764
Some(tcx.visibility(def_id))
765765
}
766766

767-
pub(crate) fn attributes_witout_repr(&self, tcx: TyCtxt<'_>, is_json: bool) -> Vec<String> {
767+
pub(crate) fn attributes_without_repr(&self, tcx: TyCtxt<'_>, is_json: bool) -> Vec<String> {
768768
const ALLOWED_ATTRIBUTES: &[Symbol] =
769769
&[sym::export_name, sym::link_section, sym::no_mangle, sym::non_exhaustive];
770770

@@ -805,15 +805,15 @@ impl Item {
805805
cache: &Cache,
806806
is_json: bool,
807807
) -> Vec<String> {
808-
let mut attrs = self.attributes_witout_repr(tcx, is_json);
808+
let mut attrs = self.attributes_without_repr(tcx, is_json);
809809

810810
if let Some(repr_attr) = self.repr(tcx, cache) {
811811
attrs.push(repr_attr);
812812
}
813813
attrs
814814
}
815815

816-
/// Returns a `#[repr(...)]` representation.
816+
/// Returns a stringified `#[repr(...)]` attribute.
817817
pub(crate) fn repr(&self, tcx: TyCtxt<'_>, cache: &Cache, is_json: bool) -> Option<String> {
818818
repr_attributes(tcx, cache, self.def_id()?, self.type_(), is_json)
819819
}
@@ -2370,8 +2370,9 @@ impl TypeAliasInnerType {
23702370
fn has_stripped_entries(&self) -> Option<bool> {
23712371
Some(match self {
23722372
Self::Enum { variants, .. } => variants.iter().any(|v| v.is_stripped()),
2373-
Self::Union { fields } => fields.iter().any(|f| f.is_stripped()),
2374-
Self::Struct { fields, .. } => fields.iter().any(|f| f.is_stripped()),
2373+
Self::Union { fields } | Self::Struct { fields, .. } => {
2374+
fields.iter().any(|f| f.is_stripped())
2375+
}
23752376
})
23762377
}
23772378
}

src/librustdoc/html/render/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,17 +1203,15 @@ fn render_attributes_in_pre(it: &clean::Item, prefix: &str, cx: &Context<'_>) ->
12031203

12041204
struct CodeAttribute(String);
12051205

1206-
impl CodeAttribute {
1207-
fn render_into(self, w: &mut impl fmt::Write) {
1208-
write!(w, "<div class=\"code-attribute\">{}</div>", self.0).unwrap();
1209-
}
1206+
fn render_code_attribute(code_attr: CodeAttribute, w: &mut impl fmt::Write) {
1207+
write!(w, "<div class=\"code-attribute\">{}</div>", code_attr.0).unwrap();
12101208
}
12111209

12121210
// When an attribute is rendered inside a <code> tag, it is formatted using
12131211
// a div to produce a newline after it.
12141212
fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Context<'_>) {
12151213
for attr in it.attributes_and_repr(cx.tcx(), cx.cache(), false) {
1216-
CodeAttribute(attr).render_into(w);
1214+
render_code_attribute(CodeAttribute(attr), w);
12171215
}
12181216
}
12191217

@@ -1225,7 +1223,7 @@ fn render_repr_attributes_in_code(
12251223
item_type: ItemType,
12261224
) {
12271225
if let Some(repr) = clean::repr_attributes(cx.tcx(), cx.cache(), def_id, item_type) {
1228-
CodeAttribute(repr).render_into(w);
1226+
render_code_attribute(CodeAttribute(repr), w);
12291227
}
12301228
}
12311229

src/librustdoc/html/render/print_item.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,26 +1457,23 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
14571457
ty.print(self.cx)
14581458
}
14591459

1460-
fn fields_iter(
1461-
&self,
1462-
) -> iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
1463-
self.fields
1464-
.iter()
1465-
.filter_map(|f| match f.kind {
1466-
clean::StructFieldItem(ref ty) => Some((f, ty)),
1467-
_ => None,
1468-
})
1469-
.peekable()
1460+
// FIXME (GuillaumeGomez): When <https://github.com/askama-rs/askama/issues/452> is implemented,
1461+
// we can replace the returned value with:
1462+
//
1463+
// `iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>>`
1464+
//
1465+
// And update `item_union.html`.
1466+
fn fields_iter(&self) -> impl Iterator<Item = (&'a clean::Item, &'a clean::Type)> {
1467+
self.fields.iter().filter_map(|f| match f.kind {
1468+
clean::StructFieldItem(ref ty) => Some((f, ty)),
1469+
_ => None,
1470+
})
14701471
}
14711472

14721473
fn render_attributes_in_pre(&self) -> impl fmt::Display {
14731474
fmt::from_fn(move |f| {
1474-
if !self.is_type_alias {
1475-
for a in self.it.attributes_and_repr(self.cx.tcx(), self.cx.cache(), false) {
1476-
writeln!(f, "{a}")?;
1477-
}
1478-
} else {
1479-
// For now we only render `repr` attributes for type aliases.
1475+
if self.is_type_alias {
1476+
// For now the only attributes we render for type aliases are `repr` attributes.
14801477
if let Some(repr) = clean::repr_attributes(
14811478
self.cx.tcx(),
14821479
self.cx.cache(),
@@ -1485,6 +1482,10 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
14851482
) {
14861483
writeln!(f, "{repr}")?;
14871484
};
1485+
} else {
1486+
for a in self.it.attributes_and_repr(self.cx.tcx(), self.cx.cache(), false) {
1487+
writeln!(f, "{a}")?;
1488+
}
14881489
}
14891490
Ok(())
14901491
})
@@ -1501,8 +1502,7 @@ fn item_union(cx: &Context<'_>, it: &clean::Item, s: &clean::Union) -> impl fmt:
15011502
is_type_alias: false,
15021503
def_id: it.def_id().unwrap(),
15031504
}
1504-
.render_into(w)
1505-
.unwrap();
1505+
.render_into(w)?;
15061506
Ok(())
15071507
})
15081508
}
@@ -1529,31 +1529,31 @@ fn print_tuple_struct_fields(cx: &Context<'_>, s: &[clean::Item]) -> impl Displa
15291529
})
15301530
}
15311531

1532-
struct DisplayEnum<'a> {
1533-
variants: &'a IndexVec<VariantIdx, clean::Item>,
1534-
generics: &'a clean::Generics,
1532+
struct DisplayEnum<'clean> {
1533+
variants: &'clean IndexVec<VariantIdx, clean::Item>,
1534+
generics: &'clean clean::Generics,
15351535
is_non_exhaustive: bool,
15361536
def_id: DefId,
15371537
}
15381538

1539-
impl<'a> DisplayEnum<'a> {
1539+
impl<'clean> DisplayEnum<'clean> {
15401540
fn render_into<W: fmt::Write>(
15411541
self,
15421542
cx: &Context<'_>,
15431543
it: &clean::Item,
15441544
is_type_alias: bool,
15451545
w: &mut W,
15461546
) -> fmt::Result {
1547-
let variants_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
1547+
let non_stripped_variant_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
15481548
let variants_len = self.variants.len();
1549-
let has_stripped_entries = variants_len != variants_count;
1549+
let has_stripped_entries = variants_len != non_stripped_variant_count;
15501550

15511551
wrap_item(w, |w| {
1552-
if !is_type_alias {
1553-
render_attributes_in_code(w, it, cx);
1554-
} else {
1555-
// For now we only render `repr` attributes for type aliases.
1552+
if is_type_alias {
1553+
// For now the only attributes we render for type aliases are `repr` attributes.
15561554
render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Enum);
1555+
} else {
1556+
render_attributes_in_code(w, it, cx);
15571557
}
15581558
write!(
15591559
w,
@@ -1565,7 +1565,7 @@ impl<'a> DisplayEnum<'a> {
15651565
cx,
15661566
Some(self.generics),
15671567
self.variants,
1568-
variants_count,
1568+
non_stripped_variant_count,
15691569
has_stripped_entries,
15701570
self.is_non_exhaustive,
15711571
self.def_id,
@@ -1574,14 +1574,16 @@ impl<'a> DisplayEnum<'a> {
15741574
})?;
15751575

15761576
let def_id = it.item_id.expect_def_id();
1577-
let layout_def_id = if !is_type_alias {
1577+
let layout_def_id = if is_type_alias {
1578+
self.def_id
1579+
} else {
15781580
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1581+
// We don't return the same `DefId` since the layout size of the type alias might be
1582+
// different since we might have more information on the generics.
15791583
def_id
1580-
} else {
1581-
self.def_id
15821584
};
15831585

1584-
if variants_count != 0 {
1586+
if non_stripped_variant_count != 0 {
15851587
write!(w, "{}", item_variants(cx, it, self.variants, self.def_id))?;
15861588
}
15871589
write!(
@@ -2005,11 +2007,11 @@ impl<'a> DisplayStruct<'a> {
20052007
w: &mut W,
20062008
) -> fmt::Result {
20072009
wrap_item(w, |w| {
2008-
if !is_type_alias {
2009-
render_attributes_in_code(w, it, cx);
2010-
} else {
2011-
// For now we only render `repr` attributes for type aliases.
2010+
if is_type_alias {
2011+
// For now the only attributes we render for type aliases are `repr` attributes.
20122012
render_repr_attributes_in_code(w, cx, self.def_id, ItemType::Struct);
2013+
} else {
2014+
render_attributes_in_code(w, it, cx);
20132015
}
20142016
write!(
20152017
w,

src/librustdoc/html/templates/item_union.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% if !self.is_type_alias %}
66
{{ self.document()|safe }}
77
{% endif %}
8-
{% if self.fields_iter().peek().is_some() %}
8+
{% if self.fields_iter().next().is_some() %}
99
<h2 id="fields" class="fields section-header"> {# #}
1010
Fields<a href="#fields" class="anchor">§</a> {# #}
1111
</h2>

0 commit comments

Comments
 (0)