Global pointer to c structure

⚓ Rust    📅 2025-08-19    👤 surdeus    👁️ 6      

surdeus

I am interfacing RUST with an embedded c library and I am getting stuck.

In C, I have the following struct:

    struct debug_context {
           uintptr_t  handle;
           int un_get_ch_buffer;
           void (*dbg_putc_func)( struct debug_context *pCtx, int ch );
  
           int (*dbg_getc_func)( struct debug_context *pCtx, uinptr_t time_start, in timeout );
    };

    struct debug_context *pGLBL_DEBUG_CONTEXT;

My questions are:

A) How do I declare the "extern C" pointer?

b) Initially, the pointer pGBL_DEBUG_CONTEXT is NULL. once ready it is non-zero

   So the rust code, needs to do this:

   RUST_dbg_putc( ch : i32 )
   {
       if pGBL_DEBUG_CONTEXT == None {
            // Debug io is not possible at this time.
      } else {
            (*(pGLBL_DEBUG_CONTEXT->dbg_putc_func))( pGLBL_DEBUG_CONTEXT, ch );
       }
   }

What I have tried is this (in rust)

#[repr(C)] 
struct DebugContext {

    pub debug_handle : *mut c_void,  // Rust does not like: intptr_t GRRR!!
    pub un_get_buffer : i32,
    pub dbg_putc_raw : unsafe extern "C" fn(myself: *mut DebugContext, data_byte : c_int ),
    pub dbg_getc_raw : unsafe extern "C" fn(myself: *mut DebugContext, tstart : c_uint, timeout : c_int )
}

static pGLBL_DBG_CONTEXT : *mut DebugContext = None;

pub fn rust_debug_putc( ch : i32 )
{
    if pGLBL_DBG_CONTEXT != None {
        (* pGLBL_DBG_CONTEXT).debug_putc( pGLBL_DBG_CONTEXT, ch as c_int );
    }
}

But all I am getting are endless syntax errors...
It wants a MUT, so I add a MUT and it does not like what I add...
Fustrated.

3 posts - 3 participants

Read full topic

🏷️ Rust_feed