about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2018-03-08 11:25:57 -0800
committerGitHub <noreply@github.com>2018-03-08 11:25:57 -0800
commit6d0d34336d791d4e5c776d56fc1813b0929ee073 (patch)
tree31e32eb157494f1933eed3f46df9cea83e43371b
parentd7f44ac52c06b3232dfe6b4fd542c17ce9c8da9e (diff)
parent08a01825364c429675d28b326e047ce4bc56ff7f (diff)
downloadrust-6d0d34336d791d4e5c776d56fc1813b0929ee073.tar.gz
rust-6d0d34336d791d4e5c776d56fc1813b0929ee073.zip
Rollup merge of #48699 - frewsxcv:frewsxcv-impl-trait, r=nikomatsakis
Replace iterator structures with `impl Trait`.

Two commits:

* Replace iterator structures with `impl Trait`.
* Run rustfmt on `src/librustc_data_structures/graph/mod.rs`.
-rw-r--r--src/librustc_data_structures/graph/mod.rs151
-rw-r--r--src/librustc_data_structures/lib.rs1
2 files changed, 53 insertions, 99 deletions
diff --git a/src/librustc_data_structures/graph/mod.rs b/src/librustc_data_structures/graph/mod.rs
index 56d5f5ffa3f..1945b82c031 100644
--- a/src/librustc_data_structures/graph/mod.rs
+++ b/src/librustc_data_structures/graph/mod.rs
@@ -196,30 +196,30 @@ impl<N: Debug, E: Debug> Graph<N, E> {
 
     // # Iterating over nodes, edges
 
-    pub fn enumerated_nodes(&self) -> EnumeratedNodes<N> {
-        EnumeratedNodes {
-            iter: self.nodes.iter().enumerate()
-        }
+    pub fn enumerated_nodes(&self) -> impl Iterator<Item = (NodeIndex, &Node<N>)> {
+        self.nodes
+            .iter()
+            .enumerate()
+            .map(|(idx, n)| (NodeIndex(idx), n))
     }
 
-    pub fn enumerated_edges(&self) -> EnumeratedEdges<E> {
-        EnumeratedEdges {
-            iter: self.edges.iter().enumerate()
-        }
+    pub fn enumerated_edges(&self) -> impl Iterator<Item = (EdgeIndex, &Edge<E>)> {
+        self.edges
+            .iter()
+            .enumerate()
+            .map(|(idx, e)| (EdgeIndex(idx), e))
     }
 
-    pub fn each_node<'a, F>(&'a self, mut f: F) -> bool
-        where F: FnMut(NodeIndex, &'a Node<N>) -> bool
-    {
+    pub fn each_node<'a>(&'a self, mut f: impl FnMut(NodeIndex, &'a Node<N>) -> bool) -> bool {
         //! Iterates over all edges defined in the graph.
-        self.enumerated_nodes().all(|(node_idx, node)| f(node_idx, node))
+        self.enumerated_nodes()
+            .all(|(node_idx, node)| f(node_idx, node))
     }
 
-    pub fn each_edge<'a, F>(&'a self, mut f: F) -> bool
-        where F: FnMut(EdgeIndex, &'a Edge<E>) -> bool
-    {
+    pub fn each_edge<'a>(&'a self, mut f: impl FnMut(EdgeIndex, &'a Edge<E>) -> bool) -> bool {
         //! Iterates over all edges defined in the graph
-        self.enumerated_edges().all(|(edge_idx, edge)| f(edge_idx, edge))
+        self.enumerated_edges()
+            .all(|(edge_idx, edge)| f(edge_idx, edge))
     }
 
     pub fn outgoing_edges(&self, source: NodeIndex) -> AdjacentEdges<N, E> {
@@ -239,26 +239,33 @@ impl<N: Debug, E: Debug> Graph<N, E> {
         }
     }
 
-    pub fn successor_nodes(&self, source: NodeIndex) -> AdjacentTargets<N, E> {
+    pub fn successor_nodes<'a>(
+        &'a self,
+        source: NodeIndex,
+    ) -> impl Iterator<Item = NodeIndex> + 'a {
         self.outgoing_edges(source).targets()
     }
 
-    pub fn predecessor_nodes(&self, target: NodeIndex) -> AdjacentSources<N, E> {
+    pub fn predecessor_nodes<'a>(
+        &'a self,
+        target: NodeIndex,
+    ) -> impl Iterator<Item = NodeIndex> + 'a {
         self.incoming_edges(target).sources()
     }
 
-    pub fn depth_traverse<'a>(&'a self,
-                              start: NodeIndex,
-                              direction: Direction)
-                              -> DepthFirstTraversal<'a, N, E> {
+    pub fn depth_traverse<'a>(
+        &'a self,
+        start: NodeIndex,
+        direction: Direction,
+    ) -> DepthFirstTraversal<'a, N, E> {
         DepthFirstTraversal::with_start_node(self, start, direction)
     }
 
-    pub fn nodes_in_postorder<'a>(&'a self,
-                                  direction: Direction,
-                                  entry_node: NodeIndex)
-                                  -> Vec<NodeIndex>
-    {
+    pub fn nodes_in_postorder<'a>(
+        &'a self,
+        direction: Direction,
+        entry_node: NodeIndex,
+    ) -> Vec<NodeIndex> {
         let mut visited = BitVector::new(self.len_nodes());
         let mut stack = vec![];
         let mut result = Vec::with_capacity(self.len_nodes());
@@ -268,7 +275,8 @@ impl<N: Debug, E: Debug> Graph<N, E> {
             }
         };
 
-        for node in Some(entry_node).into_iter()
+        for node in Some(entry_node)
+            .into_iter()
             .chain(self.enumerated_nodes().map(|(node, _)| node))
         {
             push_node(&mut stack, node);
@@ -293,50 +301,23 @@ impl<N: Debug, E: Debug> Graph<N, E> {
 
 // # Iterators
 
-pub struct EnumeratedNodes<'g, N>
-    where N: 'g,
-{
-    iter: ::std::iter::Enumerate<::std::slice::Iter<'g, Node<N>>>
-}
-
-impl<'g, N: Debug> Iterator for EnumeratedNodes<'g, N> {
-    type Item = (NodeIndex, &'g Node<N>);
-
-    fn next(&mut self) -> Option<(NodeIndex, &'g Node<N>)> {
-        self.iter.next().map(|(idx, n)| (NodeIndex(idx), n))
-    }
-}
-
-pub struct EnumeratedEdges<'g, E>
-    where E: 'g,
-{
-    iter: ::std::iter::Enumerate<::std::slice::Iter<'g, Edge<E>>>
-}
-
-impl<'g, E: Debug> Iterator for EnumeratedEdges<'g, E> {
-    type Item = (EdgeIndex, &'g Edge<E>);
-
-    fn next(&mut self) -> Option<(EdgeIndex, &'g Edge<E>)> {
-        self.iter.next().map(|(idx, e)| (EdgeIndex(idx), e))
-    }
-}
-
 pub struct AdjacentEdges<'g, N, E>
-    where N: 'g,
-          E: 'g
+where
+    N: 'g,
+    E: 'g,
 {
     graph: &'g Graph<N, E>,
     direction: Direction,
     next: EdgeIndex,
 }
 
-impl<'g, N, E> AdjacentEdges<'g, N, E> {
-    fn targets(self) -> AdjacentTargets<'g, N, E> {
-        AdjacentTargets { edges: self }
+impl<'g, N: Debug, E: Debug> AdjacentEdges<'g, N, E> {
+    fn targets(self) -> impl Iterator<Item = NodeIndex> + 'g {
+        self.into_iter().map(|(_, edge)| edge.target)
     }
 
-    fn sources(self) -> AdjacentSources<'g, N, E> {
-        AdjacentSources { edges: self }
+    fn sources(self) -> impl Iterator<Item = NodeIndex> + 'g {
+        self.into_iter().map(|(_, edge)| edge.source)
     }
 }
 
@@ -355,39 +336,10 @@ impl<'g, N: Debug, E: Debug> Iterator for AdjacentEdges<'g, N, E> {
     }
 }
 
-pub struct AdjacentTargets<'g, N, E>
-    where N: 'g,
-          E: 'g
-{
-    edges: AdjacentEdges<'g, N, E>,
-}
-
-impl<'g, N: Debug, E: Debug> Iterator for AdjacentTargets<'g, N, E> {
-    type Item = NodeIndex;
-
-    fn next(&mut self) -> Option<NodeIndex> {
-        self.edges.next().map(|(_, edge)| edge.target)
-    }
-}
-
-pub struct AdjacentSources<'g, N, E>
-    where N: 'g,
-          E: 'g
-{
-    edges: AdjacentEdges<'g, N, E>,
-}
-
-impl<'g, N: Debug, E: Debug> Iterator for AdjacentSources<'g, N, E> {
-    type Item = NodeIndex;
-
-    fn next(&mut self) -> Option<NodeIndex> {
-        self.edges.next().map(|(_, edge)| edge.source)
-    }
-}
-
 pub struct DepthFirstTraversal<'g, N, E>
-    where N: 'g,
-          E: 'g
+where
+    N: 'g,
+    E: 'g,
 {
     graph: &'g Graph<N, E>,
     stack: Vec<NodeIndex>,
@@ -396,10 +348,11 @@ pub struct DepthFirstTraversal<'g, N, E>
 }
 
 impl<'g, N: Debug, E: Debug> DepthFirstTraversal<'g, N, E> {
-    pub fn with_start_node(graph: &'g Graph<N, E>,
-                           start_node: NodeIndex,
-                           direction: Direction)
-                           -> Self {
+    pub fn with_start_node(
+        graph: &'g Graph<N, E>,
+        start_node: NodeIndex,
+        direction: Direction,
+    ) -> Self {
         let mut visited = BitVector::new(graph.len_nodes());
         visited.insert(start_node.node_id());
         DepthFirstTraversal {
diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs
index 265c6485830..81246aea1b5 100644
--- a/src/librustc_data_structures/lib.rs
+++ b/src/librustc_data_structures/lib.rs
@@ -34,6 +34,7 @@
 #![feature(underscore_lifetimes)]
 #![feature(macro_vis_matcher)]
 #![feature(allow_internal_unstable)]
+#![feature(universal_impl_trait)]
 
 #![cfg_attr(unix, feature(libc))]
 #![cfg_attr(test, feature(test))]