1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#![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
#[allow(unsafe_code)]
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)
}
}