iced_core/window/
id.rs

1use std::fmt;
2use std::hash::Hash;
3use std::sync::atomic::{self, AtomicU64};
4
5/// The id of the window.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct Id(u64);
8
9static COUNT: AtomicU64 = AtomicU64::new(1);
10
11impl Id {
12    /// No window will match this Id
13    pub const NONE: Id = Id(0);
14    pub const RESERVED: Id = Id(1);
15
16    /// Creates a new unique window [`Id`].
17    pub fn unique() -> Id {
18        let id = Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed));
19        if id.0 == 0 {
20            Id(COUNT.fetch_add(2, atomic::Ordering::Relaxed))
21        } else if id.0 == 1 {
22            Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
23        } else {
24            id
25        }
26    }
27}
28
29impl fmt::Display for Id {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        self.0.fmt(f)
32    }
33}