Skip to main content

kozan_platform/
id.rs

1//! Platform-level identifiers.
2//!
3//! All IDs are Kozan's own types — no windowing backend types leak.
4//! The backend (kozan-winit) maps between its IDs and ours internally.
5
6use kozan_primitives::arena::RawId;
7
8/// Unique identifier for a View.
9///
10/// Type-safe wrapper around [`RawId`]. `Copy + Send + Sync`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct ViewId(RawId);
13
14impl ViewId {
15    /// Create from a raw arena ID. Used by windowing backends.
16    #[inline]
17    #[must_use]
18    pub fn from_raw(raw: RawId) -> Self {
19        Self(raw)
20    }
21
22    #[inline]
23    #[must_use]
24    pub fn raw(self) -> RawId {
25        self.0
26    }
27}
28
29impl std::fmt::Display for ViewId {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(f, "View({})", self.0)
32    }
33}
34
35/// Unique identifier for a Window.
36///
37/// Kozan's own type — the windowing backend maps its native ID to this.
38/// `Copy + Send + Sync`.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40pub struct WindowId(u64);
41
42static NEXT_WINDOW_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
43
44impl WindowId {
45    /// Allocate a new unique `WindowId`.
46    pub fn next() -> Self {
47        Self(NEXT_WINDOW_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
48    }
49
50    /// Get the raw u64 value.
51    #[inline]
52    #[must_use]
53    pub fn raw(self) -> u64 {
54        self.0
55    }
56}
57
58impl std::fmt::Display for WindowId {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "Window({})", self.0)
61    }
62}