Make it compile and work

timeout_old
Tassilo Horn 4 years ago
parent 0c3f87531e
commit 84822ec645
  1. 55
      src/lib.rs

@ -108,29 +108,36 @@ pub struct Node {
} }
impl Node { impl Node {
fn add_children<'a>(&'a self, v: &'a mut Vec<&'a Node>) { fn iter(&self) -> PreOrderNodeIter {
for n in self.nodes.iter() { PreOrderNodeIter::new(self)
v.push(&n);
n.add_children(v);
} }
} }
fn iter(&self) -> NodeIter { struct PreOrderNodeIter<'a> {
let mut v = vec![self]; stack: Vec<&'a Node>,
self.add_children(&mut v);
NodeIter { stack: v }
}
} }
struct NodeIter<'a> { impl<'a> PreOrderNodeIter<'a> {
stack: Vec<&'a Node>, fn new(node: &'a Node) -> PreOrderNodeIter {
PreOrderNodeIter { stack: vec![node] }
}
} }
impl<'a> Iterator for NodeIter<'a> { impl<'a> Iterator for PreOrderNodeIter<'a> {
type Item = &'a Node; type Item = &'a Node;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<Self::Item> {
self.stack.pop() if let Some(node) = self.stack.pop() {
for n in &node.nodes {
self.stack.push(&n);
}
for n in &node.floating_nodes {
self.stack.push(&n);
}
Some(node)
} else {
None
}
} }
} }
@ -163,7 +170,7 @@ pub struct Con<'a> {
impl<'a> std::fmt::Display for Con<'a> { impl<'a> std::fmt::Display for Con<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{} {}", self.name, self.app_id.unwrap_or("")) write!(f, "{} {}", self.app_id.unwrap_or(""), self.name)
} }
} }
@ -187,3 +194,21 @@ pub fn get_cons<'a>(tree: &'a Node) -> Vec<Con<'a>> {
} }
v v
} }
#[test]
fn test_get_cons() {
let tree = get_tree();
println!("Those IDs are in get_tree():");
for n in tree.iter() {
println!(" id: {}", n.id);
}
let cons = get_cons(&tree);
println!("There are {} cons.", cons.len());
for c in cons {
println!("{}", c);
}
}

Loading…
Cancel
Save