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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::{event::Bus, resource::ResourceHandle, worker::Worker};
use bee_storage::backend::StorageBackend;
use async_trait::async_trait;
use futures::{channel::oneshot, future::Future};
use std::any::Any;
pub struct NodeInfo {
pub name: String,
pub version: String,
}
#[async_trait]
pub trait Node: Send + Sized + 'static {
type Builder: NodeBuilder<Self>;
type Backend: StorageBackend;
type Error: std::error::Error;
async fn stop(mut self) -> Result<(), Self::Error>;
fn spawn<W, G, F>(&mut self, g: G)
where
W: Worker<Self>,
G: FnOnce(oneshot::Receiver<()>) -> F,
F: Future<Output = ()> + Send + 'static;
fn worker<W>(&self) -> Option<&W>
where
W: Worker<Self> + Send + Sync;
fn register_resource<R: Any + Send + Sync>(&mut self, res: R);
fn remove_resource<R: Any + Send + Sync>(&mut self) -> Option<R>;
#[track_caller]
fn resource<R: Any + Send + Sync>(&self) -> ResourceHandle<R>;
#[track_caller]
fn info(&self) -> ResourceHandle<NodeInfo> {
self.resource()
}
#[track_caller]
fn storage(&self) -> ResourceHandle<Self::Backend> {
self.resource()
}
#[track_caller]
fn bus(&self) -> ResourceHandle<Bus<'static>> {
self.resource()
}
}
#[async_trait(?Send)]
pub trait NodeBuilder<N: Node>: Sized {
type Error: std::error::Error;
type Config;
fn new(config: Self::Config) -> Result<Self, Self::Error>;
fn with_worker<W: Worker<N> + 'static>(self) -> Self
where
W::Config: Default;
fn with_worker_cfg<W: Worker<N> + 'static>(self, config: W::Config) -> Self;
fn with_resource<R: Any + Send + Sync>(self, res: R) -> Self;
async fn finish(self) -> Result<N, Self::Error>;
}