crb_runtime/
task.rs

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
74
75
76
77
78
79
80
81
82
use crate::context::Context;
use crate::interruptor::Interruptor;
use crate::runtime::{InteractiveRuntime, Runtime};
use async_trait::async_trait;
use crb_core::JoinHandle;
use derive_more::{Deref, DerefMut};
use std::marker::PhantomData;

pub trait InteractiveTask<T>: Task<T> + InteractiveRuntime {
    fn spawn_connected(self) -> <Self::Context as Context>::Address {
        let address = self.address();
        self.spawn();
        address
    }
}

#[async_trait]
pub trait Task<T = ()>: Runtime + Sized {
    fn spawn(mut self) -> TaskHandle<T> {
        let interruptor = self.get_interruptor();
        let handle = crb_core::spawn(async move {
            self.routine().await;
        });
        let job = JobHandle {
            interruptor,
            handle,
            cancel_on_drop: false,
        };
        TaskHandle {
            job,
            _task: PhantomData,
        }
    }

    async fn run(mut self) {
        self.routine().await;
    }
}

#[derive(Deref, DerefMut)]
pub struct TaskHandle<T> {
    #[deref]
    #[deref_mut]
    job: JobHandle,
    _task: PhantomData<T>,
}

impl<T> TaskHandle<T> {
    pub fn job(self) -> JobHandle {
        self.into()
    }
}

impl<T> From<TaskHandle<T>> for JobHandle {
    fn from(task_handle: TaskHandle<T>) -> Self {
        task_handle.job
    }
}

pub struct JobHandle {
    interruptor: Interruptor,
    handle: JoinHandle<()>,
    cancel_on_drop: bool,
}

impl JobHandle {
    pub fn cancel_on_drop(&mut self, cancel: bool) {
        self.cancel_on_drop = cancel;
    }

    pub fn interrupt(&mut self) {
        self.interruptor.stop(true);
    }
}

impl Drop for JobHandle {
    fn drop(&mut self) {
        if self.cancel_on_drop {
            self.handle.abort();
        }
    }
}