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
use crate::service::{DynService, NamedService, Service, ServiceAdapter};
use potatonet_common::LocalServiceId;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
pub struct App {
pub(crate) services: Vec<(String, Box<DynService>)>,
pub(crate) services_map: HashMap<String, LocalServiceId>,
}
impl App {
pub fn new() -> Self {
Self {
services: Vec::new(),
services_map: HashMap::new(),
}
}
pub fn service<S>(self, service: S) -> Self
where
S: NamedService + 'static,
S::Req: Serialize + DeserializeOwned + Send,
S::Rep: Serialize + DeserializeOwned + Send,
S::Notify: Serialize + DeserializeOwned + Send,
{
self.service_with_name(service.name().to_string(), service)
}
pub fn service_with_name<N, S>(mut self, name: N, service: S) -> Self
where
N: Into<String>,
S: Service + 'static,
S::Req: Serialize + DeserializeOwned + Send,
S::Rep: Serialize + DeserializeOwned + Send,
S::Notify: Serialize + DeserializeOwned + Send,
{
let name = name.into();
self.services
.push((name.clone(), Box::new(ServiceAdapter(service))));
self.services_map
.insert(name, LocalServiceId((self.services.len() - 1) as u32));
self
}
}