简体   繁体   中英

How to define extern type?

I have some c functions with a struct pointer argument.

extern "C" {
    fn InitSomeStruct() -> *SomeStruct;
    fn SomeFunction(v: *SomeStruct);
    fn DestroySomeStruct(v: *SomeStruct);
}

fn main() {
    unsafe {
        let s = InitSomeStruct();
        SomeFunction(s);
        DestroySomeStruct(s);
    }
}

The implementation of SomeStruct is unknown. How to declare and use external struct like SomeStruct from the rust code?

The convention is to use an empty enum for opaque FFI types, that is:

enum SomeStruct {}

An empty struct like struct SomeStruct; is also used sometimes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM