ark-api 0.17.0-pre.15

Ark API
Documentation
#![allow(clippy::unused_self)] // it's an example!

use crate::ffi::example_manual as ffi;
use crate::Error;
use crate::ErrorCode;

/// APIs often have some module side state associated with them and use a struct
/// or enum as a high level API object the state can be stored in. For our
/// Example API we just have an empty struct.
pub struct Example {}

impl Example {
    /// I'm a doc comment, which is good to have on the functionality that the
    /// modules are actually going to use!
    // The name of the method/function is generally the same as FFI function it
    // is wrapping. The signature itself, is mostly the same as the "original"
    // function example, though notably, `ark-api` has its own `Error` type
    // which is generally recommended over the more primitive `ErrorCode`
    pub fn is_ready(&self, name: &str, max: u32) -> Result<ffi::IsReady, Error> {
        let mut is_ready = ffi::IsReady {
            is_ready: false,
            size: 0,
            _pad: Default::default(),
        };

        let state = ffi::State::SomeStateX;

        // FFI functions can only be called within an unsafe block
        let error_code = unsafe {
            ffi::example__is_ready(
                // Split the string into pointer + len
                name.as_ptr(),
                name.len() as u32,
                max,
                // Cast the enum to the correct primitive type
                state as u32,
                // References can automatically be converted to pointers
                &mut is_ready,
            )
        };

        // This helper macro converts the errorcode into an error for us
        if error_code != ErrorCode::Success {
            return Err(Error::from(error_code));
        }

        Ok(is_ready)
    }
}