crb_runtime/
runtime.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
//! A runtime for composable blocks.

use crate::context::Context;
use crate::interruptor::Interruptor;
use async_trait::async_trait;
use std::ops::DerefMut;

/// A runtime that can be executed by a supervisor.
#[async_trait]
pub trait Runtime: Send + 'static {
    /// Used by a lifetime tracker of the supervisor to stop it.
    /// It's the separate type that wraps address made by a runtime.
    fn get_interruptor(&mut self) -> Interruptor;

    async fn routine(&mut self);
}

pub trait InteractiveRuntime: Runtime {
    /// Type of the composable block's contenxt.
    type Context: Context;

    fn address(&self) -> <Self::Context as Context>::Address;
}

#[async_trait]
impl Runtime for Box<dyn Runtime> {
    fn get_interruptor(&mut self) -> Interruptor {
        self.deref_mut().get_interruptor()
    }

    async fn routine(&mut self) {
        self.deref_mut().routine().await
    }
}