rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.58k stars 12.74k forks source link

[Nested pointer]How translate rust struct to nested pointer? #48688

Closed yangchengjian closed 6 years ago

yangchengjian commented 6 years ago

There are a rust function generated by bindgen with a header c_api.h

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ArSession_ {
    _unused: [u8; 0],
}
pub type ArSession = ArSession_;

pub fn ArSession_create(env: *mut ::std::os::raw::c_void,
                            application_context: *mut ::std::os::raw::c_void,
                            out_session_pointer: *mut *mut ArSession)
     -> ArStatus;

and I want to call from another rust module

unsafe {
        let mut s = ArSession{_unused};
        let mut ar_session : *mut *mut ArSession = xxxxxxxx ;                               <---
        let ar_status:ArStatus = ArSession_create(env, application_context, ar_session);
    }

How should I translate rust struct to nested pointer?

nagisa commented 6 years ago

To achieve the equivalent of

ArSession **ar_session = NULL;

you can write

let ar_session: *mut *mut () = ::std::ptr::null_mut();

To achieve the equivalent of:

ArSession *ar_session = NULL;
ArSession_create(env, application_context, &ar_session)

write

let mut ar_session: *mut ArSession = ::std::ptr::null_mut();
ArSession_create(env, application_context, &mut ar_session);

All that being said, Rustc issue tracker is not a help forum. Next time, you can request for help on the users forum, reddit, StackOverflow or IRC (#rust @ irc.mozilla.org).