xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/rusticl/util/properties.rs (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 pub struct Properties<T> {
2     pub props: Vec<(T, T)>,
3 }
4 
5 impl<T: Copy + PartialEq + Default> Properties<T> {
6     #[allow(clippy::not_unsafe_ptr_arg_deref)]
from_ptr_raw(mut p: *const T) -> Vec<T>7     pub fn from_ptr_raw(mut p: *const T) -> Vec<T> {
8         let mut res: Vec<T> = Vec::new();
9 
10         if !p.is_null() {
11             unsafe {
12                 while *p != T::default() {
13                     res.push(*p);
14                     res.push(*p.add(1));
15                     p = p.add(2);
16                 }
17             }
18             res.push(T::default());
19         }
20 
21         res
22     }
23 
24     #[allow(clippy::not_unsafe_ptr_arg_deref)]
from_ptr(mut p: *const T) -> Option<Self>25     pub fn from_ptr(mut p: *const T) -> Option<Self> {
26         let mut res = Self::default();
27 
28         if !p.is_null() {
29             let mut k: Vec<T> = Vec::new();
30             let mut v: Vec<T> = Vec::new();
31 
32             unsafe {
33                 while *p != T::default() {
34                     if k.contains(&*p) {
35                         return None;
36                     }
37                     k.push(*p);
38                     v.push(*p.add(1));
39                     p = p.add(2);
40                 }
41             }
42 
43             res.props = k.iter().cloned().zip(v).collect();
44         }
45 
46         Some(res)
47     }
48 }
49 
50 impl<T> Default for Properties<T> {
default() -> Self51     fn default() -> Self {
52         Self { props: Vec::new() }
53     }
54 }
55