dear_imgui_rs/
internal.rs1#![allow(
7 clippy::cast_possible_truncation,
8 clippy::cast_sign_loss,
9 clippy::as_conversions
10)]
11use crate::sys;
12use std::ffi::c_int;
13use std::slice;
14
15#[repr(i32)]
17#[derive(Copy, Clone, Debug, Eq, PartialEq)]
18pub enum DataType {
19 I8 = sys::ImGuiDataType_S8 as i32,
20 U8 = sys::ImGuiDataType_U8 as i32,
21 I16 = sys::ImGuiDataType_S16 as i32,
22 U16 = sys::ImGuiDataType_U16 as i32,
23 I32 = sys::ImGuiDataType_S32 as i32,
24 U32 = sys::ImGuiDataType_U32 as i32,
25 I64 = sys::ImGuiDataType_S64 as i32,
26 U64 = sys::ImGuiDataType_U64 as i32,
27 F32 = sys::ImGuiDataType_Float as i32,
28 F64 = sys::ImGuiDataType_Double as i32,
29}
30
31pub unsafe trait DataTypeKind: Copy {
39 const KIND: DataType;
40}
41
42unsafe impl DataTypeKind for i8 {
43 const KIND: DataType = DataType::I8;
44}
45unsafe impl DataTypeKind for u8 {
46 const KIND: DataType = DataType::U8;
47}
48unsafe impl DataTypeKind for i16 {
49 const KIND: DataType = DataType::I16;
50}
51unsafe impl DataTypeKind for u16 {
52 const KIND: DataType = DataType::U16;
53}
54unsafe impl DataTypeKind for i32 {
55 const KIND: DataType = DataType::I32;
56}
57unsafe impl DataTypeKind for u32 {
58 const KIND: DataType = DataType::U32;
59}
60unsafe impl DataTypeKind for i64 {
61 const KIND: DataType = DataType::I64;
62}
63unsafe impl DataTypeKind for u64 {
64 const KIND: DataType = DataType::U64;
65}
66unsafe impl DataTypeKind for f32 {
67 const KIND: DataType = DataType::F32;
68}
69unsafe impl DataTypeKind for f64 {
70 const KIND: DataType = DataType::F64;
71}
72
73unsafe impl DataTypeKind for usize {
74 #[cfg(target_pointer_width = "16")]
75 const KIND: DataType = DataType::U16;
76
77 #[cfg(target_pointer_width = "32")]
78 const KIND: DataType = DataType::U32;
79
80 #[cfg(target_pointer_width = "64")]
81 const KIND: DataType = DataType::U64;
82
83 #[cfg(not(any(
86 target_pointer_width = "16",
87 target_pointer_width = "32",
88 target_pointer_width = "64"
89 )))]
90 compile_error!(
91 "cannot impl DataTypeKind for usize: unsupported target pointer width. supported values are 16, 32, 64"
92 );
93}
94
95unsafe impl DataTypeKind for isize {
96 #[cfg(target_pointer_width = "16")]
97 const KIND: DataType = DataType::I16;
98
99 #[cfg(target_pointer_width = "32")]
100 const KIND: DataType = DataType::I32;
101
102 #[cfg(target_pointer_width = "64")]
103 const KIND: DataType = DataType::I64;
104
105 #[cfg(not(any(
108 target_pointer_width = "16",
109 target_pointer_width = "32",
110 target_pointer_width = "64"
111 )))]
112 compile_error!(
113 "cannot impl DataTypeKind for isize: unsupported target pointer width. supported values are 16, 32, 64"
114 );
115}
116
117#[repr(C)]
121pub struct ImVector<T> {
122 pub(crate) size: c_int,
123 pub(crate) capacity: c_int,
124 pub(crate) data: *mut T,
125}
126
127impl<T> ImVector<T> {
128 #[inline]
130 pub fn as_slice(&self) -> &[T] {
131 if self.size <= 0 || self.data.is_null() {
132 return &[];
133 }
134 let len = match usize::try_from(self.size) {
135 Ok(len) => len,
136 Err(_) => return &[],
137 };
138 unsafe { slice::from_raw_parts(self.data, len) }
139 }
140
141 #[inline]
143 pub fn as_slice_mut(&mut self) -> &mut [T] {
144 if self.size <= 0 || self.data.is_null() {
145 return &mut [];
146 }
147 let len = match usize::try_from(self.size) {
148 Ok(len) => len,
149 Err(_) => return &mut [],
150 };
151 unsafe { slice::from_raw_parts_mut(self.data, len) }
152 }
153
154 #[inline]
156 pub fn len(&self) -> usize {
157 if self.size <= 0 {
158 return 0;
159 }
160 usize::try_from(self.size).unwrap_or(0)
161 }
162
163 #[inline]
165 pub fn is_empty(&self) -> bool {
166 self.size <= 0
167 }
168}
169
170impl<T> Default for ImVector<T> {
171 fn default() -> Self {
172 Self {
173 size: 0,
174 capacity: 0,
175 data: std::ptr::null_mut(),
176 }
177 }
178}
179
180impl<T> ImVector<T> {
181 #[inline]
183 pub fn iter(&self) -> slice::Iter<'_, T> {
184 self.as_slice().iter()
185 }
186
187 #[inline]
189 pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
190 self.as_slice_mut().iter_mut()
191 }
192}
193
194#[inline]
201pub unsafe fn imvector_cast_ref<T, R>(raw: &R) -> &ImVector<T> {
202 unsafe { &*(raw as *const R as *const ImVector<T>) }
203}
204
205#[inline]
211pub unsafe fn imvector_cast_mut<T, R>(raw: &mut R) -> &mut ImVector<T> {
212 unsafe { &mut *(raw as *mut R as *mut ImVector<T>) }
213}
214
215#[doc(alias = "UpdateHoveredWindowAndCaptureFlags")]
224pub fn update_hovered_window_and_capture_flags(mouse_pos: [f32; 2]) {
225 unsafe {
226 let pos = sys::ImVec2 {
227 x: mouse_pos[0],
228 y: mouse_pos[1],
229 };
230 sys::igUpdateHoveredWindowAndCaptureFlags(pos.into());
231 }
232}
233
234pub trait RawWrapper {
236 type Raw;
238 unsafe fn raw(&self) -> &Self::Raw;
245 unsafe fn raw_mut(&mut self) -> &mut Self::Raw;
252}
253
254pub unsafe trait RawCast<T>: Sized {
261 #[inline]
267 unsafe fn from_raw(raw: &T) -> &Self {
268 unsafe { &*(raw as *const _ as *const Self) }
269 }
270
271 #[inline]
277 unsafe fn from_raw_mut(raw: &mut T) -> &mut Self {
278 unsafe { &mut *(raw as *mut _ as *mut Self) }
279 }
280
281 #[inline]
287 unsafe fn raw(&self) -> &T {
288 unsafe { &*(self as *const _ as *const T) }
289 }
290
291 #[inline]
297 unsafe fn raw_mut(&mut self) -> &mut T {
298 unsafe { &mut *(self as *mut _ as *mut T) }
299 }
300}