The following does not compile on stable, much to my surprise:
fn foo(ptr: &impl Tr) {
// For extending the lifetime of a reference you can
// *guarantee* will not escape the function it is extended in.
unsafe fn extend<'a, T>(t: &T) -> &'a T {
std::mem::transmute(t)
}
let _ = extend::<'static, _>(ptr);
}
Transmute (or perhaps the borrowck) appears to be unwilling to admit a &'static T if I don’t assert that T: 'static; this can be fixed by writing impl Copy + 'static. I don’t want to require this because my uses of this function are for something along the lines of
fn foo(ptr: &(impl Tr + Send)) {
// Extend this reference in order to make the borrow
// checker not attach a lifetime to the closure below.
// This is safe, because those closures are all destroyed by
// the join at the end of this function.
let ptr = extend::<'static, _>(ptr);
for .. {
thread_pool.exec(move || { ptr.bar(); ... });
}
// Ensure no extended references escape this function.
thread_pool.join();
}
There’s a workaround with dyn Tr + Send, but I’d like to know if this is an intentionally rejected program. If it is, this seems like a really weird place to take the edge off transmute.