12 ms·
Note that by creating the reference to a local and passing it up through the callback, you are using a fresh region that can’t possibly outlive any of one of th
by cmrx64 2y ago
Note that by creating the reference to a local and passing it up through the callback, you are using a fresh region that can’t possibly outlive any of one of the ones you are generic over. Fundamentally, that callback could stash the reference you pass it into state somewhere and now the pointer has escaped, invalidated as soon as that iteration of the loops ends.
See that the definition of Group is tying those together. Instead, you can split them apart and maybe use HRTB to ensure the closure _must_ be able to treat the lifetime as fresh? But then you’ll probably have other issues…
… which can largely be circumvented simply by pinning, in your reduced example, which probably doesn’t retain enough detail.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b05e99cce303001b0e10fda815369457 https://play.rust-lang.org/?version=stable&mode=debug&editio...
My suggestion to avoid the pain? Use ArcStr.
- vlovich123 2y agoBut why does pinning solve the issue? Fundamentally the lifetime of the future is unchanged as far as the compiler is concerned so in theory the callback should be capable of doing the same stashing, no?
- cmrx64 2y agoThe lifetime _is_ changed; this lets you use the lifetime from the HRTB instead of the function generics. It’s not so much the pinning itself that does it, for the type system, but using the trait object enables referring to that HRTB to require true lifetime generic (and then pinning comes along for the ride).
- vlovich123 2y agoSwitched to 2024 and asyncfn completely removes the need for half the annotations :)