#![allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::as_conversions
)]
use crate::sys;
use std::ffi::c_int;
use std::slice;
#[repr(i32)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DataType {
I8 = sys::ImGuiDataType_S8 as i32,
U8 = sys::ImGuiDataType_U8 as i32,
I16 = sys::ImGuiDataType_S16 as i32,
U16 = sys::ImGuiDataType_U16 as i32,
I32 = sys::ImGuiDataType_S32 as i32,
U32 = sys::ImGuiDataType_U32 as i32,
I64 = sys::ImGuiDataType_S64 as i32,
U64 = sys::ImGuiDataType_U64 as i32,
F32 = sys::ImGuiDataType_Float as i32,
F64 = sys::ImGuiDataType_Double as i32,
}
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;
}
unsafe impl DataTypeKind for usize {
#[cfg(target_pointer_width = "16")]
const KIND: DataType = DataType::U16;
#[cfg(target_pointer_width = "32")]
const KIND: DataType = DataType::U32;
#[cfg(target_pointer_width = "64")]
const KIND: DataType = DataType::U64;
#[cfg(not(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64"
)))]
compile_error!(
"cannot impl DataTypeKind for usize: unsupported target pointer width. supported values are 16, 32, 64"
);
}
unsafe impl DataTypeKind for isize {
#[cfg(target_pointer_width = "16")]
const KIND: DataType = DataType::I16;
#[cfg(target_pointer_width = "32")]
const KIND: DataType = DataType::I32;
#[cfg(target_pointer_width = "64")]
const KIND: DataType = DataType::I64;
#[cfg(not(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64"
)))]
compile_error!(
"cannot impl DataTypeKind for isize: unsupported target pointer width. supported values are 16, 32, 64"
);
}
#[repr(C)]
pub struct ImVector<T> {
pub(crate) size: c_int,
pub(crate) capacity: c_int,
pub(crate) data: *mut T,
}
impl<T> ImVector<T> {
#[inline]
pub fn as_slice(&self) -> &[T] {
if self.size <= 0 || self.data.is_null() {
return &[];
}
let len = match usize::try_from(self.size) {
Ok(len) => len,
Err(_) => return &[],
};
unsafe { slice::from_raw_parts(self.data, len) }
}
#[inline]
pub fn as_slice_mut(&mut self) -> &mut [T] {
if self.size <= 0 || self.data.is_null() {
return &mut [];
}
let len = match usize::try_from(self.size) {
Ok(len) => len,
Err(_) => return &mut [],
};
unsafe { slice::from_raw_parts_mut(self.data, len) }
}
#[inline]
pub fn len(&self) -> usize {
if self.size <= 0 {
return 0;
}
usize::try_from(self.size).unwrap_or(0)
}
#[inline]
pub fn is_empty(&self) -> bool {
self.size <= 0
}
}
impl<T> Default for ImVector<T> {
fn default() -> Self {
Self {
size: 0,
capacity: 0,
data: std::ptr::null_mut(),
}
}
}
impl<T> ImVector<T> {
#[inline]
pub fn iter(&self) -> slice::Iter<'_, T> {
self.as_slice().iter()
}
#[inline]
pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
self.as_slice_mut().iter_mut()
}
}
#[inline]
pub unsafe fn imvector_cast_ref<T, R>(raw: &R) -> &ImVector<T> {
unsafe { &*(raw as *const R as *const ImVector<T>) }
}
#[inline]
pub unsafe fn imvector_cast_mut<T, R>(raw: &mut R) -> &mut ImVector<T> {
unsafe { &mut *(raw as *mut R as *mut ImVector<T>) }
}
#[doc(alias = "UpdateHoveredWindowAndCaptureFlags")]
pub fn update_hovered_window_and_capture_flags(mouse_pos: [f32; 2]) {
unsafe {
let pos = sys::ImVec2 {
x: mouse_pos[0],
y: mouse_pos[1],
};
sys::igUpdateHoveredWindowAndCaptureFlags(pos.into());
}
}
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 {
#[inline]
unsafe fn from_raw(raw: &T) -> &Self {
unsafe { &*(raw as *const _ as *const Self) }
}
#[inline]
unsafe fn from_raw_mut(raw: &mut T) -> &mut Self {
unsafe { &mut *(raw as *mut _ as *mut Self) }
}
#[inline]
unsafe fn raw(&self) -> &T {
unsafe { &*(self as *const _ as *const T) }
}
#[inline]
unsafe fn raw_mut(&mut self) -> &mut T {
unsafe { &mut *(self as *mut _ as *mut T) }
}
}