Skip to main content

il2cpp_bridge_rs/structs/collections/
dictionary.rs

1//! IL2CPP Dictionary definition and operations
2use super::array::Il2cppArray;
3use std::ffi::c_void;
4use std::marker::PhantomData;
5
6#[repr(C)]
7pub struct Il2cppDictionary<K: Copy, V: Copy> {
8    /// Pointer to the dictionary class
9    pub klass: *mut c_void,
10    /// Monitor for synchronization
11    pub monitor: *mut c_void,
12    /// Array of bucket indices
13    pub buckets: *mut Il2cppArray<i32>,
14    /// Array of entries
15    pub entries: *mut Il2cppArray<c_void>,
16    /// Array of keys
17    pub keys: *mut Il2cppArray<K>,
18    /// Array of values
19    pub values: *mut Il2cppArray<V>,
20    /// Number of touched slots
21    pub touched_slots: i32,
22    /// Index of the first empty slot
23    pub empty_slot: i32,
24    /// Number of elements in the dictionary
25    pub count: i32,
26    _phantom_k: PhantomData<K>,
27    _phantom_v: PhantomData<V>,
28}
29
30impl<K: Copy, V: Copy> Il2cppDictionary<K, V> {
31    /// Gets the keys array
32    ///
33    /// # Returns
34    /// * `Option<&Il2cppArray<K>>` - The array of keys, or None if null
35    pub fn keys_array(&self) -> Option<&Il2cppArray<K>> {
36        if self.keys.is_null() {
37            None
38        } else {
39            unsafe { Some(&*self.keys) }
40        }
41    }
42
43    /// Gets the values array
44    ///
45    /// # Returns
46    /// * `Option<&Il2cppArray<V>>` - The array of values, or None if null
47    pub fn values_array(&self) -> Option<&Il2cppArray<V>> {
48        if self.values.is_null() {
49            None
50        } else {
51            unsafe { Some(&*self.values) }
52        }
53    }
54
55    /// Gets keys as a Rust Vec
56    ///
57    /// # Returns
58    /// * `Vec<K>` - A vector containing all keys
59    pub fn get_keys(&self) -> Vec<K> {
60        self.keys_array()
61            .map(|arr| arr.to_vector())
62            .unwrap_or_default()
63    }
64
65    /// Gets values as a Rust Vec
66    ///
67    /// # Returns
68    /// * `Vec<V>` - A vector containing all values
69    pub fn get_values(&self) -> Vec<V> {
70        self.values_array()
71            .map(|arr| arr.to_vector())
72            .unwrap_or_default()
73    }
74
75    /// Gets a pointer to the keys array data
76    ///
77    /// # Returns
78    /// * `Option<*const K>` - Pointer to the first key, or None
79    pub fn keys_pointer(&self) -> Option<*const K> {
80        self.keys_array().map(|arr| arr.get_pointer())
81    }
82
83    /// Gets a pointer to the values array data
84    ///
85    /// # Returns
86    /// * `Option<*const V>` - Pointer to the first value, or None
87    pub fn values_pointer(&self) -> Option<*const V> {
88        self.values_array().map(|arr| arr.get_pointer())
89    }
90}