dexalt_lib/clist/raw.rs
1//! # Debug Clist
2//! This are the Debug Bindings from this Modulee for the CList.
3//! You should not this for any generally action, only for experts.
4//!
5//! Note
6//! ----
7//! This Module includes the CList full from dexalt-lib-sys.
8//!
9//! <details>
10//! <summary>Includes</summary>
11//! <ol>
12//! <li><pre><code>pub struct CList</code></pre></li>
13//! <li><pre><code>pub fn CList_init</code></pre></li>
14//! <li><pre><code>pub fn CList_free</code></pre></li>
15//! <li><pre><code>pub fn CList_append</code></pre></li>
16//! <li><pre><code>pub fn CList_extend</code></pre></li>
17//! <li><pre><code>pub fn CList_push</code></pre></li>
18//! <li><pre><code>pub fn CList_remove</code></pre></li>
19//! <li><pre><code>pub fn CList_pop</code></pre></li>
20//! <li><pre><code>pub fn CList_to_array</code></pre></li>
21//! <li><pre><code>pub fn CList_to_array_new</code></pre></li>
22//! <li><pre><code>pub fn CList_size</code></pre></li>
23//! <li><pre><code>pub fn CList_len</code></pre></li>
24//! <li><pre><code>pub fn CList_lock</code></pre></li>
25//! <li><pre><code>pub fn CList_unlock</code></pre></li>
26//! <li><pre><code>pub fn CList_is_locked</code></pre></li>
27//! </ol>
28//! </details>
29//!
30//! **Attention**
31//! This Modulee is only for experts.
32//! The Includes summary show you only the generally names
33//!
34//! ## use
35//!
36//! ```rust
37//! use dexalt_lib::clist::raw::include::*;
38//! ```
39pub mod bindings;
40
41pub mod include {
42 pub use crate::clist::raw::bindings::*;
43}
44
45#[allow(unused_imports)]
46use std::ffi;
47#[allow(unused_imports)]
48use std::os::raw;
49#[allow(unused_imports)]
50use std::ptr;
51
52/// The Data enum.
53///
54/// # Example
55///
56/// ```rust
57/// let data = Data::int(10);
58/// ```
59pub enum Data {
60 Int(i32),
61 UInt(u32),
62 Float(f32),
63 Double(f64),
64 CString(ffi::CString),
65}
66
67/// The DebugCList struct.
68///
69/// # Example
70///
71/// ```rust
72/// let mut list = DebugCList::new(10);
73/// ```
74pub struct DebugCList {
75 pub inner: include::RawCList,
76}
77
78impl DebugCList {
79 /// Create a new CList with the given size.
80 ///
81 /// # Example
82 ///
83 /// ```rust
84 /// let mut list = DebugCList::new(10);
85 /// ```
86 pub fn new(size: usize) -> Self {
87 let size_int = size.try_into().unwrap();
88 let inner_n = include::RawCList::new(size_int);
89 DebugCList { inner: inner_n }
90 }
91
92 /// Clear the CList.
93 ///
94 /// # Example
95 ///
96 /// ```rust
97 /// let mut list = DebugCList::new(10);
98 /// list.clear();
99 /// ```
100 pub fn clear(&mut self) {
101 self.inner.free();
102 }
103
104 /// Get the CList with the given index.
105 ///
106 /// # Example
107 ///
108 /// ```rust
109 /// let mut list = DebugCList::new(10);
110 /// let data = list.get(0);
111 /// ```
112 pub fn get(&mut self, index: usize) -> ffi::CString {
113 let index_int: raw::c_int = index.try_into().unwrap();
114 let data_ptr = self.inner.get(index_int);
115 let data_c = unsafe { ffi::CStr::from_ptr(data_ptr as *const _)};
116 let data_bytes = data_c.to_bytes();
117 let data_cstring = ffi::CString::new(data_bytes).unwrap();
118 data_cstring
119 }
120
121 /// Append the CList with the given data.
122 ///
123 /// # Example
124 ///
125 /// ```rust
126 /// let mut list = DebugCList::new(10);
127 /// list.append(ffi::CString::new("Hello").unwrap());
128 /// ```
129 pub fn append(&mut self, data: ffi::CString) {
130 let data_ptr = data.as_ptr();
131 self.inner.append(data_ptr as *mut raw::c_void);
132 }
133
134 /// Extend the CList with the given data.
135 ///
136 /// # Example
137 ///
138 /// ```rust
139 /// let mut list = DebugCList::new(10);
140 /// list.extend(&mut list);
141 /// ```
142 pub fn extend(&mut self, list: &mut include::RawCList) {
143 let mut clist_origin = include::include::CList {
144 data: list.get_data(),
145 size: list.size(),
146 len: list.len(),
147 locked: list.is_locked(),
148 next: list.get_next(),
149 };
150 self.inner.extend(&mut clist_origin);
151 }
152
153 /// Push the CList with the given data.
154 ///
155 /// # Example
156 ///
157 /// ```rust
158 /// let mut list = DebugCList::new(10);
159 /// list.push(Data::Int(10));
160 /// ```
161 pub fn push(&mut self, data: Data) {
162 match data {
163 Data::Int(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
164 Data::UInt(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
165 Data::Float(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
166 Data::Double(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
167 Data::CString(val) => self.inner.push(val.as_ptr() as *mut raw::c_void),
168 }
169 }
170
171 /// Remove the CList with the given data.
172 ///
173 /// # Example
174 ///
175 /// ```rust
176 /// let mut list = DebugCList::new(10);
177 /// list.remove(ffi::CString::new("Hello").unwrap());
178 /// ```
179 pub fn remove(&mut self, data: ffi::CString) {
180 let data_ptr = data.as_ptr();
181 self.inner.remove(data_ptr as *mut raw::c_void);
182 }
183
184 /// Pop the CList with the given index.
185 ///
186 /// # Example
187 ///
188 /// ```rust
189 /// let mut list = DebugCList::new(10);
190 /// list.pop(0);
191 /// ```
192 pub fn pop(&mut self, index: usize) {
193 let index_int: raw::c_int = index.try_into().unwrap();
194 self.inner.pop(index_int);
195 }
196
197 /// Convert the CList to an array.
198 ///
199 /// # Example
200 ///
201 /// ```rust
202 /// let mut list = DebugCList::new(10);
203 /// let mut array = Vec::new();
204 /// list.to_array(&mut array);
205 /// ```
206 pub fn to_array(&mut self, array: &mut Vec<ffi::CString>) {
207 let mut raw_array: Vec<*mut raw::c_void> = array.iter_mut()
208 .map(|cstr| cstr.as_ptr() as *mut raw::c_void)
209 .collect();
210 let array_ptr: *mut *mut raw::c_void = raw_array.as_mut_ptr();
211 self.inner.to_array(array_ptr);
212 // Update the original array with new values if needed
213 }
214
215 /// Convert the CList to an array.
216 ///
217 /// # Example
218 ///
219 /// ```rust
220 /// let mut list = DebugCList::new(10);
221 /// let array = list.to_array_new();
222 /// ```
223 pub fn to_array_new(&mut self) -> Vec<ffi::CString> {
224 let mut array: Vec<ffi::CString> = Vec::new();
225 self.to_array(&mut array);
226 array
227 }
228
229
230 /// Get the size of the CList.
231 ///
232 /// # Example
233 ///
234 /// ```rust
235 /// let mut list = DebugCList::new(10);
236 /// let size = list.size();
237 /// ```
238 pub fn size(&mut self) -> usize {
239 let size_int = self.inner.size();
240 size_int.try_into().unwrap()
241 }
242
243 /// Get the length of the CList.
244 ///
245 /// # Example
246 ///
247 /// ```rust
248 /// let mut list = DebugCList::new(10);
249 /// let len = list.len();
250 /// ```
251 pub fn len(&mut self) -> usize {
252 let len_int = self.inner.len();
253 len_int.try_into().unwrap()
254 }
255
256 /// Lock the CList.
257 ///
258 /// # Example
259 ///
260 /// ```rust
261 /// let mut list = DebugCList::new(10);
262 /// list.lock();
263 /// ```
264 pub fn lock(&mut self) {
265 self.inner.lock();
266 }
267
268 /// Unlock the CList.
269 ///
270 /// # Example
271 ///
272 /// ```rust
273 /// let mut list = DebugCList::new(10);
274 /// list.unlock();
275 /// ```
276 pub fn unlock(&mut self) {
277 self.inner.unlock();
278 }
279
280 /// Check if the CList is locked.
281 ///
282 /// # Example
283 ///
284 /// ```rust
285 /// let mut list = DebugCList::new(10);
286 /// let is_locked = list.is_locked();
287 /// ```
288 pub fn is_locked(&mut self) -> bool {
289 self.inner.is_locked()
290 }
291}