Hi,
I'm able to create an iterator for iterating through an object hierarchy, but I need to defer some slow computation and be able to access data from higher levels of the tree if required.
It's quite easy to do with unsafe Rust, and I know that the code is memory safe, but I can't prove it to Rust with setting the right lifetimes.
Here's a code that I created for minimizing the problem:
struct S<'a,'p> {
data: &'a mut Option<u32>,
e: E<'a,'p>
}
enum E<'a,'p> {
Root(),
Parent(&'p mut S<'a,'p>)
}
fn get(s:&mut S)->u32 {
match(s.data) {
Some(r)=>*r,
None=>{
let E::Parent(p)=&mut s.e else {
panic!("!!!");
};
let rr=get(p);
*s.data=Some(rr);
rr
}
}
}
struct Data {
extract: bool,
data: Option<u32>,
sub: Option<Box<Data>>
}
fn walk<'a,'p>(d:&'a mut Data, mut s:S<'a,'p>) {
if(d.extract) {
get(&mut s);
}
if let Some(sub)=&mut d.sub {
walk(sub.as_mut(), S {e: E::Parent(&mut s), data: &mut d.data})
}
}
fn test() {
let mut data=Data {extract: false, data: Some(5),
sub: Some(Box::new(
Data {extract: false, data: None, sub: Some(Box::new(
Data {extract: false, data: None, sub: None}
))}
))};
walk(&mut data.sub.unwrap(), S {data: &mut data.data, e: E::Root()});
}
PS: It seems my previous post was taken down by spam filter when I was trying to fix formatting, so I tried again.