Skip to content

Commit 0a386ba

Browse files
committed
Make PlaceProjectionsIter a proper iterator
1 parent b461740 commit 0a386ba

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/librustc/mir/mod.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::rustc_serialize::{self as serialize};
2020
use smallvec::SmallVec;
2121
use std::borrow::Cow;
2222
use std::fmt::{self, Debug, Formatter, Write};
23+
use std::iter::FusedIterator;
2324
use std::ops::{Index, IndexMut};
2425
use std::slice;
2526
use std::vec::IntoIter;
@@ -2105,6 +2106,15 @@ pub enum PlaceProjections<'p, 'tcx: 'p> {
21052106
}
21062107

21072108
impl<'p, 'tcx> PlaceProjections<'p, 'tcx> {
2109+
fn iter(&self) -> PlaceProjectionsIter<'_, 'tcx> {
2110+
PlaceProjectionsIter { value: self }
2111+
}
2112+
}
2113+
2114+
impl<'p, 'tcx> IntoIterator for &'p PlaceProjections<'p, 'tcx> {
2115+
type Item = &'p PlaceProjection<'tcx>;
2116+
type IntoIter = PlaceProjectionsIter<'p, 'tcx>;
2117+
21082118
/// Converts a list of `PlaceProjection` components into an iterator;
21092119
/// this iterator yields up a never-ending stream of `Option<&Place>`.
21102120
/// These begin with the "innermost" projection and then with each
@@ -2114,8 +2124,8 @@ impl<'p, 'tcx> PlaceProjections<'p, 'tcx> {
21142124
/// ```notrust
21152125
/// Some(`a`), Some(`a.b`), Some(`a.b.c`), None, None, ...
21162126
/// ```
2117-
fn iter(&self) -> PlaceProjectionsIter<'_, 'tcx> {
2118-
PlaceProjectionsIter { value: self }
2127+
fn into_iter(self) -> Self::IntoIter {
2128+
self.iter()
21192129
}
21202130
}
21212131

@@ -2129,8 +2139,10 @@ pub struct PlaceProjectionsIter<'p, 'tcx: 'p> {
21292139
pub value: &'p PlaceProjections<'p, 'tcx>,
21302140
}
21312141

2132-
impl<'p, 'tcx> PlaceProjectionsIter<'p, 'tcx> {
2133-
pub fn next(&mut self) -> Option<&'p PlaceProjection<'tcx>> {
2142+
impl<'p, 'tcx> Iterator for PlaceProjectionsIter<'p, 'tcx> {
2143+
type Item = &'p PlaceProjection<'tcx>;
2144+
2145+
fn next(&mut self) -> Option<Self::Item> {
21342146
if let &PlaceProjections::List { projection, next } = self.value {
21352147
self.value = next;
21362148
Some(projection)
@@ -2140,6 +2152,8 @@ impl<'p, 'tcx> PlaceProjectionsIter<'p, 'tcx> {
21402152
}
21412153
}
21422154

2155+
impl<'p, 'tcx> FusedIterator for PlaceProjectionsIter<'p, 'tcx> {}
2156+
21432157
impl<'tcx> Debug for Place<'tcx> {
21442158
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
21452159
use self::Place::*;

0 commit comments

Comments
 (0)