1//! Internal raw utilities (don't use unless you know what you're doing!)
23use std::slice;
45/// A generic version of the raw imgui-sys ImVector struct types
6#[repr(C)]
7pub struct ImVector<T> {
8 size: i32,
9 capacity: i32,
10pub(crate) data: *mut T,
11}
1213impl<T> ImVector<T> {
14#[inline]
15pub fn as_slice(&self) -> &[T] {
16unsafe { slice::from_raw_parts(self.data, self.size as usize) }
17 }
18}
1920#[test]
21#[cfg(test)]
22fn test_imvector_memory_layout() {
23use std::mem;
24assert_eq!(
25 mem::size_of::<ImVector<u8>>(),
26 mem::size_of::<sys::ImVector_char>()
27 );
28assert_eq!(
29 mem::align_of::<ImVector<u8>>(),
30 mem::align_of::<sys::ImVector_char>()
31 );
32use sys::ImVector_char;
33type VectorChar = ImVector<u8>;
34macro_rules! assert_field_offset {
35 ($l:ident, $r:ident) => {
36assert_eq!(
37memoffset::offset_of!(VectorChar, $l),
38memoffset::offset_of!(ImVector_char, $r)
39 );
40 };
41 }
42assert_field_offset!(size, Size);
43assert_field_offset!(capacity, Capacity);
44assert_field_offset!(data, Data);
45}
4647/// Marks a type as a transparent wrapper over a raw type
48pub trait RawWrapper {
49/// Wrapped raw type
50type 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.
57unsafe 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.
64unsafe fn raw_mut(&mut self) -> &mut Self::Raw;
65}
6667/// 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]
75unsafe 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]
84unsafe 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]
93unsafe 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]
102unsafe fn raw_mut(&mut self) -> &mut T {
103&mut *(self as *mut _ as *mut T)
104 }
105}
106107/// 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}
122123/// 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 {
128const KIND: DataType;
129}
130unsafe impl DataTypeKind for i8 {
131const KIND: DataType = DataType::I8;
132}
133unsafe impl DataTypeKind for u8 {
134const KIND: DataType = DataType::U8;
135}
136unsafe impl DataTypeKind for i16 {
137const KIND: DataType = DataType::I16;
138}
139unsafe impl DataTypeKind for u16 {
140const KIND: DataType = DataType::U16;
141}
142unsafe impl DataTypeKind for i32 {
143const KIND: DataType = DataType::I32;
144}
145unsafe impl DataTypeKind for u32 {
146const KIND: DataType = DataType::U32;
147}
148unsafe impl DataTypeKind for i64 {
149const KIND: DataType = DataType::I64;
150}
151unsafe impl DataTypeKind for u64 {
152const KIND: DataType = DataType::U64;
153}
154unsafe impl DataTypeKind for f32 {
155const KIND: DataType = DataType::F32;
156}
157unsafe impl DataTypeKind for f64 {
158const KIND: DataType = DataType::F64;
159}