Reference Bundle

Hello,

These "no" cases are the functionality of the API that I would like to accomplish.

fn main() {
    let op = B { op: Box::new(|_| {}) };
    let mut u = U {};
    let mut v = V {};
    for _ in 0..10 {
        // (op.op)(&mut u); // yes
        
        // (op.op)(&mut v); // yes
        
        // (op.op)(&mut (&u, &v)); // no

        // struct UV<'a> { // no
        //     u: &'a mut U,
        //     v: &'a mut V
        // }
        // (op.op)(&mut UV {u: &mut u, v: &mut v});
    }
}

struct U;
struct V;

struct B<T> {
    op: Box<dyn Fn(&mut T)>
}

My understanding is that in both the "no" cases the generic T in B is resolving to a single lifetime like B<T<'a>> & to the compiler it looks as if B will store T<'a>.

What are my options to move forward?
Is there a way to allow the users of the API to bundle & pass whatever mutable state they want to?

Thank you for your help in advance!

Yes.

You can think of it that way if you like, but it's more like the single lifetime is part of the type that is assumed to have a destructor, so the lifetime has to be alive at the drop points.

That's mostly a pedantic correction for this particular case I suppose.

Depends on the goal. Declaring op after u and v works for the playground.

There's no fully general solution for "closure higher-ranked over an arbitrary number of lifetimes". So ultimately, probably, it will be a task for users of the API to work around.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.