Skip to content

Avoid the Box in TyCtxt::associated_items. #55604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/traits/specialize/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl<'a, 'gcx, 'tcx> Node {
pub fn items(
&self,
tcx: TyCtxt<'a, 'gcx, 'tcx>,
) -> impl Iterator<Item = ty::AssociatedItem> + 'a {
) -> ty::AssociatedItemsIterator<'a, 'gcx, 'tcx> {
tcx.associated_items(self.def_id())
}

Expand Down
31 changes: 27 additions & 4 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2674,10 +2674,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn associated_items(
self,
def_id: DefId,
) -> impl Iterator<Item = AssociatedItem> + 'a {
let def_ids = self.associated_item_def_ids(def_id);
Box::new((0..def_ids.len()).map(move |i| self.associated_item(def_ids[i])))
as Box<dyn Iterator<Item = AssociatedItem> + 'a>
) -> AssociatedItemsIterator<'a, 'gcx, 'tcx> {
// Ideally, we would use `-> impl Iterator` here, but it falls
// afoul of the conservative "capture [restrictions]" we put
// in place, so we use a hand-written iterator.
//
// [restrictions]: https://github.com/rust-lang/rust/issues/34511#issuecomment-373423999
AssociatedItemsIterator {
tcx: self,
def_ids: self.associated_item_def_ids(def_id),
next_index: 0,
}
}

/// Returns `true` if the impls are the same polarity and the trait either
Expand Down Expand Up @@ -2874,6 +2881,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

pub struct AssociatedItemsIterator<'a, 'gcx: 'tcx, 'tcx: 'a> {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
def_ids: Lrc<Vec<DefId>>,
next_index: usize,
}

impl Iterator for AssociatedItemsIterator<'_, '_, '_> {
type Item = AssociatedItem;

fn next(&mut self) -> Option<AssociatedItem> {
let def_id = self.def_ids.get(self.next_index)?;
self.next_index += 1;
Some(self.tcx.associated_item(*def_id))
}
}

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn with_freevars<T, F>(self, fid: NodeId, f: F) -> T where
F: FnOnce(&[hir::Freevar]) -> T,
Expand Down