use std::slice;
#[repr(C)]
pub struct ImVector<T> {
size: i32,
capacity: i32,
pub(crate) data: *mut T,
}
impl<T> ImVector<T> {
pub fn as_slice(&self) -> &[T] {
unsafe { slice::from_raw_parts(self.data, self.size as usize) }
}
}
#[test]
fn test_imvector_memory_layout() {
use std::mem;
assert_eq!(
mem::size_of::<ImVector<u8>>(),
mem::size_of::<sys::ImVector_char>()
);
assert_eq!(
mem::align_of::<ImVector<u8>>(),
mem::align_of::<sys::ImVector_char>()
);
use memoffset::offset_of;
use sys::ImVector_char;
type VectorChar = ImVector<u8>;
macro_rules! assert_field_offset {
($l:ident, $r:ident) => {
assert_eq!(offset_of!(VectorChar, $l), offset_of!(ImVector_char, $r));
};
};
assert_field_offset!(size, Size);
assert_field_offset!(capacity, Capacity);
assert_field_offset!(data, Data);
}
pub trait RawWrapper {
type Raw;
unsafe fn raw(&self) -> &Self::Raw;
unsafe fn raw_mut(&mut self) -> &mut Self::Raw;
}
pub unsafe trait RawCast<T>: Sized {
unsafe fn from_raw(raw: &T) -> &Self {
&*(raw as *const _ as *const Self)
}
unsafe fn from_raw_mut(raw: &mut T) -> &mut Self {
&mut *(raw as *mut _ as *mut Self)
}
unsafe fn raw(&self) -> &T {
&*(self as *const _ as *const T)
}
unsafe fn raw_mut(&mut self) -> &mut T {
&mut *(self as *mut _ as *mut T)
}
}
#[repr(u32)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DataType {
I8 = sys::ImGuiDataType_S8,
U8 = sys::ImGuiDataType_U8,
I16 = sys::ImGuiDataType_S16,
U16 = sys::ImGuiDataType_U16,
I32 = sys::ImGuiDataType_S32,
U32 = sys::ImGuiDataType_U32,
I64 = sys::ImGuiDataType_S64,
U64 = sys::ImGuiDataType_U64,
F32 = sys::ImGuiDataType_Float,
F64 = sys::ImGuiDataType_Double,
}
pub unsafe trait DataTypeKind: Copy {
const KIND: DataType;
}
unsafe impl DataTypeKind for i8 {
const KIND: DataType = DataType::I8;
}
unsafe impl DataTypeKind for u8 {
const KIND: DataType = DataType::U8;
}
unsafe impl DataTypeKind for i16 {
const KIND: DataType = DataType::I16;
}
unsafe impl DataTypeKind for u16 {
const KIND: DataType = DataType::U16;
}
unsafe impl DataTypeKind for i32 {
const KIND: DataType = DataType::I32;
}
unsafe impl DataTypeKind for u32 {
const KIND: DataType = DataType::U32;
}
unsafe impl DataTypeKind for i64 {
const KIND: DataType = DataType::I64;
}
unsafe impl DataTypeKind for u64 {
const KIND: DataType = DataType::U64;
}
unsafe impl DataTypeKind for f32 {
const KIND: DataType = DataType::F32;
}
unsafe impl DataTypeKind for f64 {
const KIND: DataType = DataType::F64;
}