2024-03-27 08:59:35 +00:00
|
|
|
# Host files
|
|
|
|
|
|
|
|
File path: `out/dir/api.rs`
|
|
|
|
|
|
|
|
```rust
|
|
|
|
// Host code
|
|
|
|
/* Rust API */
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Commands {
|
|
|
|
Test(test::Test),
|
|
|
|
}
|
|
|
|
pub mod test {
|
|
|
|
/// This struct will be used later on (and this is also a struct doc comment test)
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Callback {
|
|
|
|
/// Very important field
|
2024-03-27 21:26:21 +00:00
|
|
|
pub func: extern "C" fn(trixy::types::String) -> trixy::types::String,
|
2024-03-27 08:59:35 +00:00
|
|
|
/// Very important field for keeping time constant
|
|
|
|
pub timeout: u32,
|
|
|
|
}
|
|
|
|
impl trixy::types::traits::convert_trait::Convertible for Callback {
|
|
|
|
type Ptr = crate::test_c::Callback_c;
|
|
|
|
fn into_ptr(self) -> Self::Ptr {
|
|
|
|
Self::Ptr {
|
|
|
|
func: self.func.into(),
|
|
|
|
timeout: self.timeout.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn from_ptr(
|
|
|
|
ptr: Self::Ptr,
|
|
|
|
) -> Result<Self, trixy::types::error::TypeConversionError> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl TryFrom<crate::test_c::Callback_c> for Callback {
|
|
|
|
type Error = trixy::types::error::TypeConversionError;
|
|
|
|
fn try_from(value: crate::test_c::Callback_c) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
func: value.func.try_into()?,
|
|
|
|
timeout: value.timeout.try_into()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Same thing as above (and a enum doc comment test)
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CallbackPriority {
|
|
|
|
/// Callback **now**
|
|
|
|
High,
|
|
|
|
/// Maybe do a callback
|
|
|
|
Medium,
|
|
|
|
/// Calling back is ..
|
|
|
|
/// really not important ..
|
|
|
|
/// like not even think about doing it
|
|
|
|
Low,
|
|
|
|
}
|
|
|
|
impl From<crate::test_c::CallbackPriority_c> for CallbackPriority {
|
|
|
|
fn from(value: crate::test_c::CallbackPriority_c) -> Self {
|
|
|
|
match value {
|
2024-03-27 21:26:21 +00:00
|
|
|
crate::test_c::CallbackPriority_c::High => Self::High,
|
|
|
|
crate::test_c::CallbackPriority_c::Medium => Self::Medium,
|
|
|
|
crate::test_c::CallbackPriority_c::Low => Self::Low,
|
2024-03-27 08:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Test {
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
execute_callback {
|
|
|
|
callback: crate::test::Callback,
|
|
|
|
priority: crate::test::CallbackPriority,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* C API */
|
|
|
|
pub mod test_c {
|
2024-03-27 21:26:21 +00:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use crate::callback_function;
|
2024-03-27 08:59:35 +00:00
|
|
|
/// Same thing as above (and a enum doc comment test)
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CallbackPriority_c {
|
|
|
|
/// Callback **now**
|
|
|
|
High,
|
|
|
|
/// Maybe do a callback
|
|
|
|
Medium,
|
|
|
|
/// Calling back is ..
|
|
|
|
/// really not important ..
|
|
|
|
/// like not even think about doing it
|
|
|
|
Low,
|
|
|
|
}
|
|
|
|
impl From<crate::test::CallbackPriority> for CallbackPriority_c {
|
|
|
|
fn from(value: crate::test::CallbackPriority) -> Self {
|
|
|
|
match value {
|
2024-03-27 21:26:21 +00:00
|
|
|
crate::test::CallbackPriority::High => Self::High,
|
|
|
|
crate::test::CallbackPriority::Medium => Self::Medium,
|
|
|
|
crate::test::CallbackPriority::Low => Self::Low,
|
2024-03-27 08:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// This struct will be used later on (and this is also a struct doc comment test)
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Callback_c {
|
|
|
|
/// Very important field
|
2024-03-27 21:26:21 +00:00
|
|
|
pub func: extern "C" fn(trixy::types::String) -> trixy::types::String,
|
2024-03-27 08:59:35 +00:00
|
|
|
/// Very important field for keeping time constant
|
|
|
|
pub timeout: trixy::types::u32,
|
|
|
|
}
|
2024-03-27 21:26:21 +00:00
|
|
|
impl TryFrom<crate::test::Callback> for Callback_c {
|
|
|
|
type Error = trixy::types::error::TypeConversionError;
|
|
|
|
fn try_from(value: crate::test::Callback) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
func: value.func.try_into()?,
|
|
|
|
timeout: value.timeout.try_into()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-03-27 08:59:35 +00:00
|
|
|
}
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn test_execute_callback(
|
2024-03-27 21:26:21 +00:00
|
|
|
callback: crate::test_c::Callback_c,
|
|
|
|
priority: crate::test_c::CallbackPriority_c,
|
2024-03-27 08:59:35 +00:00
|
|
|
) -> core::ffi::c_int {
|
2024-03-27 21:26:21 +00:00
|
|
|
callback_function(
|
|
|
|
crate::Commands::Test(crate::test::Test::execute_callback {
|
|
|
|
callback: match callback.try_into() {
|
|
|
|
Ok(ok) => ok,
|
|
|
|
Err(err) => {
|
|
|
|
trixy::types::traits::errno::set(err.into());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
priority: match priority.try_into() {
|
|
|
|
Ok(ok) => ok,
|
|
|
|
Err(err) => {
|
|
|
|
trixy::types::traits::errno::set(err.into());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2024-03-27 08:59:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// vim: filetype=rust
|
|
|
|
```
|
|
|
|
|
|
|
|
# Auxiliary files
|
|
|
|
|
|
|
|
File path: `dist/interface.h`
|
|
|
|
|
|
|
|
```c
|
|
|
|
#if !defined TRIXY_MAIN_HEADER
|
|
|
|
#define TRIXY_MAIN_HEADER
|
|
|
|
|
|
|
|
#include "errno.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "vec.h"
|
|
|
|
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Same thing as above (and a enum doc comment test)
|
|
|
|
*/
|
2024-03-27 08:59:35 +00:00
|
|
|
enum CallbackPriority
|
|
|
|
{
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Callback **now**
|
|
|
|
*/
|
2024-03-27 08:59:35 +00:00
|
|
|
High,
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Maybe do a callback
|
|
|
|
*/
|
2024-03-27 08:59:35 +00:00
|
|
|
Medium,
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Calling back is ..
|
|
|
|
* really not important ..
|
|
|
|
* like not even think about doing it
|
|
|
|
*/
|
2024-03-27 08:59:35 +00:00
|
|
|
Low,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* This struct will be used later on (and this is also a struct doc comment
|
|
|
|
* test)
|
|
|
|
*/
|
|
|
|
struct Callback
|
2024-03-27 08:59:35 +00:00
|
|
|
{
|
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Very important field
|
|
|
|
*/
|
2024-03-27 09:54:01 +00:00
|
|
|
const char *(*func) (const char *);
|
2024-03-27 09:51:39 +00:00
|
|
|
/**
|
|
|
|
* Very important field for keeping time constant
|
|
|
|
*/
|
2024-03-27 08:59:35 +00:00
|
|
|
uint32_t timeout;
|
|
|
|
};
|
|
|
|
extern int test_execute_callback (struct Callback callback,
|
|
|
|
enum CallbackPriority priority);
|
|
|
|
|
|
|
|
struct test
|
|
|
|
{
|
2024-03-27 21:26:21 +00:00
|
|
|
int (*execute_callback) (struct Callback, enum CallbackPriority);
|
2024-03-27 08:59:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct test test = {
|
|
|
|
.execute_callback = test_execute_callback,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // if !defined TRIXY_MAIN_HEADER
|
|
|
|
// vim: filetype=c
|
|
|
|
```
|