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
23cfg_if::cfg_if! {
24    if #[cfg(feature = "wasm")] {
25        cfg_if::cfg_if! {
26            if #[cfg(feature = "docking")] {
27                cfg_if::cfg_if! {
28                    if #[cfg(feature = "freetype")] {
29                        mod wasm_docking_freetype_bindings;
30                        pub use crate::wasm_docking_freetype_bindings::*;
31                    } else {
32                        mod wasm_docking_bindings;
33                        pub use crate::wasm_docking_bindings::*;
34                    }
35                }
36            } else {
37                cfg_if::cfg_if! {
38                    if #[cfg(feature = "freetype")] {
39                        mod wasm_freetype_bindings;
40                        pub use crate::wasm_freetype_bindings::*;
41                    } else {
42                        mod wasm_bindings;
43                        pub use crate::wasm_bindings::*;
44                    }
45                }
46            }
47        }
48    } else {
49        cfg_if::cfg_if! {
50            if #[cfg(feature = "docking")] {
51                cfg_if::cfg_if! {
52                    if #[cfg(feature = "freetype")] {
53                        mod docking_freetype_bindings;
54                        pub use crate::docking_freetype_bindings::*;
55                    } else {
56                        mod docking_bindings;
57                        pub use crate::docking_bindings::*;
58                    }
59                }
60            } else {
61                cfg_if::cfg_if! {
62                    if #[cfg(feature = "freetype")] {
63                        mod freetype_bindings;
64                        pub use crate::freetype_bindings::*;
65                    } else {
66                        mod bindings;
67                        pub use crate::bindings::*;
68                    }
69                }
70            }
71        }
72    }
73}
74
75impl ImVec2 {
76    #[inline]
77    pub const fn new(x: f32, y: f32) -> ImVec2 {
78        ImVec2 { x, y }
79    }
80    #[inline]
81    pub const fn zero() -> ImVec2 {
82        ImVec2 { x: 0.0, y: 0.0 }
83    }
84}
85
86impl From<[f32; 2]> for ImVec2 {
87    #[inline]
88    fn from(array: [f32; 2]) -> ImVec2 {
89        ImVec2::new(array[0], array[1])
90    }
91}
92
93impl From<(f32, f32)> for ImVec2 {
94    #[inline]
95    fn from((x, y): (f32, f32)) -> ImVec2 {
96        ImVec2::new(x, y)
97    }
98}
99
100impl From<ImVec2> for [f32; 2] {
101    #[inline]
102    fn from(v: ImVec2) -> [f32; 2] {
103        [v.x, v.y]
104    }
105}
106
107impl From<ImVec2> for (f32, f32) {
108    #[inline]
109    fn from(v: ImVec2) -> (f32, f32) {
110        (v.x, v.y)
111    }
112}
113
114impl From<mint::Vector2<f32>> for ImVec2 {
115    fn from(o: mint::Vector2<f32>) -> Self {
116        Self { x: o.x, y: o.y }
117    }
118}
119
120impl From<mint::Vector4<f32>> for ImVec4 {
121    fn from(o: mint::Vector4<f32>) -> Self {
122        Self {
123            x: o.x,
124            y: o.y,
125            z: o.z,
126            w: o.w,
127        }
128    }
129}
130
131impl ImVec4 {
132    #[inline]
133    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 {
134        ImVec4 { x, y, z, w }
135    }
136    #[inline]
137    pub const fn zero() -> ImVec4 {
138        ImVec4 {
139            x: 0.0,
140            y: 0.0,
141            z: 0.0,
142            w: 0.0,
143        }
144    }
145}
146
147impl From<[f32; 4]> for ImVec4 {
148    #[inline]
149    fn from(array: [f32; 4]) -> ImVec4 {
150        ImVec4::new(array[0], array[1], array[2], array[3])
151    }
152}
153
154impl From<(f32, f32, f32, f32)> for ImVec4 {
155    #[inline]
156    fn from((x, y, z, w): (f32, f32, f32, f32)) -> ImVec4 {
157        ImVec4::new(x, y, z, w)
158    }
159}
160
161impl From<ImVec4> for [f32; 4] {
162    #[inline]
163    fn from(v: ImVec4) -> [f32; 4] {
164        [v.x, v.y, v.z, v.w]
165    }
166}
167
168impl From<ImVec4> for (f32, f32, f32, f32) {
169    #[inline]
170    fn from(v: ImVec4) -> (f32, f32, f32, f32) {
171        (v.x, v.y, v.z, v.w)
172    }
173}
174
175#[test]
176fn test_imvec2_memory_layout() {
177    use core::mem;
178    assert_eq!(mem::size_of::<ImVec2>(), mem::size_of::<[f32; 2]>());
179    assert_eq!(mem::align_of::<ImVec2>(), mem::align_of::<[f32; 2]>());
180    let test = ImVec2::new(1.0, 2.0);
181    let ref_a: &ImVec2 = &test;
182    let ref_b: &[f32; 2] = unsafe { &*(&test as *const _ as *const [f32; 2]) };
183    assert_eq!(&ref_a.x as *const _, &ref_b[0] as *const _);
184    assert_eq!(&ref_a.y as *const _, &ref_b[1] as *const _);
185}
186
187#[test]
188fn test_imvec4_memory_layout() {
189    use core::mem;
190    assert_eq!(mem::size_of::<ImVec4>(), mem::size_of::<[f32; 4]>());
191    assert_eq!(mem::align_of::<ImVec4>(), mem::align_of::<[f32; 4]>());
192    let test = ImVec4::new(1.0, 2.0, 3.0, 4.0);
193    let ref_a: &ImVec4 = &test;
194    let ref_b: &[f32; 4] = unsafe { &*(&test as *const _ as *const [f32; 4]) };
195    assert_eq!(&ref_a.x as *const _, &ref_b[0] as *const _);
196    assert_eq!(&ref_a.y as *const _, &ref_b[1] as *const _);
197    assert_eq!(&ref_a.z as *const _, &ref_b[2] as *const _);
198    assert_eq!(&ref_a.w as *const _, &ref_b[3] as *const _);
199}