Skip to main content

substrate_data/model/atom/
table.rs

1//! The process's one table of names: installed by the runtime, else the crate's own.
2
3use super::local;
4use crate::sync::OnceLock;
5
6/// The table's two functions: a name's number (made the first time), and
7/// a number's name (`None`: no such atom).
8#[derive(Debug, Clone, Copy)]
9pub struct Table {
10    /// A name's number, made the first time.
11    pub intern: fn(&str) -> u32,
12    /// A number's name (`None`: no such atom).
13    pub name: fn(u32) -> Option<&'static str>,
14}
15
16static TABLE: OnceLock<Table> = OnceLock::new();
17
18/// Points this copy of the crate at the process's table (the runtime's);
19/// before any atom is made. False if it has one already.
20pub fn install(table: Table) -> bool {
21    TABLE.set(table).is_ok()
22}
23
24/// The process's table: the installed one, else this crate's own.
25pub(super) fn table() -> &'static Table {
26    TABLE.get_or_init(|| Table {
27        intern: local::intern,
28        name: local::name,
29    })
30}