struct A;
struct B<'x> { a: &'x mut A}
struct C<'x> { b: &'x mut B<'x>}
struct D<'x> { c: &'x mut C<'x>}
impl<'x> B<'x> {
pub fn new(a: &'x mut A) -> Self {
B { a }
}
}
impl<'x> C<'x> {
pub fn new(b: &'x mut B<'x>) -> Self {
C { b }
}
}
impl<'x> D<'x> {
pub fn new(c: &'x mut C<'x>) -> Self {
D { c }
}
}
fn build_it<F: FnOnce(&mut D)>(a: &mut A, func: F) {
let mut b = B::new(a);
let mut c = C::new(&mut b);
let mut d = D::new(&mut c);
func(&mut d)
let _ = d.c.b.a;
}
fn my_func(d: &mut D) {
let _ = d.c.b.a;
}
fn main() {
let mut a = A{};
build_context_and_run(&mut a, my_func);
//Done!
}
This works, but I've been frequently told that it's bad to write any rust that uses a &'a mut Foo<'a>.
I've built one or two database applications using this pattern to let me separate out different layers of abstraction.
It doesn't feel like it should be valid. How did I get away with this? Is it semantically valid? (honestly, this was just something I tried because I didn't want to write out 8 different lifetimes, and it compiled and worked)
In real life, there are one or two more layers of nesting, parameters, and other cruft that are not shown here.