dexalt-lib 0.2.2

Dexalt Library
Documentation
//! # Debug Clist
//! This are the Debug Bindings from this Modulee for the CList.
//! You should not this for any generally action, only for experts.
//! 
//! Note
//! ----
//! This Module includes the CList full from dexalt-lib-sys.
//! 
//! <details>
//!     <summary>Includes</summary>
//!     <ol>
//!         <li><pre><code>pub struct CList</code></pre></li>
//!         <li><pre><code>pub fn CList_init</code></pre></li>
//!         <li><pre><code>pub fn CList_free</code></pre></li>
//!         <li><pre><code>pub fn CList_append</code></pre></li>
//!         <li><pre><code>pub fn CList_extend</code></pre></li>
//!         <li><pre><code>pub fn CList_push</code></pre></li>
//!         <li><pre><code>pub fn CList_remove</code></pre></li>
//!         <li><pre><code>pub fn CList_pop</code></pre></li>
//!         <li><pre><code>pub fn CList_to_array</code></pre></li>
//!         <li><pre><code>pub fn CList_to_array_new</code></pre></li>
//!         <li><pre><code>pub fn CList_size</code></pre></li>
//!         <li><pre><code>pub fn CList_len</code></pre></li>
//!         <li><pre><code>pub fn CList_lock</code></pre></li>
//!         <li><pre><code>pub fn CList_unlock</code></pre></li>
//!         <li><pre><code>pub fn CList_is_locked</code></pre></li>
//!     </ol>
//! </details>
//! 
//! **Attention**
//! This Modulee is only for experts.
//! The Includes summary show you only the generally names
//! 
//! ## use
//! 
//! ```rust
//! use dexalt_lib::clist::raw::include::*;
//! ```
pub mod bindings;

pub mod include {
    pub use crate::clist::raw::bindings::*;
}

#[allow(unused_imports)]
use std::ffi;
#[allow(unused_imports)]
use std::os::raw;
#[allow(unused_imports)]
use std::ptr;

/// The Data enum.
/// 
/// # Example
/// 
/// ```rust
/// let data = Data::int(10);
/// ```
pub enum Data {
    Int(i32),
    UInt(u32),
    Float(f32),
    Double(f64),
    CString(ffi::CString),
}

/// The DebugCList struct.
/// 
/// # Example
/// 
/// ```rust
/// let mut list = DebugCList::new(10);
/// ```
pub struct DebugCList {
    pub inner: include::RawCList,
}

impl DebugCList {
    /// Create a new CList with the given size.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// ```
    pub fn new(size: usize) -> Self {
        let size_int = size.try_into().unwrap();
        let inner_n = include::RawCList::new(size_int);
        DebugCList { inner: inner_n }
    }

    /// Clear the CList.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.clear();
    /// ```
    pub fn clear(&mut self) {
        self.inner.free();
    }

    /// Get the CList with the given index.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let data = list.get(0);
    /// ```
    pub fn get(&mut self, index: usize) -> ffi::CString {
        let index_int: raw::c_int = index.try_into().unwrap();
        let data_ptr = self.inner.get(index_int);
        let data_c = unsafe { ffi::CStr::from_ptr(data_ptr as *const _)};
        let data_bytes = data_c.to_bytes();
        let data_cstring = ffi::CString::new(data_bytes).unwrap();
        data_cstring
    }

    /// Append the CList with the given data.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.append(ffi::CString::new("Hello").unwrap());
    /// ```
    pub fn append(&mut self, data: ffi::CString) {
        let data_ptr = data.as_ptr();
        self.inner.append(data_ptr as *mut raw::c_void);
    }

    /// Extend the CList with the given data.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.extend(&mut list);
    /// ```
    pub fn extend(&mut self, list: &mut include::RawCList) {
        let mut clist_origin = include::include::CList {
            data: list.get_data(),
            size: list.size(),
            len: list.len(),
            locked: list.is_locked(),
            next: list.get_next(),
        };
        self.inner.extend(&mut clist_origin);
    }

    /// Push the CList with the given data.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.push(Data::Int(10));
    /// ```
    pub fn push(&mut self, data: Data) {
        match data {
            Data::Int(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
            Data::UInt(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
            Data::Float(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
            Data::Double(val) => self.inner.push(&val as *const _ as *mut raw::c_void),
            Data::CString(val) => self.inner.push(val.as_ptr() as *mut raw::c_void),
        }
    }

    /// Remove the CList with the given data.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.remove(ffi::CString::new("Hello").unwrap());
    /// ```
    pub fn remove(&mut self, data: ffi::CString) {
        let data_ptr = data.as_ptr();
        self.inner.remove(data_ptr as *mut raw::c_void);
    }

    /// Pop the CList with the given index.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.pop(0);
    /// ```
    pub fn pop(&mut self, index: usize) {
        let index_int: raw::c_int = index.try_into().unwrap();
        self.inner.pop(index_int);
    }

    /// Convert the CList to an array.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let mut array = Vec::new();
    /// list.to_array(&mut array);
    /// ```
    pub fn to_array(&mut self, array: &mut Vec<ffi::CString>) {
        let mut raw_array: Vec<*mut raw::c_void> = array.iter_mut()
            .map(|cstr| cstr.as_ptr() as *mut raw::c_void)
            .collect();
        let array_ptr: *mut *mut raw::c_void = raw_array.as_mut_ptr();
        self.inner.to_array(array_ptr);
        // Update the original array with new values if needed
    }

    /// Convert the CList to an array.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let array = list.to_array_new();
    /// ```
    pub fn to_array_new(&mut self) -> Vec<ffi::CString> {
        let mut array: Vec<ffi::CString> = Vec::new();
        self.to_array(&mut array);
        array
    }


    /// Get the size of the CList.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let size = list.size();
    /// ```
    pub fn size(&mut self) -> usize {
        let size_int = self.inner.size();
        size_int.try_into().unwrap()
    }

    /// Get the length of the CList.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let len = list.len();
    /// ```
    pub fn len(&mut self) -> usize {
        let len_int = self.inner.len();
        len_int.try_into().unwrap()
    }

    /// Lock the CList.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.lock();
    /// ```
    pub fn lock(&mut self) {
        self.inner.lock();
    }

    /// Unlock the CList.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// list.unlock();
    /// ```
    pub fn unlock(&mut self) {
        self.inner.unlock();
    }

    /// Check if the CList is locked.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// let mut list = DebugCList::new(10);
    /// let is_locked = list.is_locked();
    /// ```
    pub fn is_locked(&mut self) -> bool {
        self.inner.is_locked()
    }
}