I think I have a question about the "lifetime" of a Future returned by a function... although I'm not sure this is the right word for it, because "lifetime" usually refers to a reference. But what I mean is: I want to return a Future which holds a reference to some data, and I want the caller to guarantee/compiler to enforce that the return promise will be used or thrown away by that particular named lifetime.
Is there a way to explicitly annotate this kind of promise?
In addition, is there a way to annotate an async block so that the Future it produces has this explicit named lifetime instead of 'static?
Example code. This function works because the compiler knows that the future is immediately awaited:
async fn test_works() {
let s = "ref";
let promise = async {
println!("Running async block: {}", s);
};
promise.await
}
I'd like to replicate this by having multiple functions and replacing the automatic lifetime inference with something explicit. e.g.:
fn test_broken_inner(s: &str) -> impl std::future::Future {
async {
println!("Running async block: {}", s);
}
}
async fn test_broken_outer() {
let s = "broken";
test_broken_inner(s).await;
}
Is there any way to explicitly annotate the future like that? I tried some explicit lifetimes but they don't work. Is this a place where PhantomData would somehow help identify the lifetime of the Future? (can traits express phantom lifetime relationships, or only concrete types?)
fn test_broken_inner<'a>(s: &'a str) -> impl std::future::Future<'a> {