tawasuyu

tawasuyu/mirada

Sovereign tiling Wayland compositor + window manager in Rust, on Llimphi.

Rust 50Markdown 30TOML 211282 Sutichaykuna

⎇ main vendor/smithay/src/utils/ids.rs Rust · 36 siq'ikuna · 974 B Ñawpa willay Pi imata llamkarqan ch'uya

chore: refresco desde el monorepo (pin tawasuyu.git v0.2.0) a91da3fea · Sergio ·

1macro_rules! id_gen {
2    ($mod_name:ident) => {
3        mod $mod_name {
4            use std::{
5                collections::HashSet,
6                sync::{LazyLock, Mutex},
7            };
8
9            static ID_DATA: LazyLock<Mutex<(HashSet<usize>, usize)>> =
10                LazyLock::new(|| Mutex::new((HashSet::new(), 0)));
11
12            pub(crate) fn next() -> usize {
13                let (id_set, counter) = &mut *ID_DATA.lock().unwrap();
14
15                if id_set.len() == usize::MAX {
16                    panic!("Out of ids");
17                }
18
19                while !id_set.insert(*counter) {
20                    *counter = counter.wrapping_add(1);
21                }
22
23                let new_id = *counter;
24                *counter = counter.wrapping_add(1);
25
26                new_id
27            }
28
29            pub(crate) fn remove(id: usize) -> bool {
30                ID_DATA.lock().unwrap().0.remove(&id)
31            }
32        }
33    };
34}
35
36pub(crate) use id_gen;