Skip to content

Commit c51f167

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.
1 parent 4f1f1a2 commit c51f167

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 15 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,21 @@ impl TypeAliasPart {
660653
},
661654
)
662655
.to_string();
663-
ret.push(AliasSerializableImpl {
656+
let trait_ = impl_
657+
.inner_impl()
658+
.trait_
659+
.as_ref()
660+
.map(|trait_| format!("{:#}", trait_.print(cx)));
661+
ret = Some(AliasSerializableImpl {
664662
text,
665-
trait_: trait_.clone(),
663+
trait_,
666664
aliases: vec![type_alias_fqp],
667665
})
668666
}
669667
}
670668
ret
671-
})
672-
.collect::<Vec<_>>();
669+
},
670+
);
673671

674672
let mut path = PathBuf::from("type.impl");
675673
for component in &aliased_type.target_fqp[..aliased_type.target_fqp.len() - 1] {
@@ -682,7 +680,7 @@ impl TypeAliasPart {
682680
));
683681

684682
let part = OrderedJson::array_sorted(
685-
impls.iter().map(OrderedJson::serialize).collect::<Result<Vec<_>, _>>().unwrap(),
683+
impls.map(OrderedJson::serialize).collect::<Result<Vec<_>, _>>().unwrap(),
686684
);
687685
path_parts.push(path, OrderedJson::array_unsorted([crate_name_json, &part]));
688686
}

0 commit comments

Comments
 (0)