mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.29k stars 294 forks source link

Special Type Conversion #883

Closed GuiyinLi closed 3 months ago

GuiyinLi commented 10 months ago

How to convert types similar to Result<usize> and Option<Arc<SpinLock<my_type>>> into types in the C language, where my_type is a struct defined in another Rust file(Conversion of Nested Types). Meanwhile, for C structs used in Rust through bindings, can they be directly used as the original C structs in the C language? For example, filp: *mut bindings::file, can I directly use struct file in C language to replace it?" In addition, I would like to know if cbindgen can convert the structs from all Rust files in a project into C language (with mutual dependencies between the structs), because currently I can only convert a single Rust file.

emilio commented 3 months ago

You need to box them or use enums that have #[repr(C)] or #[repr(u8)]. Or treat them as opaque and only pass them by pointer (but that's not usable for return types for obvious reasons).

I would like to know if cbindgen can convert the structs from all Rust files in a project into C language (with mutual dependencies between the structs), because currently I can only convert a single Rust file.

It definitely can. If you point it to a lib.rs it should generate all dependent modules. Also if you want dependencies generated there are options for that (parse_deps, in particular).