Hi,
I have another FFI conundrum i am trying to figure out. I want to fill an array of structs from rust so that i can directly access them in c. Example:
src tree:
build.rs
src - main.rs
- test.cpp
- test.hpp
main.rs
-------------------------------------
extern crate libc;
use libc::{c_char, c_int, size_t};
#[repr(C)]
#[derive(Debug)]
pub struct Arr {
str : *mut i8,
size : u32,
}
#[repr(C)]
#[derive(Debug)]
pub struct Rec {
rarr: *mut Arr,
count : u32,
}
extern "C" {
pub fn printer() -> bool;
}
fn main() {
// how do i fill char array from rust so that i can use printer to print it in c
}
test.hpp
-------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdint.h>
typedef struct {
char* str;
uint32_t size;
} record_t;
typedef struct {
record_t* rarr;
uint32_t count;
} rarray_t;
extern rarray_t * ExtData;
// abi
extern "C" {
bool printer ();
}
test.cpp
--------------------------------------------
#include "test.hpp"
extern "C" {
bool printer(){
for (i = 0; i< ExtData->count; i++){
cout << i << ": ";
for (j = 0; j< ExtData->rarr->size; j++){
cout << ExtData->rarr->str[j] << "";
}
cout<< endl;
}
return true;
}
}
Or is there a better way to do this? So what i am doing right now is filling multiple vectors on rust side and passing them using functions to c and then refilling the two structures in c. data needs to be in c structures like described because i do not want to go changing the entire c code ... also once it is printed it needs to be destroyed from rust side . can this be done ?
Thank you ! Please do not hold it against me for maybe asking trivial question. I still cannot judge if they are trivial or not