我如何从 Rust 中的 C 联合访问值?

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

我正在尝试调用 C 库 SDL2 中的函数 SDL_PollEvent。我知道已经有可用的包装器,但我想创建自己的包装器,只是为了学习。该函数需要一个指向此 C 联合的指针:

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;

我导入了这样的函数:

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

并这样声明类型:

type SdlEvent = [u8; 56];                                                    

现在我可以调用 SDL_Pollevent 并检索联合的 type 值:

// 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 
     }
    }                                      
}

这工作正常,但现在我想知道按下了哪个键,这意味着我需要检索类型 SDL_KeyboardEvent 的值 key。我该怎么做?

联合基本上和 C 完全一样,这意味着访问它们是不安全的。从 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);
    }
}