Implement IntoIterator for Layout.

This commit is contained in:
Jakob Stoklund Olesen
2016-07-18 18:47:42 -07:00
parent e926674b4e
commit 4ee2ab5042

View File

@@ -3,7 +3,7 @@
//! The order of extended basic blocks in a function and the order of instructions in an EBB is //! 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. //! 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 entity_map::{EntityMap, EntityRef};
use entities::{Ebb, NO_EBB, Inst, NO_INST}; 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. /// Methods for arranging instructions.
/// ///
/// An instruction starts out as *not inserted* in the layout. An instruction can be inserted into /// 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)); assert!(layout.is_ebb_inserted(e2));
let v: Vec<Ebb> = layout.ebbs().collect(); let v: Vec<Ebb> = layout.ebbs().collect();
assert_eq!(v, [e1, e2, e0]); 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] #[test]