From 4ee2ab5042c821dd7b9fa47df9cd63f86b74b784 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Mon, 18 Jul 2016 18:47:42 -0700 Subject: [PATCH] Implement IntoIterator for Layout. --- src/libcretonne/layout.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libcretonne/layout.rs b/src/libcretonne/layout.rs index e0ca2cc3cc..3cc2ffc03e 100644 --- a/src/libcretonne/layout.rs +++ b/src/libcretonne/layout.rs @@ -3,7 +3,7 @@ //! The order of extended basic blocks in a function and the order of instructions in an EBB is //! determined by the `Layout` data structure defined in this module. -use std::iter::Iterator; +use std::iter::{Iterator, IntoIterator}; use entity_map::{EntityMap, EntityRef}; use entities::{Ebb, NO_EBB, Inst, NO_INST}; @@ -135,6 +135,16 @@ impl<'a> Iterator for Ebbs<'a> { } } +/// Use a layout reference in a for loop. +impl<'a> IntoIterator for &'a Layout { + type Item = Ebb; + type IntoIter = Ebbs<'a>; + + fn into_iter(self) -> Ebbs<'a> { + self.ebbs() + } +} + /// Methods for arranging instructions. /// /// An instruction starts out as *not inserted* in the layout. An instruction can be inserted into @@ -267,6 +277,15 @@ mod tests { assert!(layout.is_ebb_inserted(e2)); let v: Vec = layout.ebbs().collect(); assert_eq!(v, [e1, e2, e0]); + + { + let imm = &layout; + let mut v = Vec::new(); + for e in imm { + v.push(e); + } + assert_eq!(v, [e1, e2, e0]); + } } #[test]