Skip to content

Commit

Permalink
Rollup merge of rust-lang#56744 - ljedrz:unbox_the_children, r=matthe…
Browse files Browse the repository at this point in the history
…wjasper

specialize: remove Boxes used by Children::insert
  • Loading branch information
kennytm committed Dec 14, 2018
2 parents facad1d + cd7e891 commit 7ec1faa
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/librustc/traits/specialize/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ impl<'a, 'gcx, 'tcx> Children {
simplified_self,
);

for possible_sibling in match simplified_self {
Some(sty) => self.filtered(sty),
None => self.iter(),
} {
let possible_siblings = match simplified_self {
Some(sty) => PotentialSiblings::Filtered(self.filtered(sty)),
None => PotentialSiblings::Unfiltered(self.iter()),
};

for possible_sibling in possible_siblings {
debug!(
"insert: impl_def_id={:?}, simplified_self={:?}, possible_sibling={:?}",
impl_def_id,
Expand Down Expand Up @@ -222,14 +224,37 @@ impl<'a, 'gcx, 'tcx> Children {
Ok(Inserted::BecameNewSibling(last_lint))
}

fn iter(&mut self) -> Box<dyn Iterator<Item = DefId> + '_> {
fn iter(&mut self) -> impl Iterator<Item = DefId> + '_ {
let nonblanket = self.nonblanket_impls.iter_mut().flat_map(|(_, v)| v.iter());
Box::new(self.blanket_impls.iter().chain(nonblanket).cloned())
self.blanket_impls.iter().chain(nonblanket).cloned()
}

fn filtered(&mut self, sty: SimplifiedType) -> Box<dyn Iterator<Item = DefId> + '_> {
fn filtered(&mut self, sty: SimplifiedType) -> impl Iterator<Item = DefId> + '_ {
let nonblanket = self.nonblanket_impls.entry(sty).or_default().iter();
Box::new(self.blanket_impls.iter().chain(nonblanket).cloned())
self.blanket_impls.iter().chain(nonblanket).cloned()
}
}

// A custom iterator used by Children::insert
enum PotentialSiblings<I, J>
where I: Iterator<Item = DefId>,
J: Iterator<Item = DefId>
{
Unfiltered(I),
Filtered(J)
}

impl<I, J> Iterator for PotentialSiblings<I, J>
where I: Iterator<Item = DefId>,
J: Iterator<Item = DefId>
{
type Item = DefId;

fn next(&mut self) -> Option<Self::Item> {
match *self {
PotentialSiblings::Unfiltered(ref mut iter) => iter.next(),
PotentialSiblings::Filtered(ref mut iter) => iter.next()
}
}
}

Expand Down

0 comments on commit 7ec1faa

Please sign in to comment.