aboutsummaryrefslogtreecommitdiff
path: root/macros/src/util.rs
blob: 45f1feeff0dd08c0d8a7f97020ff8a722f2ea45f (plain)
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
use std::collections::HashMap;

use syn::Ident;

use syntax::App;

pub type Ceilings = HashMap<Ident, Ceiling>;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Ceiling {
    Owned,
    Shared(u8),
}

impl Ceiling {
    pub fn is_owned(&self) -> bool {
        *self == Ceiling::Owned
    }
}

pub fn compute_ceilings(app: &App) -> Ceilings {
    let mut ceilings = HashMap::new();

    for resource in &app.idle.resources {
        ceilings.insert(resource.clone(), Ceiling::Owned);
    }

    for task in app.tasks.values() {
        for resource in &task.resources {
            if let Some(ceiling) = ceilings.get_mut(resource) {
                match *ceiling {
                    Ceiling::Owned => *ceiling = Ceiling::Shared(task.priority),
                    Ceiling::Shared(old) => {
                        if task.priority > old {
                            *ceiling = Ceiling::Shared(task.priority);
                        }
                    }
                }

                continue;
            }

            ceilings.insert(resource.clone(), Ceiling::Owned);
        }
    }

    ceilings
}