Skip to main content

reovim_kernel/mm/
tab_id.rs

1//! Tab page identifier type.
2//!
3//! Linux equivalent: Tab handle identifier (mechanism only)
4//!
5//! This module provides a simple identifier type for tab pages, following the
6//! same pattern as `WindowId` and `BufferId`.
7
8use std::{
9    fmt,
10    sync::atomic::{AtomicUsize, Ordering},
11};
12
13/// Unique tab page identifier.
14///
15/// Tab pages are identified by a unique, monotonically increasing ID.
16/// This is a lightweight handle that can be cheaply cloned and compared.
17///
18/// # Example
19///
20/// ```
21/// use reovim_kernel::api::v1::TabId;
22///
23/// let tab1 = TabId::new();
24/// let tab2 = TabId::new();
25///
26/// assert_ne!(tab1, tab2);
27/// assert_eq!(tab1, tab1);
28/// ```
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub struct TabId(usize);
31
32/// Global tab ID counter.
33static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
34
35impl TabId {
36    /// Create a new unique tab ID.
37    #[must_use]
38    pub fn new() -> Self {
39        Self(NEXT_ID.fetch_add(1, Ordering::Relaxed))
40    }
41
42    /// Create a tab ID from a raw value.
43    ///
44    /// Used when converting from external tab ID representations.
45    #[must_use]
46    pub const fn from_raw(id: usize) -> Self {
47        Self(id)
48    }
49
50    /// Get the raw ID value.
51    #[must_use]
52    pub const fn as_usize(&self) -> usize {
53        self.0
54    }
55}
56
57impl Default for TabId {
58    fn default() -> Self {
59        Self::new()
60    }
61}
62
63impl fmt::Display for TabId {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "tab:{}", self.0)
66    }
67}
68
69#[cfg(test)]
70#[path = "tests/tab_id.rs"]
71mod tests;