Skip to content

Commit 8dde6d5

Browse files
committed
Streamline TypeAliasPart::get.
- `ret` only ever gets at most one entry, so it can be an `Option` instead of a `Vec`. - Which means we can use `filter_map` instead of `flat_map`. - Move `trait_` next to the `ret` assignment, which can only happen once. - No need for `impls` to be a `Vec`, it can remain an iterator. - Avoid `Result` when collecting `impls`.
1 parent 4f1f1a2 commit 8dde6d5

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -607,25 +607,18 @@ impl TypeAliasPart {
607607
let cx = type_impl_collector.cx;
608608
let aliased_types = type_impl_collector.aliased_types;
609609
for aliased_type in aliased_types.values() {
610-
let impls = aliased_type
611-
.impl_
612-
.values()
613-
.flat_map(|AliasedTypeImpl { impl_, type_aliases }| {
614-
let mut ret: Vec<AliasSerializableImpl> = Vec::new();
615-
let trait_ = impl_
616-
.inner_impl()
617-
.trait_
618-
.as_ref()
619-
.map(|trait_| format!("{:#}", trait_.print(cx)));
610+
let impls = aliased_type.impl_.values().filter_map(
611+
|AliasedTypeImpl { impl_, type_aliases }| {
612+
let mut ret: Option<AliasSerializableImpl> = None;
620613
// render_impl will filter out "impossible-to-call" methods
621614
// to make that functionality work here, it needs to be called with
622615
// each type alias, and if it gives a different result, split the impl
623616
for &(type_alias_fqp, type_alias_item) in type_aliases {
624617
cx.id_map.borrow_mut().clear();
625618
cx.deref_id_map.borrow_mut().clear();
626619
let type_alias_fqp = (*type_alias_fqp).iter().join("::");
627-
if let Some(last) = ret.last_mut() {
628-
last.aliases.push(type_alias_fqp);
620+
if let Some(ret) = &mut ret {
621+
ret.aliases.push(type_alias_fqp);
629622
} else {
630623
let target_did = impl_
631624
.inner_impl()
@@ -660,16 +653,22 @@ impl TypeAliasPart {
660653
},
661654
)
662655
.to_string();
663-
ret.push(AliasSerializableImpl {
656+
// The alternate display disables html escaping of '<' and '>'.
657+
let trait_ = impl_
658+
.inner_impl()
659+
.trait_
660+
.as_ref()
661+
.map(|trait_| format!("{:#}", trait_.print(cx)));
662+
ret = Some(AliasSerializableImpl {
664663
text,
665-
trait_: trait_.clone(),
664+
trait_,
666665
aliases: vec![type_alias_fqp],
667666
})
668667
}
669668
}
670669
ret
671-
})
672-
.collect::<Vec<_>>();
670+
},
671+
);
673672

674673
let mut path = PathBuf::from("type.impl");
675674
for component in &aliased_type.target_fqp[..aliased_type.target_fqp.len() - 1] {
@@ -682,7 +681,7 @@ impl TypeAliasPart {
682681
));
683682

684683
let part = OrderedJson::array_sorted(
685-
impls.iter().map(OrderedJson::serialize).collect::<Result<Vec<_>, _>>().unwrap(),
684+
impls.map(|impl_| OrderedJson::serialize(impl_).unwrap()),
686685
);
687686
path_parts.push(path, OrderedJson::array_unsorted([crate_name_json, &part]));
688687
}

0 commit comments

Comments
 (0)