arcdps_imgui/
internal.rs

1//! Internal raw utilities (don't use unless you know what you're doing!)
2
3use std::slice;
4
5/// A generic version of the raw imgui-sys ImVector struct types
6#[repr(C)]
7pub struct ImVector<T> {
8    size: i32,
9    capacity: i32,
10    pub(crate) data: *mut T,
11}
12
13impl<T> ImVector<T> {
14    #[inline]
15    pub fn as_slice(&self) -> &[T] {
16        unsafe { slice::from_raw_parts(self.data, self.size as usize) }
17    }
18}
19
20#[test]
21#[cfg(test)]
22fn test_imvector_memory_layout() {
23    use std::mem;
24    assert_eq!(
25        mem::size_of::<ImVector<u8>>(),
26        mem::size_of::<sys::ImVector_char>()
27    );
28    assert_eq!(
29        mem::align_of::<ImVector<u8>>(),
30        mem::align_of::<sys::ImVector_char>()
31    );
32    use sys::ImVector_char;
33    type VectorChar = ImVector<u8>;
34    macro_rules! assert_field_offset {
35        ($l:ident, $r:ident) => {
36            assert_eq!(
37                memoffset::offset_of!(VectorChar, $l),
38                memoffset::offset_of!(ImVector_char, $r)
39            );
40        };
41    }
42    assert_field_offset!(size, Size);
43    assert_field_offset!(capacity, Capacity);
44    assert_field_offset!(data, Data);
45}
46
47/// Marks a type as a transparent wrapper over a raw type
48pub trait RawWrapper {
49    /// Wrapped raw type
50    type Raw;
51    /// Returns an immutable reference to the wrapped raw value
52    ///
53    /// # Safety
54    ///
55    /// It is up to the caller to use the returned raw reference without causing undefined
56    /// behaviour or breaking safety rules.
57    unsafe fn raw(&self) -> &Self::Raw;
58    /// Returns a mutable reference to the wrapped raw value
59    ///
60    /// # Safety
61    ///
62    /// It is up to the caller to use the returned mutable raw reference without causing undefined
63    /// behaviour or breaking safety rules.
64    unsafe fn raw_mut(&mut self) -> &mut Self::Raw;
65}
66
67/// Casting from/to a raw type that has the same layout and alignment as the target type
68pub unsafe trait RawCast<T>: Sized {
69    /// Casts an immutable reference from the raw type
70    ///
71    /// # Safety
72    ///
73    /// It is up to the caller to guarantee the cast is valid.
74    #[inline]
75    unsafe fn from_raw(raw: &T) -> &Self {
76        &*(raw as *const _ as *const Self)
77    }
78    /// Casts a mutable reference from the raw type
79    ///
80    /// # Safety
81    ///
82    /// It is up to the caller to guarantee the cast is valid.
83    #[inline]
84    unsafe fn from_raw_mut(raw: &mut T) -> &mut Self {
85        &mut *(raw as *mut _ as *mut Self)
86    }
87    /// Casts an immutable reference to the raw type
88    ///
89    /// # Safety
90    ///
91    /// It is up to the caller to guarantee the cast is valid.
92    #[inline]
93    unsafe fn raw(&self) -> &T {
94        &*(self as *const _ as *const T)
95    }
96    /// Casts a mutable reference to the raw type
97    ///
98    /// # Safety
99    ///
100    /// It is up to the caller to guarantee the cast is valid.
101    #[inline]
102    unsafe fn raw_mut(&mut self) -> &mut T {
103        &mut *(self as *mut _ as *mut T)
104    }
105}
106
107/// A primary data type
108#[repr(u32)]
109#[derive(Copy, Clone, Debug, Eq, PartialEq)]
110pub enum DataType {
111    I8 = sys::ImGuiDataType_S8,
112    U8 = sys::ImGuiDataType_U8,
113    I16 = sys::ImGuiDataType_S16,
114    U16 = sys::ImGuiDataType_U16,
115    I32 = sys::ImGuiDataType_S32,
116    U32 = sys::ImGuiDataType_U32,
117    I64 = sys::ImGuiDataType_S64,
118    U64 = sys::ImGuiDataType_U64,
119    F32 = sys::ImGuiDataType_Float,
120    F64 = sys::ImGuiDataType_Double,
121}
122
123/// Primitive type marker.
124///
125/// If this trait is implemented for a type, it is assumed to have *exactly* the same
126/// representation in memory as the primitive value described by the associated `KIND` constant.
127pub unsafe trait DataTypeKind: Copy {
128    const KIND: DataType;
129}
130unsafe impl DataTypeKind for i8 {
131    const KIND: DataType = DataType::I8;
132}
133unsafe impl DataTypeKind for u8 {
134    const KIND: DataType = DataType::U8;
135}
136unsafe impl DataTypeKind for i16 {
137    const KIND: DataType = DataType::I16;
138}
139unsafe impl DataTypeKind for u16 {
140    const KIND: DataType = DataType::U16;
141}
142unsafe impl DataTypeKind for i32 {
143    const KIND: DataType = DataType::I32;
144}
145unsafe impl DataTypeKind for u32 {
146    const KIND: DataType = DataType::U32;
147}
148unsafe impl DataTypeKind for i64 {
149    const KIND: DataType = DataType::I64;
150}
151unsafe impl DataTypeKind for u64 {
152    const KIND: DataType = DataType::U64;
153}
154unsafe impl DataTypeKind for f32 {
155    const KIND: DataType = DataType::F32;
156}
157unsafe impl DataTypeKind for f64 {
158    const KIND: DataType = DataType::F64;
159}