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
use std::{cell::RefCell, future::Future, rc::Rc};
use derivative::Derivative;
use crate::{
client::traits::Client,
events::EventSource,
globals::{AnyGlobal, Bind},
};
pub trait Server: Sized {
type ClientContext: Client<ServerContext = Self>;
type Conn;
type Error;
type GlobalStore: GlobalStore<Self::Global>;
type Global: Bind<Self::ClientContext>
+ AnyGlobal<Object = <Self::ClientContext as Client>::Object>;
fn globals(&self) -> &RefCell<Self::GlobalStore>;
fn new_connection(&self, conn: Self::Conn) -> Result<(), Self::Error>;
}
#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub enum GlobalsUpdate<G> {
Added(u32, Rc<G>),
Removed(u32),
}
impl<G> std::fmt::Debug for GlobalsUpdate<G> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GlobalsUpdate::Added(id, _) => f.debug_tuple("Added").field(id).field(&"...").finish(),
GlobalsUpdate::Removed(id) => f.debug_tuple("Removed").field(id).finish(),
}
}
}
pub trait GlobalStore<G>: EventSource<GlobalsUpdate<G>> {
type Iter<'a>: Iterator<Item = (u32, &'a Rc<G>)>
where
Self: 'a,
G: 'a;
type InsertFut<'a, IntoG>: Future<Output = u32> + 'a
where
Self: 'a,
IntoG: 'a + Into<G>;
type RemoveFut<'a>: Future<Output = bool> + 'a
where
Self: 'a;
fn insert<'a, IntoG: Into<G> + 'a>(&'a mut self, global: IntoG) -> Self::InsertFut<'a, IntoG>;
fn get(&self, id: u32) -> Option<&Rc<G>>;
fn remove(&mut self, id: u32) -> Self::RemoveFut<'_>;
fn iter(&self) -> Self::Iter<'_>;
}