Hi,
Probably a very simple problem but i am stuck. I am trying to access structure elements in c code from rust. What i have is the following little example:
Project source tree:
Cargo.toml
build.rs
src/
├── test.cpp
├── test.hpp
└── main.rs
main.rs
extern crate libc;
use libc::{size_t};
#[repr(C)]
#[derive(Debug,Copy, Clone)]
pub struct Bt {
opaque: [*const u8; 0]
}
extern "C" {
pub fn bt_del(idx: *mut Bt) -> bool;
pub fn bt_make(size: size_t) -> *mut Bt;
}
struct Index{
bt: *mut Bt,
req: Vec<u8>,
}
impl Index {
pub fn new()->Self{
Index{
bt: Index::make(10 as u32),
req: Vec::new(),
}
}
pub fn make(x:u32) -> *mut Bt{
unsafe {
bt_make(x as size_t)
}
}
}
fn main() {
let mut idx = Index::new();
println!("{:?}", (*idx.bt).size);
}
builder.rs
const FILES: &[&str] = &[
"src/test.cpp",
];
const HEADERS: &[&str] = &[
"src/test.hpp",
];
fn main() {
for file in FILES {
println!("cargo:rerun-if-changed={}", file);
}
for file in HEADERS {
println!("cargo:rerun-if-changed={}", file);
}
cc::Build::new()
.define("COMPILATION_TIME_PLACE", "\"build.rs\"")
.warnings(false)
.extra_warnings(false)
.files(FILES)
.flag("-fPIC")
.compile("bt.a");
println!("cargo:rustc-link-lib=z");
println!("cargo:rustc-link-lib=m");
println!("cargo:rustc-link-lib=stdc++");
}
test.cpp
#include "test.hpp"
extern "C" {
bt_t *bt_make(int size)
{
bt_t *bt;
bt = (bt_t*)calloc(1, sizeof(bt_t));
bt->bt = (uint32_t*)calloc(size, 4);
bt->size = size;
for (int i =0; i < size;i++){
bt->bt[i] = i*2;
}
return bt;
}
bool bt_del(bt_t *bt){
if (bt == 0) return false;
free(bt->bt);
free(bt);
return true;
}
}
test.hpp
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
typedef struct {
uint32_t* bt;
uint32_t size;
} bt_t;
Error:
error[E0609]: no field `size` on type `Bt`
--> src/main.rs:47:33
|
47 | println!("{:?}", (*idx.bt).size);
| ^^^^ unknown field
|
= note: available fields are: `opaque`
For more information about this error, try `rustc --explain E0609`.
Did i miss something or did i completely fail to understand FFI and rust?
Thank you so much for your help!