Idea : Explicit And More Flexible Lifetime Annotation And Thread's At Exit Hook

There will no move. The core idea is it is not forced to be moved, because if it is moved then no difference with the standart spawn(move || { .... })

The goal is able to borrow heap reference without Arc aka zero cost, and without moving the value. So it will deactivate impl Drop. Let's the hook clean up the memory

That is the initial idea. But just now I realize there is problem, it is impossible to make the compiler know when the thread will finish. If there is other code that use the same value, but then the thread finish ealier than said other code, then it is dangling reference. If it is forced to be moved, then it is no different than what is currently available, the normal thread spawn with move. So I think placing the close inside at_exit hook is not safe :<

So there will some restriction. The use case become :

fn a() -> (JoinHandle<()>, String) {
    let val = String::from("Hello") until MyLifetime;
    let handle = thread::spawn(|| {

    });

    handle.at_exit(|| {
        close MyLifetime;
    });

    (handle, val)
}

fn b() {

    // simulating using the value in other code because it is not moved
    
    let (handle, val) = a();
    my_processing(&val);
    
    // waiting the thread here in other code block
    // automatically dropping the value after finish
    handle.join();
}

Currently to do that need raw pointer if want zero cost, which is unsafe. The available method to use value without moving, std::thread::scope force to wait the thread in place, as soon as it is called then the main thread is blocked, can not let it run in background first then waiting in other code

Me myself also not sure how to implement this technically in compiler level, but I share my idea anyway in case others know how to, is it reliable, etc. If it is really possible and achieved, it makes the code really more flexible and easier