procstat/
contexts.rs

1#[derive(Debug)]
2pub struct Contexts {
3    pub total_switches: u64,
4}
5
6impl Contexts {
7    pub fn empty () -> Self {
8        Self {
9            total_switches: 0
10        }
11    }
12
13    pub fn from_vec(context_val: Vec<u64>) -> Self {
14        if context_val.len() == 0 { return Contexts::empty() }
15        
16        Self {
17            total_switches: *context_val.first().unwrap()
18        }
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use crate::contexts::Contexts;
25
26    #[test]
27    fn creates_context_from_vector() {
28        let test_data: Vec<u64> = vec![37115488136];
29        let contexts: Contexts = Contexts::from_vec(test_data);
30        
31        assert_eq!(contexts.total_switches, 37115488136);
32    }
33
34    #[test]
35    fn returns_default_if_vector_is_empty() {
36        let contexts: Contexts = Contexts::from_vec(Vec::new());
37        
38        assert_eq!(contexts.total_switches, 0);
39    }
40}