opendaq 0.1.1

Safe Rust bindings for openDAQ, the open-source data acquisition SDK
Documentation
// Generated by tools/generate_bindings.py -- do not edit by hand.

// Mechanical output: lint findings here are the generator's business.
#![allow(clippy::all, unused_imports, rustdoc::bare_urls)]

use crate::error::{check, Error, Result};
use crate::object::{BaseObject, Interface, Ref};
use crate::sys;
use crate::value::{Complex, Ratio, Value};
use crate::generated::*;
use std::ffi::c_char;

/// @ingroup opendaq_scheduler_components
/// @addtogroup opendaq_awaitable Awaitable
/// Wrapper over the openDAQ `daqAwaitable` interface.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct Awaitable(pub(crate) BaseObject);

impl std::ops::Deref for Awaitable {
    type Target = BaseObject;
    fn deref(&self) -> &BaseObject { &self.0 }
}
impl crate::sealed::Sealed for Awaitable {}
unsafe impl Interface for Awaitable {
    const NAME: &'static str = "daqAwaitable";
    fn interface_id() -> Option<crate::IntfID> {
        let mut id = crate::IntfID { Data1: 0, Data2: 0, Data3: 0, Data4: 0 };
        unsafe { (crate::sys::api().daqAwaitable_getInterfaceId)(&mut id) };
        Some(id)
    }
    unsafe fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        Ref::from_owned(ptr).map(Self::__from_ref)
    }
    fn as_base_object(&self) -> &BaseObject { &self.0 }
}
impl Awaitable {
    #[doc(hidden)]
    pub(crate) fn __from_ref(r: Ref) -> Self { Awaitable(BaseObject(r)) }
}
impl std::fmt::Display for Awaitable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_base_object(), f)
    }
}
impl From<&Awaitable> for Value {
    fn from(value: &Awaitable) -> Value { Value::Object(value.to_base_object()) }
}
impl From<Awaitable> for Value {
    fn from(value: Awaitable) -> Value { Value::Object(value.to_base_object()) }
}
impl crate::value::FromDaqOwned for Awaitable {
    unsafe fn from_daq_owned(ptr: *mut std::ffi::c_void, op: &'static str) -> Result<Self> {
        crate::value::cast_owned(ptr, op)
    }
}

/// Represents a way to get a string representation of a graph usually in some diagram description language like DOT, mermaid or D2.
/// Wrapper over the openDAQ `daqGraphVisualization` interface.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct GraphVisualization(pub(crate) BaseObject);

impl std::ops::Deref for GraphVisualization {
    type Target = BaseObject;
    fn deref(&self) -> &BaseObject { &self.0 }
}
impl crate::sealed::Sealed for GraphVisualization {}
unsafe impl Interface for GraphVisualization {
    const NAME: &'static str = "daqGraphVisualization";
    fn interface_id() -> Option<crate::IntfID> {
        let mut id = crate::IntfID { Data1: 0, Data2: 0, Data3: 0, Data4: 0 };
        unsafe { (crate::sys::api().daqGraphVisualization_getInterfaceId)(&mut id) };
        Some(id)
    }
    unsafe fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        Ref::from_owned(ptr).map(Self::__from_ref)
    }
    fn as_base_object(&self) -> &BaseObject { &self.0 }
}
impl GraphVisualization {
    #[doc(hidden)]
    pub(crate) fn __from_ref(r: Ref) -> Self { GraphVisualization(BaseObject(r)) }
}
impl std::fmt::Display for GraphVisualization {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_base_object(), f)
    }
}
impl From<&GraphVisualization> for Value {
    fn from(value: &GraphVisualization) -> Value { Value::Object(value.to_base_object()) }
}
impl From<GraphVisualization> for Value {
    fn from(value: GraphVisualization) -> Value { Value::Object(value.to_base_object()) }
}
impl crate::value::FromDaqOwned for GraphVisualization {
    unsafe fn from_daq_owned(ptr: *mut std::ffi::c_void, op: &'static str) -> Result<Self> {
        crate::value::cast_owned(ptr, op)
    }
}

/// A packaged callback with possible continuations and dependencies that can be arranged in a dependency graph (directed acyclic graph). The task is not executed directly but only when the graph is scheduled for execution and all dependencies have been satisfied.
/// Wrapper over the openDAQ `daqTask` interface.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct Task(pub(crate) BaseObject);

impl std::ops::Deref for Task {
    type Target = BaseObject;
    fn deref(&self) -> &BaseObject { &self.0 }
}
impl crate::sealed::Sealed for Task {}
unsafe impl Interface for Task {
    const NAME: &'static str = "daqTask";
    fn interface_id() -> Option<crate::IntfID> {
        let mut id = crate::IntfID { Data1: 0, Data2: 0, Data3: 0, Data4: 0 };
        unsafe { (crate::sys::api().daqTask_getInterfaceId)(&mut id) };
        Some(id)
    }
    unsafe fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        Ref::from_owned(ptr).map(Self::__from_ref)
    }
    fn as_base_object(&self) -> &BaseObject { &self.0 }
}
impl Task {
    #[doc(hidden)]
    pub(crate) fn __from_ref(r: Ref) -> Self { Task(BaseObject(r)) }
}
impl std::fmt::Display for Task {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_base_object(), f)
    }
}
impl From<&Task> for Value {
    fn from(value: &Task) -> Value { Value::Object(value.to_base_object()) }
}
impl From<Task> for Value {
    fn from(value: Task) -> Value { Value::Object(value.to_base_object()) }
}
impl crate::value::FromDaqOwned for Task {
    unsafe fn from_daq_owned(ptr: *mut std::ffi::c_void, op: &'static str) -> Result<Self> {
        crate::value::cast_owned(ptr, op)
    }
}

/// A dependency graph (directed acyclic graph) of tasks that can be scheduled for execution on a Scheduler.
/// Wrapper over the openDAQ `daqTaskGraph` interface.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct TaskGraph(pub(crate) Task);

impl std::ops::Deref for TaskGraph {
    type Target = Task;
    fn deref(&self) -> &Task { &self.0 }
}
impl crate::sealed::Sealed for TaskGraph {}
unsafe impl Interface for TaskGraph {
    const NAME: &'static str = "daqTaskGraph";
    fn interface_id() -> Option<crate::IntfID> {
        let mut id = crate::IntfID { Data1: 0, Data2: 0, Data3: 0, Data4: 0 };
        unsafe { (crate::sys::api().daqTaskGraph_getInterfaceId)(&mut id) };
        Some(id)
    }
    unsafe fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        Ref::from_owned(ptr).map(Self::__from_ref)
    }
    fn as_base_object(&self) -> &BaseObject { &self.0 }
}
impl TaskGraph {
    #[doc(hidden)]
    pub(crate) fn __from_ref(r: Ref) -> Self { TaskGraph(Task::__from_ref(r)) }
}
impl std::fmt::Display for TaskGraph {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_base_object(), f)
    }
}
impl From<&TaskGraph> for Value {
    fn from(value: &TaskGraph) -> Value { Value::Object(value.to_base_object()) }
}
impl From<TaskGraph> for Value {
    fn from(value: TaskGraph) -> Value { Value::Object(value.to_base_object()) }
}
impl crate::value::FromDaqOwned for TaskGraph {
    unsafe fn from_daq_owned(ptr: *mut std::ffi::c_void, op: &'static str) -> Result<Self> {
        crate::value::cast_owned(ptr, op)
    }
}

/// A lightweight implementation of callback used in scheduler for worker tasks.
/// Wrapper over the openDAQ `daqWork` interface.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct Work(pub(crate) BaseObject);

impl std::ops::Deref for Work {
    type Target = BaseObject;
    fn deref(&self) -> &BaseObject { &self.0 }
}
impl crate::sealed::Sealed for Work {}
unsafe impl Interface for Work {
    const NAME: &'static str = "daqWork";
    fn interface_id() -> Option<crate::IntfID> {
        let mut id = crate::IntfID { Data1: 0, Data2: 0, Data3: 0, Data4: 0 };
        unsafe { (crate::sys::api().daqWork_getInterfaceId)(&mut id) };
        Some(id)
    }
    unsafe fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        Ref::from_owned(ptr).map(Self::__from_ref)
    }
    fn as_base_object(&self) -> &BaseObject { &self.0 }
}
impl Work {
    #[doc(hidden)]
    pub(crate) fn __from_ref(r: Ref) -> Self { Work(BaseObject(r)) }
}
impl std::fmt::Display for Work {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_base_object(), f)
    }
}
impl From<&Work> for Value {
    fn from(value: &Work) -> Value { Value::Object(value.to_base_object()) }
}
impl From<Work> for Value {
    fn from(value: Work) -> Value { Value::Object(value.to_base_object()) }
}
impl crate::value::FromDaqOwned for Work {
    unsafe fn from_daq_owned(ptr: *mut std::ffi::c_void, op: &'static str) -> Result<Self> {
        crate::value::cast_owned(ptr, op)
    }
}

impl Awaitable {
    /// Cancels the outstanding work if it has not already started.
    ///
    /// # Returns
    /// - `canceled`: Is `true` if the execution was canceled or `false` if the execution has already completed
    ///
    /// Calls the openDAQ C function `daqAwaitable_cancel()`.
    pub fn cancel(&self) -> Result<bool> {
        let mut __canceled: u8 = 0;
        let __code = unsafe { (crate::sys::api().daqAwaitable_cancel)(self.as_raw() as *mut _, &mut __canceled) };
        check(__code, "daqAwaitable_cancel")?;
        Ok(__canceled != 0)
    }

    /// Waits until the awaitable has a valid result and retrieves it or re-throws the exception that occurred during the execution.
    ///
    /// # Returns
    /// - `result`: The execution result if any otherwise `nullptr.`
    ///
    /// # Errors
    /// - `OPENDAQ_ERR_EMPTY_AWAITABLE`: when there is no work associated with the awaitable.
    ///
    /// Calls the openDAQ C function `daqAwaitable_getResult()`.
    pub fn result(&self) -> Result<Value> {
        let mut __result: *mut sys::daqBaseObject = std::ptr::null_mut();
        let __code = unsafe { (crate::sys::api().daqAwaitable_getResult)(self.as_raw() as *mut _, &mut __result) };
        check(__code, "daqAwaitable_getResult")?;
        Ok(unsafe { crate::value::take_value(__result, "daqAwaitable_getResult") }?)
    }

    /// Checks if the execution has already finished.
    ///
    /// # Returns
    /// - `completed`: Is `true` if the execution has finished or `false` if the execution is in progress or there is no work associated
    ///
    /// Calls the openDAQ C function `daqAwaitable_hasCompleted()`.
    pub fn has_completed(&self) -> Result<bool> {
        let mut __completed: u8 = 0;
        let __code = unsafe { (crate::sys::api().daqAwaitable_hasCompleted)(self.as_raw() as *mut _, &mut __completed) };
        check(__code, "daqAwaitable_hasCompleted")?;
        Ok(__completed != 0)
    }

    /// Calls the openDAQ C function `daqAwaitable_wait()`.
    pub fn wait(&self) -> Result<()> {
        let __code = unsafe { (crate::sys::api().daqAwaitable_wait)(self.as_raw() as *mut _) };
        check(__code, "daqAwaitable_wait")?;
        Ok(())
    }

}

impl GraphVisualization {
    /// Returns the graph representation as a string.
    ///
    /// # Returns
    /// - `dot`: Graph's string representation
    ///
    /// Calls the openDAQ C function `daqGraphVisualization_dump()`.
    pub fn dump(&self) -> Result<String> {
        let mut __dot: *mut sys::daqString = std::ptr::null_mut();
        let __code = unsafe { (crate::sys::api().daqGraphVisualization_dump)(self.as_raw() as *mut _, &mut __dot) };
        check(__code, "daqGraphVisualization_dump")?;
        Ok(unsafe { crate::marshal::take_string(__dot) })
    }

}

impl TaskGraph {
    /// Calls the openDAQ C function `daqTaskGraph_createTaskGraph()`.
    pub fn new(work: &Procedure, name: &str) -> Result<TaskGraph> {
        let __name = crate::marshal::make_string(name)?;
        let mut __obj: *mut sys::daqTaskGraph = std::ptr::null_mut();
        let __code = unsafe { (crate::sys::api().daqTaskGraph_createTaskGraph)(&mut __obj, work.as_raw() as *mut _, __name.as_ptr() as *mut _) };
        check(__code, "daqTaskGraph_createTaskGraph")?;
        Ok(unsafe { crate::marshal::require_object::<TaskGraph>(__obj as *mut _, "daqTaskGraph_createTaskGraph") }?)
    }

}

impl Task {
    /// Calls the openDAQ C function `daqTask_createTask()`.
    pub fn new(work: &Procedure, name: &str) -> Result<Task> {
        let __name = crate::marshal::make_string(name)?;
        let mut __obj: *mut sys::daqTask = std::ptr::null_mut();
        let __code = unsafe { (crate::sys::api().daqTask_createTask)(&mut __obj, work.as_raw() as *mut _, __name.as_ptr() as *mut _) };
        check(__code, "daqTask_createTask")?;
        Ok(unsafe { crate::marshal::require_object::<Task>(__obj as *mut _, "daqTask_createTask") }?)
    }

    /// Gets the task name.
    ///
    /// # Returns
    /// - `name`: The task name.
    ///
    /// Calls the openDAQ C function `daqTask_getName()`.
    pub fn name(&self) -> Result<String> {
        let mut __name: *mut sys::daqString = std::ptr::null_mut();
        let __code = unsafe { (crate::sys::api().daqTask_getName)(self.as_raw() as *mut _, &mut __name) };
        check(__code, "daqTask_getName")?;
        Ok(unsafe { crate::marshal::take_string(__name) })
    }

    /// Sets the task name that is used in diagnostics.
    ///
    /// # Parameters
    /// - `name`: The new task name.
    ///
    /// Calls the openDAQ C function `daqTask_setName()`.
    pub fn set_name(&self, name: &str) -> Result<()> {
        let __name = crate::marshal::make_string(name)?;
        let __code = unsafe { (crate::sys::api().daqTask_setName)(self.as_raw() as *mut _, __name.as_ptr() as *mut _) };
        check(__code, "daqTask_setName")?;
        Ok(())
    }

    /// Sets the continuation to only execute after this task completes. Be careful of forming cycles as tasks whose dependencies cannot be satisfied will never execute.
    ///
    /// # Parameters
    /// - `continuation`: The task that should wait for this one to complete.
    ///
    /// # Errors
    /// - `OPENDAQ_ERR_NOT_SUPPORTED`: If the `continuation` implementation was not crated by the scheduler library.
    ///
    /// Calls the openDAQ C function `daqTask_then()`.
    pub fn then(&self, continuation: &Task) -> Result<()> {
        let __code = unsafe { (crate::sys::api().daqTask_then)(self.as_raw() as *mut _, continuation.as_raw() as *mut _) };
        check(__code, "daqTask_then")?;
        Ok(())
    }

}

impl Work {
    /// Executes the callback.
    ///
    /// Calls the openDAQ C function `daqWork_execute()`.
    pub fn execute(&self) -> Result<()> {
        let __code = unsafe { (crate::sys::api().daqWork_execute)(self.as_raw() as *mut _) };
        check(__code, "daqWork_execute")?;
        Ok(())
    }

}