summaryrefslogtreecommitdiff
path: root/examples/spawn-local.rs
blob: a9da1b4de98821e6b3c19d37c9e5736682aa9522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! A simple single-threaded executor that can spawn non-`Send` futures.

use std::cell::Cell;
use std::future::Future;
use std::rc::Rc;

use async_task::{Runnable, Task};

thread_local! {
    // A queue that holds scheduled tasks.
    static QUEUE: (flume::Sender<Runnable>, flume::Receiver<Runnable>) = flume::unbounded();
}

/// Spawns a future on the executor.
fn spawn<F, T>(future: F) -> Task<T>
where
    F: Future<Output = T> + 'static,
    T: 'static,
{
    // Create a task that is scheduled by pushing itself into the queue.
    let schedule = |runnable| QUEUE.with(|(s, _)| s.send(runnable).unwrap());
    let (runnable, task) = async_task::spawn_local(future, schedule);

    // Schedule the task by pushing it into the queue.
    runnable.schedule();

    task
}

/// Runs a future to completion.
fn run<F, T>(future: F) -> T
where
    F: Future<Output = T> + 'static,
    T: 'static,
{
    // Spawn a task that sends its result through a channel.
    let (s, r) = flume::unbounded();
    spawn(async move { drop(s.send(future.await)) }).detach();

    loop {
        // If the original task has completed, return its result.
        if let Ok(val) = r.try_recv() {
            return val;
        }

        // Otherwise, take a task from the queue and run it.
        QUEUE.with(|(_, r)| r.recv().unwrap().run());
    }
}

fn main() {
    let val = Rc::new(Cell::new(0));

    // Run a future that increments a non-`Send` value.
    run({
        let val = val.clone();
        async move {
            // Spawn a future that increments the value.
            let task = spawn({
                let val = val.clone();
                async move {
                    val.set(dbg!(val.get()) + 1);
                }
            });

            val.set(dbg!(val.get()) + 1);
            task.await;
        }
    });

    // The value should be 2 at the end of the program.
    dbg!(val.get());
}