Search code examples
rustunions

How can I access values from a C union in Rust?


I'm trying to call the function SDL_PollEvent from the C library SDL2. I'm aware that there are already wrappers available, but I'd like to create my own, just for learning. The function expects a pointer to this C union:

typedef union{
  Uint8 type;
  SDL_ActiveEvent active;
  SDL_KeyboardEvent key;
  SDL_MouseMotionEvent motion;
  SDL_MouseButtonEvent button;
  SDL_JoyAxisEvent jaxis;
  SDL_JoyBallEvent jball;
  SDL_JoyHatEvent jhat;
  SDL_JoyButtonEvent jbutton;
  SDL_ResizeEvent resize;
  SDL_ExposeEvent expose;
  SDL_QuitEvent quit;
  SDL_UserEvent user;
  SDL_SysWMEvent syswm;
} SDL_Event;

I have imported the function like this:

#[link(name = "SDL2")]
extern "C" {
    fn SDL_PollEvent(event: *mut SdlEvent) -> libc::c_int;
} 

And declared the type like this:

type SdlEvent = [u8; 56];                                                    

Now I can call SDL_Pollevent and retrieve the type value of the union:

// According to sizeof(SDL_Event), the unit is 56 bytes
let mut sdl_event: SdlEvent = [0; 56];

unsafe { SDL_PollEvent(&mut sdl_event) };

let event_type: u32 = u32::from_be((sdl_event[0] as u32) << 24 | (sdl_event[1] as u32) << 16 |
                                   (sdl_event[2] as u32) << 8 |
                                   (sdl_event[3] as u32));

match event_type {                              
    0x100 => {
        Event {                     
             // 0x100 is SDL_QUIT -> quit application
     }
    }                                      
    0x200 => {
        Event { // SDL_KEYDOWN
             // How can I 
     }
    }                                      
}

This works fine, but now I'd like to know which key has been pressed, which means I need to retrieve the value key of the type SDL_KeyboardEvent. How can I do this?


Solution

  • Unions are basically exactly like C, which means they are unsafe to access. They are also an unstable feature as of Rust 1.16:

    #![feature(untagged_unions)]
    
    extern crate libc;
    
    // Definitions for SDL_ActiveEvent, SDL_KeyboardEvent, etc.
    
    #[repr(C)]
    union SDLEvent {
        typ: libc::uint8_t,
        active: SDL_ActiveEvent,
        key: SDL_KeyboardEvent,
        // All the rest
    }
    
    fn thing(event: SDLEvent) {
        unsafe { 
            println!("{}", event.typ);
        }
    }