sparreal_kernel/driver/
id.rs

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
use crate::sync::RwLock;
use alloc::{
    collections::btree_map::BTreeMap,
    string::{String, ToString},
};

pub use driver_interface::DeviceId;

static MANAGER: RwLock<IdManager> = RwLock::new(IdManager::new());

pub fn device_id_by_node_name(name: &str) -> DeviceId {
    MANAGER.write().id_by_node_name(name)
}
pub fn device_id_next() -> DeviceId {
    let mut g = MANAGER.write();
    g.id_iter += 1;
    g.id_iter.into()
}

struct IdManager {
    id_iter: u64,
    node_name_id_map: BTreeMap<String, DeviceId>,
}

impl IdManager {
    const fn new() -> Self {
        Self {
            id_iter: 0,
            node_name_id_map: BTreeMap::new(),
        }
    }

    pub fn id_by_node_name(&mut self, name: &str) -> DeviceId {
        *self
            .node_name_id_map
            .entry(name.to_string())
            .or_insert_with(|| {
                self.id_iter += 1;
                self.id_iter.into()
            })
    }
}