Skip to main content

spawn_local

Function spawn_local 

Source
pub fn spawn_local<T: 'static>(
    future: impl Future<Output = T> + 'static,
) -> JoinHandle<T> 
Expand description

Spawns a new future on the current thread.

This function may be used to spawn tasks when the message loop is not running. The provided future starts running once the message loop is entered with block_on() or MessageLoop::run().

§Examples

This example is a compile-time test to ensure that we only accept 'static return types to prevent https://github.com/rust-lang/rust/issues/84366.

fn test_fn<'a>() {
    let closure = || -> &'a str { "" };
    wintf_winmsg_executor::spawn_local(async move { closure() });
}
Examples found in repository?
examples/basic.rs (lines 22-27)
19fn main() {
20    println!("hello");
21    block_on(async {
22        let task = spawn_local(async {
23            println!("async hello 1");
24            poll_n_times(3).await;
25            println!("async bye 1");
26            "async 1 result"
27        });
28
29        println!("async hello 2");
30        poll_n_times(2).await;
31        println!("async bye 2");
32
33        println!("{}", task.await);
34    });
35    println!("bye");
36}