Rust has RefCell
to move &/&mut
checks to runtime. Is there any way to do something similar with variance? I frequently encounter situations where something suddenly is invariant and to change it I will need to change the library I am using or introduce allocations... I would rather somehow got a lifetime out of thin air with some runtime checks (to not have &'a mut Foo<'a>
). Do you know something that can do that?
I don't follow. RefCell
already prevents the invariant lifetime foot-gun because borrow_mut()
constrains the lifetime to one shorter than 'self
.
The most common way to get an invariant lifetime is by calling a function with that specific signature. So the question is why are you calling such a function? I'm afraid the premise as described is too vague for me to make any suggestions.
Without a specific example, the only thing I can think of that might apply is to make your lifetime invariant and do runtime checking of all "upcasts".
1 Like