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
use crate::Session;
use std::sync::Arc;
#[derive(Default, Debug)]
pub struct Sessions {
db: Vec<Arc<Session>>,
}
impl Sessions {
pub fn len(&self) -> usize {
self.db.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn take(&mut self, client_id: &str) -> Option<Arc<Session>> {
self.db
.iter()
.position(|c| c.client_id() == client_id)
.map(|index| self.db.swap_remove(index))
}
pub fn get(&self, client_id: &str) -> Option<Arc<Session>> {
self.db
.iter()
.position(|c| c.client_id() == client_id)
.map(|index| self.db[index].clone())
}
pub fn add(&mut self, session: Arc<Session>) {
self.db.push(session);
}
pub fn iter(&self) -> SessionsIterator {
SessionsIterator {
inner_it: self.db.iter(),
}
}
}
pub struct SessionsIterator<'a> {
inner_it: std::slice::Iter<'a, Arc<Session>>,
}
impl<'a> Iterator for SessionsIterator<'a> {
type Item = &'a Arc<Session>;
fn next(&mut self) -> Option<Self::Item> {
self.inner_it.next()
}
}