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;

/// Holds sessions manipulated from the Command Loop
#[derive(Default, Debug)]
pub struct Sessions {
    db: Vec<Arc<Session>>,
}

impl Sessions {
    /// Returns the number of sessions
    pub fn len(&self) -> usize {
        self.db.len()
    }

    /// Returns true is the collection is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Searches for the Session at given index and returns it.
    /// If `take`  is set, the session will be extracted from the database
    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))
    }

    /// Returns the client given its id. If not client exist, returns None
    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())
    }

    /// Add the given session into the database
    pub fn add(&mut self, session: Arc<Session>) {
        self.db.push(session);
    }

    /// Returns an iterator over sessions
    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()
    }
}