some more minor refactoring in constellation

This commit is contained in:
Tim Kuehn 2013-08-08 16:31:52 -07:00
parent bd5526de94
commit 2c44288c26

View file

@ -100,40 +100,37 @@ impl SendableFrameTree {
} }
impl FrameTree { impl FrameTree {
fn contains(&self, id: PipelineId) -> bool { fn contains(@mut self, id: PipelineId) -> bool {
self.pipeline.id == id || do self.iter().any |frame_tree| {
do self.children.iter().any |&ChildFrameTree { frame_tree: ref frame_tree, _ }| { id == frame_tree.pipeline.id
frame_tree.contains(id)
} }
} }
/// Returns the frame tree whose key is id /// Returns the frame tree whose key is id
fn find_mut(@mut self, id: PipelineId) -> Option<@mut FrameTree> { fn find_mut(@mut self, id: PipelineId) -> Option<@mut FrameTree> {
if self.pipeline.id == id { return Some(self); } do self.iter().find_ |frame_tree| {
let mut finder = do self.children.iter() id == frame_tree.pipeline.id
.filter_map |&ChildFrameTree { frame_tree: ref frame_tree, _ }| { }
frame_tree.find_mut(id)
};
finder.next()
} }
/// Replaces a node of the frame tree in place. Returns the node that was removed or the original node /// Replaces a node of the frame tree in place. Returns the node that was removed or the original node
/// if the node to replace could not be found. /// if the node to replace could not be found.
fn replace_child(&mut self, id: PipelineId, new_child: @mut FrameTree) -> Either<@mut FrameTree, @mut FrameTree> { fn replace_child(@mut self,
let new_child_cell = Cell::new(new_child); id: PipelineId,
for &ChildFrameTree { frame_tree: ref mut child, _ } in self.children.mut_iter() { new_child: @mut FrameTree)
let new_child = new_child_cell.take(); -> Result<@mut FrameTree, @mut FrameTree> {
if child.pipeline.id == id { let mut child = (do self.iter().filter_map |frame_tree| {
new_child.parent = child.parent; (do frame_tree.children.iter().find |child| {
return Left(replace(child, new_child)); child.frame_tree.pipeline.id == id
} }).map(|& &child| child)
let replaced = child.replace_child(id, new_child); }).next();
if replaced.is_left() { match child {
return replaced; Some(ref mut child) => {
new_child.parent = child.frame_tree.parent;
Left(replace(&mut child.frame_tree, new_child))
} }
new_child_cell.put_back(replaced.unwrap_right()); None => Right(new_child)
} }
Right(new_child_cell.take())
} }
fn to_sendable(&self) -> SendableFrameTree { fn to_sendable(&self) -> SendableFrameTree {