arcdps_imgui\render/
renderer.rs
1use std::collections::HashMap;
2
3#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
5#[repr(transparent)]
6pub struct TextureId(usize);
7
8impl TextureId {
9 #[inline]
11 pub const fn new(id: usize) -> Self {
12 Self(id)
13 }
14
15 #[inline]
17 pub const fn id(self) -> usize {
18 self.0
19 }
20}
21
22impl From<usize> for TextureId {
23 #[inline]
24 fn from(id: usize) -> Self {
25 TextureId(id)
26 }
27}
28
29impl<T> From<*const T> for TextureId {
30 #[inline]
31 fn from(ptr: *const T) -> Self {
32 TextureId(ptr as usize)
33 }
34}
35
36impl<T> From<*mut T> for TextureId {
37 #[inline]
38 fn from(ptr: *mut T) -> Self {
39 TextureId(ptr as usize)
40 }
41}
42
43#[test]
44fn test_texture_id_memory_layout() {
45 use std::mem;
46 assert_eq!(
47 mem::size_of::<TextureId>(),
48 mem::size_of::<sys::ImTextureID>()
49 );
50 assert_eq!(
51 mem::align_of::<TextureId>(),
52 mem::align_of::<sys::ImTextureID>()
53 );
54}
55
56#[derive(Debug, Default)]
58pub struct Textures<T> {
59 textures: HashMap<usize, T>,
60 next: usize,
61}
62
63impl<T> Textures<T> {
64 pub fn new() -> Self {
67 Textures {
68 textures: HashMap::new(),
69 next: 0,
70 }
71 }
72
73 pub fn insert(&mut self, texture: T) -> TextureId {
74 let id = self.next;
75 self.textures.insert(id, texture);
76 self.next += 1;
77 TextureId::from(id)
78 }
79
80 pub fn replace(&mut self, id: TextureId, texture: T) -> Option<T> {
81 self.textures.insert(id.0, texture)
82 }
83
84 pub fn remove(&mut self, id: TextureId) -> Option<T> {
85 self.textures.remove(&id.0)
86 }
87
88 pub fn get(&self, id: TextureId) -> Option<&T> {
89 self.textures.get(&id.0)
90 }
91
92 pub fn get_mut(&mut self, id: TextureId) -> Option<&mut T> {
93 self.textures.get_mut(&id.0)
94 }
95}