99 lines
2.1 KiB
C
99 lines
2.1 KiB
C
# Host files
|
|
|
|
File path: `out/dir/api.rs`
|
|
|
|
```rust
|
|
// Host code
|
|
/* Rust API */
|
|
#[derive(Debug)]
|
|
pub enum Commands {
|
|
/// Call out an outstanding person
|
|
#[allow(non_camel_case_types)]
|
|
call_me_back_outstanding {
|
|
callback: extern "C" fn(trixy::types::String) -> trixy::types::String,
|
|
},
|
|
One(one::One),
|
|
}
|
|
pub mod one {
|
|
#[derive(Debug)]
|
|
pub enum One {
|
|
/// Call out a person
|
|
#[allow(non_camel_case_types)]
|
|
call_me_back { callback: extern "C" fn(trixy::types::u32) },
|
|
}
|
|
}
|
|
/* C API */
|
|
#[no_mangle]
|
|
pub extern "C" fn call_me_back_outstanding(
|
|
callback: extern "C" fn(trixy::types::String) -> trixy::types::String,
|
|
) -> core::ffi::c_int {
|
|
callback_function(crate::Commands::call_me_back_outstanding {
|
|
callback: match callback.try_into() {
|
|
Ok(ok) => ok,
|
|
Err(err) => {
|
|
trixy::types::traits::errno::set(err.into());
|
|
return 0;
|
|
}
|
|
},
|
|
});
|
|
return 1;
|
|
}
|
|
pub mod one_c {
|
|
#[allow(unused_imports)]
|
|
use crate::callback_function;
|
|
}
|
|
#[no_mangle]
|
|
pub extern "C" fn one_call_me_back(
|
|
callback: extern "C" fn(trixy::types::u32),
|
|
) -> core::ffi::c_int {
|
|
callback_function(
|
|
crate::Commands::One(crate::one::One::call_me_back {
|
|
callback: match callback.try_into() {
|
|
Ok(ok) => ok,
|
|
Err(err) => {
|
|
trixy::types::traits::errno::set(err.into());
|
|
return 0;
|
|
}
|
|
},
|
|
}),
|
|
);
|
|
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"
|
|
|
|
/**
|
|
* Call out an outstanding person
|
|
*/
|
|
extern int call_me_back_outstanding (const char *(*callback) (const char *));
|
|
|
|
/**
|
|
* Call out a person
|
|
*/
|
|
extern int one_call_me_back (void (*callback) (uint32_t));
|
|
|
|
struct one
|
|
{
|
|
int (*call_me_back) (void (*callback) (uint32_t));
|
|
};
|
|
|
|
const struct one one = {
|
|
.call_me_back = one_call_me_back,
|
|
};
|
|
|
|
#endif // if !defined TRIXY_MAIN_HEADER
|
|
// vim: filetype=c
|
|
```
|