summaryrefslogtreecommitdiff
path: root/src/gallium/frontends/rusticl/util/properties.rs
blob: aad44e5098adaecc9dccdf75cb3376629c67ae27 (plain)
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
50
51
52
53
54
pub struct Properties<T> {
    pub props: Vec<(T, T)>,
}

impl<T: Copy + PartialEq + Default> Properties<T> {
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn from_ptr_raw(mut p: *const T) -> Vec<T> {
        let mut res: Vec<T> = Vec::new();

        if !p.is_null() {
            unsafe {
                while *p != T::default() {
                    res.push(*p);
                    res.push(*p.add(1));
                    p = p.add(2);
                }
            }
            res.push(T::default());
        }

        res
    }

    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn from_ptr(mut p: *const T) -> Option<Self> {
        let mut res = Self::default();

        if !p.is_null() {
            let mut k: Vec<T> = Vec::new();
            let mut v: Vec<T> = Vec::new();

            unsafe {
                while *p != T::default() {
                    if k.contains(&*p) {
                        return None;
                    }
                    k.push(*p);
                    v.push(*p.add(1));
                    p = p.add(2);
                }
            }

            res.props = k.iter().cloned().zip(v).collect();
        }

        Some(res)
    }
}

impl<T> Default for Properties<T> {
    fn default() -> Self {
        Self { props: Vec::new() }
    }
}