For context, I am the maintainer of a crate that allows creating self-referencing structs (ouroboros
). I have had a couple of users run into trouble with structs that use lifetime parameters. Their errors could be resolved if it can be assumed that all lifetime parameters outlive the struct, but I want to make sure that it is always the case before I add that assumption to the crate. Here is an example of one of the patterns this assumption would enable, not particularly useful but illustrates the concept:
#[ouroboros::self_referencing]
struct RefHolder<'a> {
external_reference: &'a str,
#[borrows(external_reference)]
internal_reference: &'this str,
}
impl<'a> RefHolder<'a> {
fn make_new(external: &'a str) -> RefHolder<'a> {
RefHolderBuilder {
external_reference: external,
internal_reference_builder: |ext| &*ext,
}
}
}
In this case, no matter what I've tried, Rust will always throw an error if I try to drop whatever external
refers to as long as there is an instance of RefHolder
that is borrowing it, so the code is safe. Can this guarantee be extended to all lifetime parameters in general?