Skip to main content

dear_imgui_rs/texture/
id.rs

1use crate::sys;
2
3/// Simple texture ID for backward compatibility
4///
5/// This is a simple wrapper around u64 that can be used to identify textures.
6/// For modern texture management, use TextureData instead.
7///
8/// Note: Changed from usize to u64 in Dear ImGui 1.91.4+ to support 64-bit handles
9/// like Vulkan and DX12 on 32-bit targets.
10#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
11#[repr(transparent)]
12pub struct TextureId(u64);
13
14impl TextureId {
15    /// Creates a new texture id with the given identifier
16    #[inline]
17    pub const fn new(id: u64) -> Self {
18        Self(id)
19    }
20
21    /// Returns the id of the TextureId
22    #[inline]
23    pub const fn id(self) -> u64 {
24        self.0
25    }
26
27    /// Creates a null texture ID
28    #[inline]
29    pub const fn null() -> Self {
30        Self(0)
31    }
32
33    /// Checks if this texture ID is null
34    #[inline]
35    pub const fn is_null(self) -> bool {
36        self.0 == 0
37    }
38
39    /// Try to view this texture id as a `usize`.
40    ///
41    /// Returns `None` if the id does not fit on this target.
42    pub fn try_as_usize(self) -> Option<usize> {
43        usize::try_from(self.0).ok()
44    }
45
46    /// Try to view this texture id as a raw pointer.
47    ///
48    /// Returns `None` if the id does not fit on this target.
49    pub fn try_as_ptr<T>(self) -> Option<*const T> {
50        self.try_as_usize().map(|value| value as *const T)
51    }
52
53    /// Try to view this texture id as a mutable raw pointer.
54    ///
55    /// Returns `None` if the id does not fit on this target.
56    pub fn try_as_mut_ptr<T>(self) -> Option<*mut T> {
57        self.try_as_usize().map(|value| value as *mut T)
58    }
59}
60
61impl From<u64> for TextureId {
62    #[inline]
63    fn from(id: u64) -> Self {
64        TextureId(id)
65    }
66}
67
68impl<T> From<*const T> for TextureId {
69    #[inline]
70    fn from(ptr: *const T) -> Self {
71        TextureId(ptr as usize as u64)
72    }
73}
74
75impl<T> From<*mut T> for TextureId {
76    #[inline]
77    fn from(ptr: *mut T) -> Self {
78        TextureId(ptr as usize as u64)
79    }
80}
81
82// Backward compatibility: allow conversion from usize for legacy code
83impl From<usize> for TextureId {
84    #[inline]
85    fn from(id: usize) -> Self {
86        TextureId(id as u64)
87    }
88}
89
90impl Default for TextureId {
91    #[inline]
92    fn default() -> Self {
93        Self::null()
94    }
95}
96
97/// Raw texture ID type for compatibility with Dear ImGui
98pub type RawTextureId = sys::ImTextureID;
99
100impl From<TextureId> for RawTextureId {
101    #[inline]
102    fn from(id: TextureId) -> Self {
103        id.id() as sys::ImTextureID
104    }
105}
106
107/// Stable identifier for an ImGui-managed texture.
108///
109/// This wraps Dear ImGui's `ImTextureData::UniqueID`. It is intended for correlating detached
110/// texture requests with renderer feedback, not as a renderer texture handle. Use [`TextureId`] for
111/// backend-owned GPU texture identifiers.
112#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
113#[repr(transparent)]
114pub struct ManagedTextureId(i32);
115
116impl ManagedTextureId {
117    #[inline]
118    pub(crate) const fn from_raw(raw: i32) -> Self {
119        Self(raw)
120    }
121
122    /// Returns Dear ImGui's raw `ImTextureData::UniqueID` value.
123    ///
124    /// Renderer backends can use this to derive deterministic backend texture handles for managed
125    /// texture requests without relying on probabilistic hashing.
126    #[inline]
127    pub const fn raw(self) -> i32 {
128        self.0
129    }
130}