1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(clippy::all)]

use std::ops::{Deref, Index};

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

impl<T> Index<usize> for ImVector<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        if index >= self.Size as usize {
            panic!("ImVector out of bounds");
        }
        unsafe { &*self.Data.add(index) }
    }
}

impl<T> Deref for ImVector<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        unsafe {
            if self.Size == 0 {
                // self.Data may be null, and that will not do for `from_raw_parts`
                &[]
            } else {
                std::slice::from_raw_parts(self.Data, self.Size as usize)
            }
        }
    }
}

impl<'a, T> IntoIterator for &'a ImVector<T> {
    type Item = &'a T;
    type IntoIter = std::slice::Iter<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        self.deref().into_iter()
    }
}

#[cfg(target_env = "msvc")]
impl From<ImVec2_rr> for ImVec2 {
    fn from(rr: ImVec2_rr) -> ImVec2 {
        ImVec2 { x: rr.x, y: rr.y }
    }
}