2024-03-26 16:08:49 +00:00
|
|
|
# Host files
|
2024-03-26 16:28:21 +00:00
|
|
|
|
2024-03-26 16:08:49 +00:00
|
|
|
File path: `out/dir/api.rs`
|
2024-03-26 16:28:21 +00:00
|
|
|
|
2024-03-26 16:08:49 +00:00
|
|
|
```rust
|
|
|
|
// Host code
|
|
|
|
/* Rust API */
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Commands {
|
|
|
|
Trinitrix(trinitrix::Trinitrix),
|
|
|
|
}
|
2024-03-27 07:07:29 +00:00
|
|
|
/// Attribute doc comment
|
2024-03-26 16:08:49 +00:00
|
|
|
pub mod trinitrix {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Trinitrix {
|
2024-03-27 07:07:29 +00:00
|
|
|
/// Attribute doc comment, but very ##" "## "# " escaped
|
2024-03-26 16:08:49 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
hi { trixy_output: trixy::oneshot::Sender<trixy::types::String>, name: String },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* C API */
|
2024-03-27 21:26:21 +00:00
|
|
|
pub mod trinitrix_c {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use crate::callback_function;
|
|
|
|
}
|
2024-03-26 16:08:49 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn trinitrix_hi(
|
|
|
|
output: *mut trixy::types::String,
|
2024-03-27 21:26:21 +00:00
|
|
|
name: trixy::types::String,
|
2024-03-26 16:08:49 +00:00
|
|
|
) -> core::ffi::c_int {
|
|
|
|
let output_val: trixy::types::String = {
|
|
|
|
let (tx, rx) = trixy::oneshot::channel();
|
2024-03-27 21:26:21 +00:00
|
|
|
callback_function(
|
|
|
|
Commands::Trinitrix(crate::trinitrix::Trinitrix::hi {
|
|
|
|
trixy_output: tx,
|
|
|
|
name: match name.try_into() {
|
|
|
|
Ok(ok) => ok,
|
|
|
|
Err(err) => {
|
|
|
|
trixy::types::traits::errno::set(err.into());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2024-03-26 16:08:49 +00:00
|
|
|
let recv = rx
|
|
|
|
.recv()
|
|
|
|
.expect("The channel should not be closed until this value is received");
|
|
|
|
recv.into()
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
std::ptr::write(output, output_val);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// vim: filetype=rust
|
|
|
|
```
|
|
|
|
|
|
|
|
# Auxiliary files
|
2024-03-26 16:28:21 +00:00
|
|
|
|
2024-03-26 16:08:49 +00:00
|
|
|
File path: `dist/interface.h`
|
2024-03-26 16:28:21 +00:00
|
|
|
|
2024-03-26 16:08:49 +00:00
|
|
|
```c
|
|
|
|
#if !defined TRIXY_MAIN_HEADER
|
|
|
|
#define TRIXY_MAIN_HEADER
|
|
|
|
|
|
|
|
#include "errno.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "vec.h"
|
|
|
|
|
2024-03-27 07:07:29 +00:00
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Attribute doc comment, but very ##" "## "# " escaped
|
|
|
|
*/
|
2024-03-26 16:08:49 +00:00
|
|
|
extern int trinitrix_hi (const char **trixy_output, const char *name);
|
|
|
|
|
2024-03-27 07:07:29 +00:00
|
|
|
/**
|
2024-03-27 09:51:39 +00:00
|
|
|
* Attribute doc comment
|
|
|
|
*/
|
2024-03-26 16:08:49 +00:00
|
|
|
struct trinitrix
|
|
|
|
{
|
|
|
|
int (*hi) (const char **, const char *);
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct trinitrix trinitrix = {
|
|
|
|
.hi = trinitrix_hi,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // if !defined TRIXY_MAIN_HEADER
|
|
|
|
// vim: filetype=c
|
|
|
|
```
|