Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Help with writing a custom tree iterator
I am trying to write a tree data structure where I can use the following methods:
let tree: ProcTree = get_proc_tree(); // a process tree
let re = args.re;
let iter = tree.filter(|p| re.is_match(p.cmdline())) // include processes that match cmdline
.with_children() // recursively include children of matched processes
.filter(|p| should_include(p)) // filter out some more processes
.filter(|p| is_myself(p)); // filter out the current running process
// etc.
for proc in iter {
process_proc(proc);
}
For each of these I would also like to provide mutable versions which can mutate the inner process. Here's my progress so far: Rust Playground. The first compiler error is:
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:88:6
|
88 | impl<'a, I> Iterator for WithChildrenMut<I>
| ^^ unconstrained lifetime parameter
caused by
pub struct WithChildrenMut<I>
{
inner: I,
visit: vec::IntoIter<usize>,
}
impl<'a, I> Iterator for WithChildrenMut<I>
where
I: ProcTreeIterMutPrivate<'a>,
{
type Item = &'a mut ProcNode;
fn next(&mut self) -> Option<Self::Item> {
let Some(idx) = self.next_idx() else {
return None;
};
Some(self.tree_mut().tree.get_mut(idx).unwrap())
}
}
What I think I'm trying to express is, "this struct as an Iterator
returns a &'a mut ProcNode
as long as its inner ProcTreeIterMutPrivate<'a>
can give a &'a ProcTree
." But I don't know how to express it correctly.
3 posts - 2 participants
🏷️ rust_feed