arcdps_imgui_sys/
lib.rs

1#![no_std]
2// We use `chlorine` over (the more well known) `cty` right now since `cty`
3// doesn't fully match std::os::raw (leading to issues like
4// https://github.com/japaric/cty/issues/18). Chlorine *does* match std::os::raw
5// (and libc), but has a longer and more confusing name, so we just alias it.
6// Also, this makes it easier to switch to something else/back easier, if we
7// decide to.
8//
9// Note that with the exception of bugs like the above, which crate we use (cty,
10// chlorine, libc, std::os::raw, ...) shouldn't matter to end user code¹, since
11// these are type aliases that should all be equivalent. This means that we're
12// free to switch back iff the bug is fixed, and users are free to use whichever
13// they prefer regardless of what we chose.
14//
15// (TODO: using extern crate for this is a hack, we probably should replace this
16// with `use chlorine as cty` in the binding files eventually, but lets punt on
17// it for a bit)
18//
19// ¹ The exception to this is that `std::os::raw` isn't there for `no_std`, and
20// `libc` has potentially undesirable linking impacts on windows.
21pub extern crate chlorine as cty;
22
23#[cfg(feature = "wasm")]
24mod wasm_bindings;
25
26#[cfg(feature = "wasm")]
27pub use crate::wasm_bindings::*;
28
29#[cfg(not(feature = "wasm"))]
30mod bindings;
31
32#[cfg(not(feature = "wasm"))]
33pub use crate::bindings::*;
34
35impl ImVec2 {
36    #[inline]
37    pub const fn new(x: f32, y: f32) -> ImVec2 {
38        ImVec2 { x, y }
39    }
40    #[inline]
41    pub const fn zero() -> ImVec2 {
42        ImVec2 { x: 0.0, y: 0.0 }
43    }
44}
45
46impl From<[f32; 2]> for ImVec2 {
47    #[inline]
48    fn from(array: [f32; 2]) -> ImVec2 {
49        ImVec2::new(array[0], array[1])
50    }
51}
52
53impl From<(f32, f32)> for ImVec2 {
54    #[inline]
55    fn from((x, y): (f32, f32)) -> ImVec2 {
56        ImVec2::new(x, y)
57    }
58}
59
60impl From<ImVec2> for [f32; 2] {
61    #[inline]
62    fn from(v: ImVec2) -> [f32; 2] {
63        [v.x, v.y]
64    }
65}
66
67impl From<ImVec2> for (f32, f32) {
68    #[inline]
69    fn from(v: ImVec2) -> (f32, f32) {
70        (v.x, v.y)
71    }
72}
73
74impl ImVec4 {
75    #[inline]
76    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 {
77        ImVec4 { x, y, z, w }
78    }
79    #[inline]
80    pub const fn zero() -> ImVec4 {
81        ImVec4 {
82            x: 0.0,
83            y: 0.0,
84            z: 0.0,
85            w: 0.0,
86        }
87    }
88}
89
90impl From<[f32; 4]> for ImVec4 {
91    #[inline]
92    fn from(array: [f32; 4]) -> ImVec4 {
93        ImVec4::new(array[0], array[1], array[2], array[3])
94    }
95}
96
97impl From<(f32, f32, f32, f32)> for ImVec4 {
98    #[inline]
99    fn from((x, y, z, w): (f32, f32, f32, f32)) -> ImVec4 {
100        ImVec4::new(x, y, z, w)
101    }
102}
103
104impl From<ImVec4> for [f32; 4] {
105    #[inline]
106    fn from(v: ImVec4) -> [f32; 4] {
107        [v.x, v.y, v.z, v.w]
108    }
109}
110
111impl From<ImVec4> for (f32, f32, f32, f32) {
112    #[inline]
113    fn from(v: ImVec4) -> (f32, f32, f32, f32) {
114        (v.x, v.y, v.z, v.w)
115    }
116}
117
118#[test]
119fn test_imvec2_memory_layout() {
120    use core::mem;
121    assert_eq!(mem::size_of::<ImVec2>(), mem::size_of::<[f32; 2]>());
122    assert_eq!(mem::align_of::<ImVec2>(), mem::align_of::<[f32; 2]>());
123    let test = ImVec2::new(1.0, 2.0);
124    let ref_a: &ImVec2 = &test;
125    let ref_b: &[f32; 2] = unsafe { &*(&test as *const _ as *const [f32; 2]) };
126    assert_eq!(&ref_a.x as *const _, &ref_b[0] as *const _);
127    assert_eq!(&ref_a.y as *const _, &ref_b[1] as *const _);
128}
129
130#[test]
131fn test_imvec4_memory_layout() {
132    use core::mem;
133    assert_eq!(mem::size_of::<ImVec4>(), mem::size_of::<[f32; 4]>());
134    assert_eq!(mem::align_of::<ImVec4>(), mem::align_of::<[f32; 4]>());
135    let test = ImVec4::new(1.0, 2.0, 3.0, 4.0);
136    let ref_a: &ImVec4 = &test;
137    let ref_b: &[f32; 4] = unsafe { &*(&test as *const _ as *const [f32; 4]) };
138    assert_eq!(&ref_a.x as *const _, &ref_b[0] as *const _);
139    assert_eq!(&ref_a.y as *const _, &ref_b[1] as *const _);
140    assert_eq!(&ref_a.z as *const _, &ref_b[2] as *const _);
141    assert_eq!(&ref_a.w as *const _, &ref_b[3] as *const _);
142}