mdflib 0.2.1

Rust bindings for mdflib
Documentation
//! ChannelArray wrapper for mdflib IChannelArray
//!
//! A channel array is a special type of channel that represents an array of values.
//! This is used for signals that are arrays, such as a spectrum or a map.

use mdflib_sys as ffi;
use std::marker::PhantomData;
use std::ops::Deref;

/// Represents an immutable reference to a channel array in an MDF file.
#[derive(Debug, Clone, Copy)]
pub struct ChannelArrayRef<'a> {
    pub(crate) inner: *const ffi::IChannelArray,
    _marker: PhantomData<&'a ()>,
}

impl std::fmt::Display for ChannelArrayRef<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ChannelArray {{ index: {}, type: {}, storage: {}, flags: {}, nof_elements: {} }}",
            self.get_index(),
            self.get_type(),
            self.get_storage(),
            self.get_flags(),
            self.get_nof_elements()
        )
    }
}

impl<'a> ChannelArrayRef<'a> {
    #[allow(dead_code)]
    pub(crate) fn new(inner: *const ffi::IChannelArray) -> Self {
        Self {
            inner,
            _marker: PhantomData,
        }
    }

    /// Gets the index of the channel array.
    pub fn get_index(&self) -> u64 {
        unsafe { ffi::ChannelArrayGetIndex(self.inner) }
    }

    /// Gets the type of the channel array.
    pub fn get_type(&self) -> u8 {
        unsafe { ffi::ChannelArrayGetType(self.inner) }
    }

    /// Gets the storage type of the channel array.
    pub fn get_storage(&self) -> u8 {
        unsafe { ffi::ChannelArrayGetStorage(self.inner) }
    }

    /// Gets the flags of the channel array.
    pub fn get_flags(&self) -> u32 {
        unsafe { ffi::ChannelArrayGetFlags(self.inner) }
    }

    /// Gets the number of elements in the channel array.
    pub fn get_nof_elements(&self) -> u64 {
        unsafe { ffi::ChannelArrayGetNofElements(self.inner) }
    }
}

/// Represents a mutable channel array in an MDF file.
#[derive(Debug)]
pub struct ChannelArray<'a> {
    pub(crate) inner: *mut ffi::IChannelArray,
    inner_ref: ChannelArrayRef<'a>,
}

impl<'a> ChannelArray<'a> {
    #[allow(dead_code)]
    pub(crate) fn new(inner: *mut ffi::IChannelArray) -> Self {
        Self {
            inner,
            inner_ref: ChannelArrayRef::new(inner),
        }
    }

    /// Sets the type of the channel array.
    pub fn set_type(&mut self, array_type: u8) {
        unsafe {
            ffi::ChannelArraySetType(self.inner, array_type);
        }
    }

    /// Sets the storage type of the channel array.
    pub fn set_storage(&mut self, storage: u8) {
        unsafe {
            ffi::ChannelArraySetStorage(self.inner, storage);
        }
    }

    /// Sets the flags of the channel array.
    pub fn set_flags(&mut self, flags: u32) {
        unsafe {
            ffi::ChannelArraySetFlags(self.inner, flags);
        }
    }

    // TODO
    // /// Sets the number of elements in the channel array.
    // pub fn set_nof_elements(&mut self, elements: u64) {
    //     unsafe {
    //         ffi::ChannelArraySetNofElements(self.inner, elements);
    //     }
    // }
}

impl<'a> Deref for ChannelArray<'a> {
    type Target = ChannelArrayRef<'a>;

    fn deref(&self) -> &Self::Target {
        &self.inner_ref
    }
}